Sunday 13 March 2016

Python / OpenCV - Intermittent error



I have written a program that captures images from an usb camera and track object position based on detecting colors in each frame. Intermittent, (this can happen after 1 minute, 10 minutes or half an hour) I get the error message:



OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cv::cvtColor, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp, line 7343
Traceback (most recent call last):
File "", line 1, in
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile

exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/file.py", line 371, in
hsv_frame = cv2.cvtColor(blur_frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7343: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cv::cvtColor


The process I follow is:






  • capture a frame with the camera, cap = cv2.VideoCapture(1) / cap.read()

  • transform geometry, cv2.warpPerspective

  • gaussian blurr filter, cv2.GaussianBlur

  • BGR to HSV conversion, cv2.cvtColor(blur_frame, cv2.COLOR_BGR2HSV)

  • Contour finding and analyzing, cv2.threshold




It works flawless, sometimes. And then I suddenly get the error message that stops the program. I think it is strange that it happens at such a late step in the process. If it was due to the camera driver being corrupted, the error should have occurred earlier?




Any thoughts what might be causing this?



EDIT 1: Can it be that i grab the frame from the global variable holding the frame, while it is being written to from the stream thread?



EDIT 2: Problem occurred due to lack of sync between threads. Moved the capture to the processing loop, and now it works perfect, and at almost the same speed as when in separate threads...


Answer



As GPPK commented, it is likely that your camera sometimes does not capture a picture. This may happen due to race conditions, problems in your IO system, high CPU load etc.
In the openCV documentation you can see that the method retrieve returns a boolean as well as the image.
cv2.VideoCapture.retrieve([image[, channel]]) → retval, image
When capturing, just check that retval is True and if it is not, try to capture another frame. If capturing fails repeatedly, the connection to your camera broke.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...