Ask Your Question
0

webcam capture problems

asked 2015-01-16 08:17:57 -0500

reggie gravatar image

I have the following simple python code:

import numpy as np
import cv2



'''Device index is just the number to specify 
which camera. Normally one camera will be connected 
(as in my case). So I simply pass 0 (or -1)
'''

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

But it doesn't run, and i get the error message:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file C:\OpenCV-2.4.2\sources\modules\imgproc\src\color.cpp, line 3739
Traceback (most recent call last):
  File "C:\Users\dom\Dropbox\eclipse_luna\testpythen\video_capture.py", line 18, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: C:\OpenCV-2.4.2\sources\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor

I'm new to OpenCV so dont know how to get this code working. I've googled around this error, but am a bit lost. Could it be a problem with my color.cpp module; or the setup of my webcam?

I'm using Eclipse to edit my python scripts.

edit retag flag offensive close merge delete

Comments

1

your input image is probably empty/invalid.

please add checks , if the capture opened, if ret was true, etc.

berak gravatar imageberak ( 2015-01-17 04:54:04 -0500 )edit

Sounds logical, how could I do that?

reggie gravatar imagereggie ( 2015-01-21 07:56:03 -0500 )edit

checks like:

if not cap.isOpened() raise Exception("bwaa, your cam does not work !")
...
if frame==None: continue #darn old cam needs some warmup
berak gravatar imageberak ( 2015-01-21 08:01:49 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-01-17 04:49:26 -0500

This message normally means that your input image should have 3 or 4 channels. What is the type of your frame before passing it to cvtColor?

edit flag offensive delete link more

Comments

I'm not sure on the type of my frame. How could I find out?

reggie gravatar imagereggie ( 2015-01-21 07:47:17 -0500 )edit

import numpy as np

import cv2

img = cv2.imread('foo.jpg')

height, width, channels = img.shape

print height, width, channels

 600 800 3
Nandika gravatar imageNandika ( 2016-01-15 02:18:18 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-01-16 08:17:57 -0500

Seen: 1,922 times

Last updated: Jan 17 '15