I am a beginner in Python and OpenCV. I am trying out a piece of code which takes an input image from the webcam. The following is the piece of code.
cam = create_capture(video_src, fallback='synth:bg=../cpp/lena.jpg:noise=0.05')
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
rects = detect(gray, cascade)
vis = img.copy()
draw_rects(vis, rects, (0, 255, 0))
for x1, y1, x2, y2 in rects:
roi = gray[y1:y2, x1:x2]
vis_roi = vis[y1:y2, x1:x2]
subrects = detect(roi.copy(), nested)
draw_rects(vis_roi, subrects, (255, 0, 0))
dt = clock() - t
draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000))
cv2.imshow('facedetect', vis)
if 0xFF & cv2.waitKey(5) == 27:
break
cv2.setMouseCallback('facedetect',capture_image)
cv2.destroyAllWindows()
I am using both both Python 2.7 ans Python 3.4 to execute this. In both I face a strange problem. The code gives an assertion Error like this
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file ........\opencv\modules\imgproc\src\color.cpp, line 3737 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: ........\opencv\modules\imgproc\src\color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
but it happens only on every ALTERNATE time of running. What might be the issue? I read from other posts that this error occurs when cv2.cvtColor tries to convert an empty image which does not have 3 or 4 channels. This happens usually when a wrong path is specified. For my case, since it is working fine EVERY ALTERNATE time, the source cannot be wrong. Please help!!
img.dtype. - DrVwaitKey()may be too small. If the camera cannot ready a new frame within 5ms, it may return an empty frame. Try passing a larger time increment. - Aureliuscam.readcall should block until there is a new frame available. - DrV