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

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.

Related

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.

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

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'

error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

I am working on a project image processing and i get an error when run this code.
I use python and opencv in the project.The error was,
error: D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\color.cpp:9748: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
and this is the code i used,
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('C:\\Users\\Hp\\Downloads\\haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(1)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
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+w]
roi_color = img[y:y+h, x:x+w]
cv2.imshow('img',img)
k = cv2.waitkey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Please help i was stucked here for 5 hrs.
Thanks.
The img variable does not contain the correct dimensions. If you have only one camera in your system use cv2.VideoCapture(0) instead of cv2.VideoCapture(1).

python Opencv error -215 scn ==3 || scn == 4

I am using python 2.7 on windows 8.1 while working with python-tesseract-0.9-0.4.win32-py2.7 my previous code's weren't working properly like face detection.
The code below is to detect face in a pic. It works fine when I uninstall python-tesseract-0.9-0.4.win32-py2.7. But I need it for OCR.
what should I do.?
the error is : open cv error: (-215) scn == 3 || scn == 4 in function cvtColor
import cv2
import sys
imagepath = ('C:\Users\Default\Downloads\group1.jpg')
cascpath ('C:\opencv\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
caspatheye=('C:\opencv\opencv\sources\data\haarcascades\haarcascade_eye.xml')
faceCascade = cv2.CascadeClassifier(cascpath)
eye_cascade = cv2.CascadeClassifier(caspatheye)
img = cv2.imread(imagepath)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(15,15),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x,y,w,h) in faces:
img = 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.namedWindow("faces found", cv2.WINDOW_NORMAL)
cv2.imshow("faces found",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV 3.0 Beta - Python - Face Detection via Webcam - code gives me error

I wrote and looked over this code multiple times and it seems right to me especially that it's very simple and straight forward (Took snippet out of opencv tutorial), however it is still giving me an error. I've checked the error and I understand that it's missing channels, however, I don't know how to fix it. Any help would be appreciated. I'm using Windows x64 and Spyder (from Anaconda distribution) as my IDE. Could it be hardware issue? (webcam)
#####################################################
# Face Detection (Video) #
#####################################################
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
video = cv2.VideoCapture(0)
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[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('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
Here's the error I get:
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:5731:
error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
i guess, you wanted a 'continuous detection, not a 'single shot' one, right ?
you're just some small changes away:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
if face_cascade.empty(): raise Exception("your face_cascade is empty. are you sure, the path is correct ?")
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
if eye_cascade.empty(): raise Exception("your eye_cascade is empty. are you sure, the path is correct ?")
video = cv2.VideoCapture(0)
while(video.isOpened()):
ret, frame = video.read()
if frame not None:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[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('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()

Categories