简体   繁体   中英

Face Detection algorithm- Haar Cascade Classifier does not detect object

i decided to change completely , because there is different issue- namely there is some errors and in the previous question i missed this error, so i have following code

import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread("baby.jpg")
gray_img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
##gray_resized = cv2.resize(gray_img, (800, 900))
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
faces = face_cascade.detectMultiScale(gray_img ,1.3, 5)
for (x, y, w, h) in faces:
   img = cv2.rectangle(img1 , (x, y), (x + w, y + h), (255, 0, 0), 2)
   roi_gray = gray_img[y:y + h, x:x + w]
   roi_color = img1[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)
cv2.waitKey(0)
cv2.destroyAllWindows()

code was written on the based of following site :

Face Detection

and image which i used is following在此处输入图片说明

also i downloaded classification files from the following link Classification

but when i run code, i got following errors

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\core\src\persistence.cpp:722: error: (-49:Unknown error code -49) Input file is empty in function 'cv::FileStorage::Impl::open'

The above exception was the direct cause of the following exception:

T

raceback (most recent call last):
      File "C:/Users/Dato/Downloads/my_virtual_python/detecting_face.py", line 6, in <module>
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

please help me to figure out, what is the source of error?

I did try your code, your code worked but it did not give the correct answer, so i changed it a little bit, be sure that you have the two xml files in the same folder as your code file, and the image should be in the same folder too. So first make a folder, put your code file, 2 xml files, and image in it, then run the code i paste below. The link to the xml files ('haarcascade_frontalface_default.xml', 'haarcascade_eye.xml') is HERE .

import cv2

img = cv2.imread("baby.jpg", 0)

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

faces = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=10)
for (x, y, w, h) in faces:
   img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
#    roi_gray = img[y:y + h, x:x + w]
   eyes = eye_cascade.detectMultiScale(img)
   for (ex, ey, ew, eh) in eyes:
      img = cv2.rectangle(img, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

cv2.imshow("Final_detected_image", img)
cv2.waitKey(0)

My output result is as below, you may see it has detected the noses mistakenly, but it is what it is for haarcascades. I suggest you to use other methods for face detection, and be sure to tweak the values for "scaleFactor" and "minNeighbors", both for faces and eyes.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM