人脸识别-bug
本帖最后由 xy1770 于 2020-7-29 14:18 编辑"""
人脸识别
"""
import cv2
# 载入图像
img = cv2.imread(r'/Users/sz/Desktop/OpenCV/lyf.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 转换成灰度图
# 创建一个级联分类器
face_cascade = cv2.CascadeClassifier(r'/Users/sz/Desktop/OpenCV\haarcascade_frontalface_default.xml')
# 搜索图像的坐标(将 NumPy 数组中的数据与级联分类器的特征数据进行匹配,找到人脸)
faces = face_cascade.detectMultiScale(img_gray, scaleFactor = 1.05, minNeighbors = 5) # scaleFactor:该参数指定每次图像缩小的比例;minNeighbors:该参数指定每一个目标至少要被检测到多少次
# 在人脸位置画个框框标记一下
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
cv2.imshow('LYF', img)
/Users/sz/PycharmProjects/untitled/venv/bin/python /Users/sz/PycharmProjects/untitled/5.py
Traceback (most recent call last):
File "/Users/sz/PycharmProjects/untitled/5.py", line 15, in <module>
faces = face_cascade.detectMultiScale(img_gray, scaleFactor = 1.05, minNeighbors = 5) # scaleFactor:该参数指定每次图像缩小的比例;minNeighbors:该参数指定每一个目标至少要被检测到多少次
cv2.error: OpenCV(4.3.0) /Users/travis/build/skvark/opencv-python/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
Process finished with exit code 1
代码是根据小甲鱼的帖子改的:
https://fishc.com.cn/thread-152145-1-1.html
已解决
1、文件路径斜杠方向错了
2、后面还需要加上:
cv2.waitKey(0)
cv2.destroyAllWindows() 我这里一切正常,也许是那个 XML 文件的问题吧,运行
print(face_cascade.detectMultiScale(img_gray, scaleFactor = 1.05, minNeighbors = 5))看它有没有返回正确的值
页:
[1]