繁体   English   中英

使用python的人脸识别软件

[英]Facial recognition Software using python

我正在关注创建面部识别软件的教程(链接: 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)". 

查看我的代码(如下)和其他类似的在线问题,它说这是因为我没有指定我的文件路径,但我仍然得到同样的错误。 我对这种类型的软件还是新手,一般来说是 python,所以我不确定我哪里出错了。 任何帮助,将不胜感激!!!

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()

感谢帮助过的人!! 解决方案是将xml文件放入项目的工作目录中,而不是尝试输入整个文件路径

你的路径是错误的。 您不需要/C之前。

本该如此,

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

但我相信你在 Windows 上,我建议使用类似下面的东西,

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

在 Windows 中,当您提及任何文件时,您应该使用“\”而不是正斜杠“/”。 所以你的文件路径应该看起来像这样:

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

另外,我建议您将所有 XML 文件放在同一个工作目录(放置代码的位置)中,这样您就不必提及整个文件路径。 您可以只提及文件名。 当您尝试使用不同的 XML 文件并需要经常更改输入文件时,这会有所帮助。 希望这会有所帮助,干杯!

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