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

时间:2019-05-27 09:00:22

标签: python-3.x 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检测器时,单位显示为:

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时,其外观如下:

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

代码正确运行。

1 个答案:

答案 0 :(得分:1)

尝试执行

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