python-3.xdlib

How to convert mmod_rectangles to rectangles via Dlib?


In this code used detectors of 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

When used the cnn detector, dets look as:

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

And an exception is thrown in the predictor line:

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>

But when used get_frontal_face_detector, the dets look as:

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

And the code works correctly.


Solution

  • try to perform

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