python - 人脸检测 - 打开 CV 找不到人脸
问题描述
我正在学习 OpenCV。这是我的代码:
import cv2
face_patterns = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sample_image = cv2.imread('1.jpg')
gray = cv2.cvtColor(sample_image,cv2.COLOR_RGB2GRAY)
faces = face_patterns.detectMultiScale(gray,1.3,5)
print(len(faces))
for (x, y, w, h) in faces:
cv2.rectangle(sample_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite('result.jpg', sample_image)
如果我使用图片 A,我可以得到很多面孔,如果我使用图片 B,我没有得到。
我多次更改论点detectMultiScale(gray,1.3,5)
,它仍然不起作用。
解决方案
我认为这更像是 Cv2 模块本身的问题。有比 HAAR CASCADE 更好的模型来检测人脸。face_recognition 库对于检测和识别人脸也非常有用。它使用hog作为默认模型。您也可以使用cnn以获得更好的准确性,但检测过程会很慢。
在这里找到更多。
import cv2
import face_recognition as fr
sample_image = fr.load_image_file("1.jpg")
unknown_face_loc = fr.face_locations(sample_image, model="hog")
print(len(unknown_face_loc)) #detected face count
for faceloc in unknown_face_loc:
y1, x2, y2, x1 = faceloc
cv2.rectangle(sample_image, (x1, y1), (x2, y2), (0, 0, 255), 2)
sample_image = sample_image[:, :, ::-1] #converting bgr image to rbg
cv2.imwrite("result.jpg", sample_image)
推荐阅读
- amazon-dynamodb - Dynamoose:提供的关键元素与架构不匹配
- selenium - 如何在不使用 browser.pause 的情况下使 E2E 稳定?
- javascript - React JS:未捕获的 TypeError:getState 不是函数
- javascript - 开玩笑期望对象属性值不是对象
- flutter - 扩展和灵活的工作在一行中不太好
- prolog - 如何在 Prolog 中使用临时变量创建交换函数?
- r - 使用 is.na() 函数检查数据框中的缺失值
- docker - Azure DevOps 服务中的 Docker 服务连接
- bluetooth-lowenergy - 如何通过gatttool从Huawei Watch GT-DEE读取BMP数据包?
- javascript - 如何防止按钮在 Bootstrap4 中重叠?