函数 'detectMultiScale' 中的 !empty()
!empty() in function 'detectMultiScale'
我 运行 我的代码在 Pycharm Community Edition 2020.1.1 x64 中,我将我的 haar_face.xml 和这个 py 文件添加到同一文件夹中。我还尝试 google 并添加一些东西,例如 cv.data.haarcascade+ 或复制 xml 文件的完整路径,但它仍然显示以下错误。
代码
import cv2 as cv
from cv2 import imshow
import numpy as np
img = cv.imread('lily.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray People', gray)
haar_cascade= cv.CascadeClassifier(cv.data.haarcascades+r'C:\Users\mycomp\Desktop\CODE\OPENCV\haar_face.xml')
print(haar_cascade.empty()) #True
faces_rect = haar_cascade.detectMultiScale(img)#gray, scaleFactor=1.1, minNeighbors=1)
print(f'Number of faces found = {len(faces_rect)}')
for (x,y,w,h) in faces_rect:
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)
cv.imshow('Detected Faces', img)
cv.waitKey(0)
结果
Traceback (most recent call last):
File "C:/Users/mycomp/Desktop/CODE/OPENCV/face_detection.py", line 12, in <module>
faces_rect = haar_cascade.detectMultiScale(img)#gray, scaleFactor=1.1, minNeighbors=1)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-duilijvh\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
这一行路径错误:
haar_cascade= cv.CascadeClassifier(
cv.data.haarcascades+r'C:\Users\mycomp\Desktop\CODE\OPENCV\haar_face.xml')
将其替换为:
haar_cascade=cv.CascadeClassifier(
cv.haarcascades+"haarcascade_frontalface_default.xml")
我 运行 我的代码在 Pycharm Community Edition 2020.1.1 x64 中,我将我的 haar_face.xml 和这个 py 文件添加到同一文件夹中。我还尝试 google 并添加一些东西,例如 cv.data.haarcascade+ 或复制 xml 文件的完整路径,但它仍然显示以下错误。
代码
import cv2 as cv
from cv2 import imshow
import numpy as np
img = cv.imread('lily.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray People', gray)
haar_cascade= cv.CascadeClassifier(cv.data.haarcascades+r'C:\Users\mycomp\Desktop\CODE\OPENCV\haar_face.xml')
print(haar_cascade.empty()) #True
faces_rect = haar_cascade.detectMultiScale(img)#gray, scaleFactor=1.1, minNeighbors=1)
print(f'Number of faces found = {len(faces_rect)}')
for (x,y,w,h) in faces_rect:
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)
cv.imshow('Detected Faces', img)
cv.waitKey(0)
结果
Traceback (most recent call last):
File "C:/Users/mycomp/Desktop/CODE/OPENCV/face_detection.py", line 12, in <module>
faces_rect = haar_cascade.detectMultiScale(img)#gray, scaleFactor=1.1, minNeighbors=1)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-duilijvh\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
这一行路径错误:
haar_cascade= cv.CascadeClassifier(
cv.data.haarcascades+r'C:\Users\mycomp\Desktop\CODE\OPENCV\haar_face.xml')
将其替换为:
haar_cascade=cv.CascadeClassifier(
cv.haarcascades+"haarcascade_frontalface_default.xml")