cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception - python

I was browsing StackOverflow, and of course I found a similar post about the problem!
"cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception"
However, as it has been 5 months with no answers on that, I've opened a new thread. Here it goes!
I've got an error when I try to run cv2.VideoCapture(url).
The box is running openCV 4.4, Centos 7, Raspberry PI 4, ARMV7L. See source and logs bellow.
I believe it's related to NumPy library, however, I'm still debugging it.
Does anyone has experienced the same? Any ideas on how to solve it?
Please see the source and log messages bellow.
Best wishes,
I
import numpy as np
import cv2
import sys
import random, string
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
url = 'rtsp://<ip-direction:port/path>'
print("Running py script detectFacesEyes.py")
video_capture = cv2.VideoCapture(url)
print(video_capture)
while True:
ret, img = video_capture.read()
print(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=3,
minSize=(30, 30))
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255,0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eyeCascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0), 2)
if len(eyes) != 0:
crop_img = img[y-99:y+h+99, x-36:x+w+36]
if len(crop_img) != 0:
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(12))
status = cv2.imwrite(result_str+".jpg", crop_img)
#This breaks on 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
python detectFacesEyes.py
Running py script detectFacesEyes.py
[ERROR:0] global /usr/local/src/opencv_build/opencv/modules/videoio/src/cap.cpp (142) open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.4.0-dev) /usr/local/src/opencv_build/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): rtsp://ip-direction:port/path in function 'icvExtractPattern'
<VideoCapture 0xb196d710>
None
Traceback (most recent call last):
File "detectFacesEyes.py", line 19, in
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0-dev) /usr/local/src/opencv_build/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

Related

Python - CV2 Error: " error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale' +global error when detecting blink

I've been trying to run a program that detects when you blink and gives a message in the console in response to a blink. It has run successfully on a different laptop before but has been causing errors on my current laptop. I'm not sure what the errors mean. I've tried looking at similar posts but I haven't been able to figure it out yet. I'm a bit new to this area. Could you help me out? Following are the errors, the versions of the programs and the code. Thank you!
Errors:
Traceback (most recent call last):
File "C:...\PycharmProjects\PersonalProjects\main.py", line 19, in
faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(200, 200))
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:1] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Versions
Python 3.9,
Pycharm 2021.2,
Opencv-python 4.5.3.56,
numpy 1.21.2,
Code:
import numpy as np
import cv2
#initializiing Face and eye cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
#Variable store execution state
first_read = True
cap = cv2.VideoCapture(0)
ret ,img = cap.read()
while(ret):
ret, img = cap.read()
#Coverting recorded image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Applying filter to remove impurities
gray = cv2.bilateralFilter(gray, 5, 1, 1)
#Detecting the face for region of image to be fed to eye classifier
faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(200, 200))
if(len(faces) > 0):
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0),2)
#rol_face is face fed to eye classifier
rol_face = gray[y:y+h, x:x+w]
rol_face_clr = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(rol_face, 1.3, 5, minSize=(50, 50))
if(len(eyes) >= 2):
#Check if program is running for detection
if(first_read):
cv2.putText(img,"Eye detected, press s to begin", (70, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0,255,0), 2)
else:
cv2.putText(img, "Eyes open!", (70, 70), cv2.FONT_HERSHEY_PLAIN, 2, (255,255,255), 2)
else:
if(first_read):
#to ensure if eyes are present before starting
cv2.putText(img, "No eyes detected", (70, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 2)
else:
#This will print on console and restart algorithm
print("Blink detected -- do you want to continue; press y")
cv2.waitKey(3000)
first_read = True
else:
cv2.putText(img, "No face detected", (100, 100), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2)
cv2.imshow('img', img)
a = cv2.waitKey(1)
if(a==ord('q')):
break
elif(a==ord('s') and first_read):
#This will start the detection
first_read = False
cap.release()
cv2.destroyAllWindows()

assertion failed 215 error when trying to detect faces

I'm using OpenCV with Python to try to detect faces with multidetect but it seems to have a problem with it.
This is my code:
#import required libraries
import cv2
import time
#point to the haar cascade file in the directory
cascPath = "haarcascade.xml"
#start the camera
video_capture = cv2.VideoCapture(0)
#give camera time to warm up
time.sleep(0.1)
#start video frame capture loop
while True:
# take the frame, convert it to black and white, and look for facial features
faceCascade = cv2.CascadeClassifier(cascPath)
ret, frame = video_capture.read()
if not ret: break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# use appropriate flag based on version of OpenCV
if int(cv2.__version__.split('.')[0]) >= 3:
cv_flag = cv2.CASCADE_SCALE_IMAGE
else:
cv_flag = cv2.cv.CV_HAAR_SCALE_IMAGE
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv_flag
)
#for each face, draw a green rectangle around it and append to the image
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
#display the resulting image
cv2.imshow('Video', frame)
#set "q" as the key to exit the program when pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# clear the stream capture
video_capture.release()
cv2.destroyAllWindows()
and this is the error I'm getting:
faces = faceCascade.detectMultiScale(
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Solution:
The important part of your error is:
error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Basically the problem is it can't find the file in the given directory or it could be a format problem. So, try to download the file again or use the file that comes with the opencv pip package like so:
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

OpenCV error (-215:Assertion failed) !empty() [duplicate]

This question already has answers here:
Face detection throws error: !empty() in function cv::CascadeClassifier::detectMultiScale
(9 answers)
Closed 9 months ago.
I have the following code:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'opencv_haarcascade_eye.xml')
img = cv2.imread('lena.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(gray)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('frame',img)
cv2.destroyAllWindows()
Running it produces the following error:
eyes = eye_cascade.detectMultiScale(roi_gray)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ttbyx0jz\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
When I removed eyes = eye_cascade.detectMultiScale(roi_gray) part, the code works fine with fave detection but with eyes = eye_cascade.detectMultiScale(roi_gray) parth its showing error.
How to solve this issue?
The error is just in the process of loading of the classifier. It should be
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_eye.xml')
Instead of
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'opencv_haarcascade_eye.xml')
At least, this solution is shown here, and I tested it with OpenCV(4.5.1)
According to this topic about roi_gray explanation
for (x,y,w,h) in faces:
roi_gray=gray[y:y+h,x:x+w] #This particular code will return the cropped face from the image.
roi_color = img[y:y+h, x:x+w] #This particular code will return the details of the image that u will recive after getting the co-ordinates of the image.
Are you sure there is a face in the image? roy_gray returns cropped face from the image but if there isn't any maybe you will get an error I think.

i am getting an error in my face recognition code while excuting it in pycharm, have also tried executing it on terminal

My face recognition code is getting and error. The error is as follows:
Traceback (most recent call last):
File "C:/Users/Harsh/Desktop/python codes/main.py", line 19, in <module>
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xeqjxthj\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xeqjxthj\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
the code is as follows:
import cv2
face_cascade = \
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_frontalface_alt.xml')
eye_cascade = \
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
if ret is True:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
continue
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for(x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+h]
roi_color = img[y:y+h, x:x+h]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xFf
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
That error means that your xml file could not be found.
You got some typos here:
face_cascade =
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_frontalface_alt.xml')
eye_cascade =
cv2.CascadeClassifier('/C:/User/Harsh/Downloads/haarcascade_eye.xml')
Notice the slash (/) at the beginning of the path. Correct that and it should work.

Assertion fail with Open CV

I'm new to openCV and am trying to get openCV to work my USB webcam on Win7 with Python 3.8. I've got the basic tutorial from here modified from Raspberry Pi cam by the same author here.
which is:
#!/usr/bin/python3
import time
import numpy as np
import cv2
#point to the haar cascade file in the directory
cascPath = "haarcascade.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
#start the camera
video_capture = cv2.VideoCapture(0)
#give camera time to warm up
time.sleep(0.1)
#start video frame capture loop
while True:
# take the frame, convert it to black and white, and look for facial features
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# use appropriate flag based on version of OpenCV
if int(cv2.__version__.split('.')[0]) >= 3:
cv_flag = cv2.CASCADE_SCALE_IMAGE
else:
cv_flag = cv2.cv.CV_HAAR_SCALE_IMAGE
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv_flag
)
#for each face, draw a green rectangle around it and append to the image
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
#display the resulting image
cv2.imshow('Video', frame)
#set "q" as the key to exit the program when pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# clear the stream capture
video_capture.release()
cv2.destroyAllWindows()
It should run out of the box, but I get the error below and I'm not sure why. CV_flag and gray have data and the other parameters are filled. Any ideas.
C:\Users\Ghoul>py D:\LearnPython\open_cv_face_track_test.py -3.8
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674)
SourceReaderCB::~SourceReaderCB
terminating async callback
Traceback (most recent call last):
File "D:\LearnPython\open_cv_face_track_test.py", line 31, in <module>
faces = faceCascade.detectMultiScale(
cv2.error: OpenCV(4.1.2) C:\projects\opencv-
python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Ass
ertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
The faceCascade classifier is empty, which means it was unable to retrieve the classifier from the path provided.
You can replace the line
cascPath = "haarcascade.xml"
with:
cascPath = '../../haarcascade.xml'
where you provide the full path of the xml file for cascPath.

Categories