I'm working in a Python app with opencv and a USB camera. Sometimes the camera driver throws an exception:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp, line 3650

or a Segmentation Fault.

I think it's because of the camera driver. The camera stops working after that (though the camera light remains on) and I need to disconnect and reconnect it.

Is there a way to unmount and then remount that Camera with Python? Or is there an alternative way to fix this?

(I've searched on /media and /mnt, and I could not find the camera there).

This is my code:

self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, camerapicshowwidth)
self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, camerapicshowheight)
while updateCamera:
    _, frame = self.cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame,cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    img.thumbnail((showpicsmallwidth, showpicsmallheight),Image.BILINEAR);
    imgtk = ImageTk.PhotoImage(image=img)
    self.cameraLabel.imgtk = imgtk
  • 1
    You should add a check if frame.empty() and if it returns false, you should proceed to process the frame. It's been noted that once camera is opened, it takes certain amount of time (probably a few hundred ms) before it could actually capture frames. Hope it helps. – dhruvvyas90 Jan 7 '16 at 9:03
  • 1
    I've tried but i got this error: AttributeError: 'numpy.ndarray' object has no attribute 'empty' – jorexe Jan 8 '16 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.