简体   繁体   English

使用python的人脸识别软件

[英]Facial recognition Software using python

I'm following a tutorial for creating a facial recognition software (link: https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ ) and I keep getting the error:我正在关注创建面部识别软件的教程(链接: https ://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/),但我不断收到错误消息:

OpenCV Error: Assertion failed (!empty()) in 
cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-
python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
Traceback (most recent call last):
  File "C:/Users/Jacob/PycharmProjects/DesignProject/main.py", line 17, in 
<module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)". 

Looking at my code (below) and other questions like this online, it says that it was because I didn't specify my file path, but i still get the same error.查看我的代码(如下)和其他类似的在线问题,它说这是因为我没有指定我的文件路径,但我仍然得到同样的错误。 I'm still new to this type of software, and python in general, so I'm not sure where I'm going wrong.我对这种类型的软件还是新手,一般来说是 python,所以我不确定我哪里出错了。 Any help would be appreciated!!!任何帮助,将不胜感激!!!

import cv2


face_cascade = cv2.CascadeClassifier('/C:/User/Jacob/Downloads/haarcascade_frontalface_default.xml')

eye_cascade = cv2.CascadeClassifier('/C:/User/Jacob/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()

Thank you to those that helped!!感谢帮助过的人!! the solution was to put the xml files into the working directory of the project instead of trying to type out the entire file path解决方案是将xml文件放入项目的工作目录中,而不是尝试输入整个文件路径

your path is wrong.你的路径是错误的。 You don't need / before C .您不需要/C之前。

This is how it should be,本该如此,

face_cascade = cv2.CascadeClassifier('C:/User/Jacob/Downloads/haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('C:/User/Jacob/Downloads/haarcascade_frontalface_default.xml')

But I believe you are on windows and I recommend using something like below,但我相信你在 Windows 上,我建议使用类似下面的东西,

face_cascade = cv2.CascadeClassifier(r'C:\User\Jacob\Downloads\haarcascade_frontalface_default.xml')

In windows when you are mentioning any file you should '\' instead of a forward slash'/'.在 Windows 中,当您提及任何文件时,您应该使用“\”而不是正斜杠“/”。 So your file path should look somewhat like this:所以你的文件路径应该看起来像这样:

C:\Users\XYZ\Desktop\project code\abc.xml C:\Users\XYZ\Desktop\项目代码\abc.xml

Also, I suggest that you put all your XML files in the same working directory(where your code is placed) so that you don't have to mention the entire file path.另外,我建议您将所有 XML 文件放在同一个工作目录(放置代码的位置)中,这样您就不必提及整个文件路径。 You can just mention the file name.您可以只提及文件名。 This helps when you are experimenting with different XML files and need to change the input files often.当您尝试使用不同的 XML 文件并需要经常更改输入文件时,这会有所帮助。 Hope this helps,cheers!希望这会有所帮助,干杯!

import cv2
import sys

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#it must be noted that original size of video must be written correctly because it     is not shown in video player
while(cap.isOpened()):
    ret,frame = cap.read()
    if ret == True:
        print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    # Detect the faces
        faces = face_cascade.detectMultiScale(gray, 1.1, 4)
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 34), 1,1)
        cv2.imshow("Test",frame)


    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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