首页 > 解决方案 > 使用 Opencv 进行人脸检测

问题描述

我正在使用 haarcascade_fontalface_default.xml 文件进行人脸检测。这是我的代码

import cv2

#load some free trained data on face fontals from opencv(haarcascade)
trained_face_data=cv2.CascadeClassifier('haarcascade_fontalface_default.xml')

#choose an image to detect faces in
img = cv2.imread('ahk.jpg')

#covert color
#ashshak = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#Detect faces
face_coordinates = trained_face_data.detectMultiScale(img)
print(face_coordinates)

#name of the window where the image will show
cv2.imshow('Face detector opencv',img)

#To set the delay time
cv2.waitKey()
print("code completed")

但是当我尝试运行代码时,我一直面临这个错误。任何人都可以帮我找出解决方案吗

这是我的错误:

Traceback (most recent call last):
  File "E:/python/opencv/Facedetection.py", line 13, in <module>
    face_coordinates = trained_face_data.detectMultiScale(img)
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

标签: pythonopencvartificial-intelligenceface-detectioncascade-classifier

解决方案


推荐阅读