使用Dlib cnn模型进行人脸检测时报错

问题:使用Dlib cnn模型进行人脸检测时报错

在使用Dlib进行人脸识别时,使用基于HOG的dlib.get_frontal_face_detector()可以正常运行,但当使用基于cnn的dlib.cnn_face_detection_model_v1(‘’)时,会报TypeError: call(): incompatible function arguments. The following argument types are supported:
1. (self: _dlib_pybind11.shape_predictor, image: array, box: _dlib_pybind11.rectangle) -> _dlib_pybind11.full_object_detection的错误

原因:

传入格式不对,box格式应该是 rectangles,但错误输入mmod_rectangles的格式

解决方法:

转换格式,在参数后面加“.rect”
如:

predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
cnn_face_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')
face_detector = cnn_face_detector(img_camera, 1)
face_predictor = predictor(img_camera, face_detector[0].rect)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here is an example code that uses Dlib's CNN model for face detection and face recognition: ```python import dlib import cv2 def detect_faces(image): detector = dlib.cnn_face_detection_model_v1('path/to/dlib_model/mmod_human_face_detector.dat') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) return faces def recognize_faces(image, faces): shape_predictor = dlib.shape_predictor('path/to/dlib_model/shape_predictor_68_face_landmarks.dat') face_recognizer = dlib.face_recognition_model_v1('path/to/dlib_model/dlib_face_recognition_resnet_model_v1.dat') descriptors = [] for face in faces: shape = shape_predictor(image, face.rect) descriptor = face_recognizer.compute_face_descriptor(image, shape) descriptors.append(descriptor) return descriptors # Load the input image image = cv2.imread('path/to/input/image.jpg') # Perform face detection faces = detect_faces(image) # Perform face recognition descriptors = recognize_faces(image, faces) # Display the results for face in faces: cv2.rectangle(image, (face.rect.left(), face.rect.top()), (face.rect.right(), face.rect.bottom()), (0, 255, 0), 2) cv2.imshow("Face Detection", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` In this code, we first define the `detect_faces` function that uses the Dlib CNN face detection model (`mmod_human_face_detector.dat`) to detect faces in the input image. We then define the `recognize_faces` function that uses the Dlib shape predictor model (`shape_predictor_68_face_landmarks.dat`) and the Dlib face recognition model (`dlib_face_recognition_resnet_model_v1.dat`) to compute face descriptors for each detected face. Finally, we load the input image, perform face detection, perform face recognition, and display the results by drawing rectangles around the detected faces. Please make sure to replace `'path/to/dlib_model/'` with the actual path to the Dlib model files on your system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值