6

Since a couple of days I can't open my iSight camera from inside an opencv application any more. cap = cv2.VideoCapture(0) returns, and cap.isOpened() returns true. However, cap.grab() just returns false. Any ideas?

Example Code:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
rval = True

while rval:
    rval, frame = vc.read()
    cv2.imshow("preview", frame)

    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break
  • Mac OS 10.8.5
  • Python 2.7.5 (but also not working from inside a C++ app)
  • OpenCV 2.4.6.1
Generic Bot
  • 309
  • 1
  • 4
  • 8
Alex Attinger
  • 63
  • 1
  • 1
  • 3
  • Did you figure this out? I'm asking again here: http://stackoverflow.com/questions/19187076/capturing-from-macbook-pro-isight-with-opencv – escapecharacter Oct 04 '13 at 17:15

2 Answers2

13

This is how I got the camera working for your code (on OSX 10.6):

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

rval, frame = vc.read()

while True:

  if frame is not None:   
     cv2.imshow("preview", frame)
  rval, frame = vc.read()

  if cv2.waitKey(1) & 0xFF == ord('q'):
     break
Generic Bot
  • 309
  • 1
  • 4
  • 8
qidaas
  • 156
  • 1
  • 4
  • Thanks. Seems to work again after a complete reinstall. Also, maybe it took longer to set up the input channel. A frame actually only comes in on the second or third call to read(). Did not have this problem earlier – Alex Attinger Oct 20 '13 at 19:15
  • How did you install OpenCV on mac? – Sandeep Giri Apr 20 '19 at 10:49
1

I had a segmentation fault after I grab an image. It turned out that I used cv2.destroyAllWindows() before cap.release(). Below I show working code.

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#do some ops

cap.release()
cv2.imshow("output", output)
cv2.waitKey(0)
cv2.destroyAllWindows()

This code works on El Captain.

Piotr Badura
  • 1,574
  • 1
  • 13
  • 17
  • Please edit your answer to explain what your code does and how it answers the question? – ScottishTapWater Nov 02 '16 at 21:37
  • 1
    @alex-attinger has a problem with grabbing image. I showed my code which grab a picture and works on macOS. – Piotr Badura Nov 03 '16 at 08:10
  • I understand that your code might solve the problem, but it's a lot more useful if you explain what was wrong with the original and how your code actually solves the question. Especially given this question is from 2013 – ScottishTapWater Nov 03 '16 at 14:54