'"cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception"
When i use the VsCode debugger to run the code i dont get any error at all and everything works fine, but when i try to run the code without the debugger i get this error:
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (116) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): vtest.avi in function 'cv::icvExtractPattern'
I use OpenCV 4.2.0.32 and numpy 1.18.1
The code i use is:
import cv2
import numpy as np
video = cv2.VideoCapture("vtest.avi")
ret, frame1 = video.read()
ret, frame2 = video.read()
while video.isOpened():
try:
difference = cv2.absdiff(frame1, frame2)
grayscale = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(grayscale, (5, 5), 0)
_, threshold = cv2.threshold(blur, 35, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(threshold, None, iterations=2)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if cv2.contourArea(contour) < 950:
continue
cv2.drawContours(frame1, contours, -1, (0, 255, 0), 1)
cv2.rectangle(frame1, (x, y), (x+w, y+h), (120, 0, 150), 2)
cv2.imshow("feed", frame1)
frame1 = frame2
ret, frame2 = video.read()
if cv2.waitKey(50) == 27:
break
except:
break
cv2.destroyAllWindows()
video.release()
Solution 1:[1]
I had the same problem. I was able to solve this issue by reinstalling my Python virtual environment. My speculation is that this is probably a dependency issue/conflict between OpenCV and a backend video encoder library.
Solution 2:[2]
Obviously OpenCV try to open your file with the CAP_IMAGE backend. Regarding the file extension (.avi) I guess it is a video file. And if OpenCV tries to open it with the still image backend, this is probably because it can't find a better choice. If you're sure you have a backend to decode AVI video on your system and your opencv was build with this backend support, then you could try to force the backend by setting your API preference in the VideoCapture constructor :
video = cv2.VideoCapture("vtest.avi", cv.CAP_FFMPEG)
See OpenCV documentation for a list of supported capture backend. To get a list of the build option use to build your opencv you could use :
print cv2.getBuildInformation()
This should print the list of available backend. Some backend libraries need to be installed separately (GStreamer and FFMPEG iirc).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dharman |
Solution 2 | antoine |