如何加速dlib人脸检测

2024-09-29 00:17:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用dlib检测狗的面部识别

但得到结果大约需要10秒

我试着缩小尺寸,但效果不好

我使用的数据是landmarker.dat和dogheader.dat

能给我一些建议让我更快吗


detector = dlib.cnn_face_detection_model_v1('dogheader.dat')
predictor = dlib.shape_predictor('landmarkDetector.dat')

def point(img_path):
    # filename, ext = os.path.splitext(os.path.basename(img_path))
    print('hello')
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    print(img.shape)
    height = 300
    if (img.shape[1] > height):
        width = int(height * img.shape[1] / img.shape[0])
        print(height, width)
        img = cv2.resize(img, dsize=(width, height), interpolation=cv2.INTER_LINEAR)

    print(img.shape)
    print('hello')
    dets = detector(img, upsample_num_times=1)
    shape = []
    for i, d in enumerate(dets):
        shape = predictor(img, d.rect)
        shape = face_utils.shape_to_np(shape)
    '''
        for i, d in enumerate(dets):
            shape = predictor(img, d.rect)
            shape = face_utils.shape_to_np(shape)

            for i, p in enumerate(shape):
                shapes.append(shape)
                cv2.circle(img_result, center=tuple(p), radius=3, color=(0, 0, 255), thickness=-1, lineType=cv2.LINE_AA)
                cv2.putText(img_result, str(i), tuple(p), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)

        img_out = cv2.cvtColor(img_result, cv2.COLOR_RGB2BGR)
        cv2.imwrite('img/%s_out%s' % (img_path, img_out))
        '''
    print(shape)
    return (shape)


def ratio(shape):
    eye2eye = np.sqrt(float(shape[2][0] - shape[5][0]) ** 2 + float(shape[2][0] - shape[5][0]) ** 2)
    nose = np.sqrt((float(shape[2][0] - shape[5][0]) / 2 - shape[3][0]) ** 2 + (
                float(shape[2][1] - shape[5][1]) / 2 - shape[3][1]) ** 2)
    eye2nose = int(eye2eye / nose * 1000)

    ear2ear = np.sqrt(float(shape[1][0] - shape[4][0]) ** 2 + float(shape[1][0] - shape[4][0]) ** 2)
    eye2ear = int(eye2eye / ear2ear * 1000)
    print("dd", eye2nose, eye2ear)
    return eye2nose, eye2ear

shape = point(img)
a,b = ratio(shape)

谢谢:)


Tags: pathimgnpfloatwidthcv2predictordat