This question already has answers here:
error: (-215) !empty() in function detectMultiScale
(26 answers)
Closed 10 months ago.
I am trying to do face and eye recognition with rectangles for eyes and face using OpenCv Python.
I read some of the questions and I tried the answers below the questions on similiar topics but still getting the error. The code that i've tried activates the webcam and in a second it stops working.
Here the error message:
error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Here my code that i have tried:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye_default.xml')
while True:
ret, frame = cap.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), 5)
roi_gray = gray[y:y+w, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Thank you.
Edit: I noticed that once I run the code the webcam has been activated and if there is no face in front of the camera it stays active without any error but once I show my face it stops working.
Hope this will work !
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
while True:
ret, frame = cap.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), 5)
roi_gray = gray[y:y+w, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Related
Last time I checked my code, the window size is normal. But when I run it now, the window became small. Please does anybody know how to get this to normal?
Here's my code:
import cv2 as cv
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeglasses_cascade = cv.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
cap = cv.VideoCapture(0)
while cap.isOpened():
_, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv.rectangle(frame, (x,y), (x+w, y+w), (255,0,0), 3)
cv.putText(frame,'Face', (x, y+h+30), cv.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
glasses = eyeglasses_cascade.detectMultiScale(roi_gray)
for (gx, gy, gw, gh) in glasses:
cv.rectangle(roi_color, (gx,gy), (gx+gw, gy+gh), (0,255,0), 2)
cv.imshow("img", frame)
if cv.waitKey(1) & 0xFF == ord('x'):
break
cap.release()
You have to tell OpenCV what size to use with the capture device:
cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 1080)
Note, your camera may only support certain resolutions, so it's important to check that.
I restarted my pc, and weirdly enough, it works. Window size is now back to normal
I have a problem with correct eye detection in the program below. The problem is the detection of eyes in the wrong place. If anyone knows the answer please help
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
while True:
ret, frame = cap.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), 5)
roi_gray = gray[y:y + w, x:x + w]
roi_color = frame[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex, ey, ew, eh) in eyes:
# center_coordinates = ex + ew // 2, ey + eh // 2
radius = eh // 2
cv2.circle(roi_color, (ex, ey), radius, (0, 0, 255), 5)
#cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
When you draw a rectangle with cv2.rectangle you need top-left and bottom-right corners but when you draw a circle with cv2.circle you need the centre point. You are using cv2.circle with the same coordinates as with the initial rectangles, you need to transform them to the centre before. Add this and it should work
for (ex,ey,ew,eh) in eyes:
radius = eh//2
eye_x = int(ex+0.5*ew)
eye_y = int(ey+0.5*ey)
cv2.circle(roi_color, (eye_x, eye_y), radius, (0, 0, 255), 5)
I'm currently trying to run a simple python program to detect face and eyes.
I downloaded the appropriate classifiers and they're in the same directory. I even checked my camera settings and it's set to on with permission to python. Any ideas how to solve this?
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
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.rectange(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,ehh) in eyes:
cv2.rectangle(roi_color, (ex,ey), (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()
Why does this OpenCV program give the error:
My camera settings show that python is using it:
Thank you!
!_src.empty() means you have an empty frame
You can check the frame to make sure you actually get the image:
ret, img = cap.read()
if img is not None:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else: < implement how you want to deal with empty frame >
The error I get while debugging on IDLE python (3.7) is:
cv2.error: OpenCV(3.4.3) (some directory files)
- error : (-215:Assertion failed) !_src.empty() in function 'cvtColor'
The program itself is taken from a website:
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('C:\Program Files\Python38\Lib\site-packages\cv2\data\haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces:
print(x,y,w,h)
roi_gray = gray[y:y + h, x:x + w] # (ycord_start, ycord_end)
roi_color = frame[y:y + h, x:x + w]
img_item = 'my-image.png'
cv2.imwrite(img_item,roi_gray)
cv2.imshow('frame',frame)
color = (0,0,255)
stroke = 2
width = x + w
height = y + h
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
So I would like to know what is causing this error.
The problem is in
cap = cv2.VideoCapture(0)
In fact 0 is the id of the opened video capturing device (i.e. a camera index). I imagine that the program can't detect any camera and thus it throws this error. You can check if video capturing has been initialized already by printing cap.isOpened() befor the loop, if it is false, then you have a problem in initializing the video capturing.
Regards
How can I save only the eye image after detection with opencv and Python?
This is the code that I have tried:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\\Users\MJ-INFO\Anaconda3\pkgs\libopencv-3.4.1-h875b8b8_3\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\Users\MJ-INFO\Anaconda3\pkgs\libopencv-3.4.1-h875b8b8_3\Library\etc\haarcascades\haarcascade_eye.xml')
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.2, 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]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
crop_img = roi_color[ey: ey + eh, ex: ex + ew]
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
But it is not working?
If I get your question correctly, you want to save crop_img in the loop? So, simply add cv::imwrite(...) after defining crop_img. Since you can have more than one eye, you need to think of naming your files.