如何通过 Dlib 将 mmod_rectangles 转换为矩形?

How to convert mmod_rectangles to rectangles via Dlib?

在此代码中使用了 dlib 检测器。

dlib.get_frontal_face_detector()
dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('res/model.dat')
# detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')

cap = cv.VideoCapture(0)

while True:
    _, frame = cap.read(0)
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    dets = detector(gray, 0)
    print(dets)
    for det in dets:
        landmarks = shape_to_np(predictor(gray, det))

    cv.imshow('test', frame)

    if cv.waitKey(1) == ord('q'):
        break

使用 cnn 检测器时,dets 如下所示:

mmod_rectangles[[(258, 254) (422, 417)]]

并在预测行中抛出异常:

TypeError: __call__(): incompatible function arguments. The following
argument types are supported:
    1. (self: dlib.shape_predictor, image: array, box: dlib.rectangle) -> dlib.full_object_detection

Invoked with: <dlib.shape_predictor object at 0x7f37a12ba9d0>,
array([[71, 69, 70, ..., 71, 70, 73],
       [71, 72, 71, ..., 72, 72, 75],
       [71, 70, 71, ..., 72, 72, 73],
       ...,
       [27, 27, 27, ..., 75, 71, 68],
       [27, 27, 27, ..., 74, 71, 71],
       [24, 25, 27, ..., 73, 71, 70]], dtype=uint8), <dlib.mmod_rectangle object at 0x7f37819beea0>

但是当使用 get_frontal_face_detector 时,dets 看起来像:

rectangles[[(273, 234) (453, 413)]]

代码运行正常。

尝试执行

faceRect = det.rect
landmarks = shape_to_np(predictor(gray, faceRect))

可能是版本问题

faceRect = det[0].rect
landmarks = shape_to_np(predictor(gray, faceRect))