MTCNN:人臉檢測

github下載:DFace

代碼中錯誤修改:

  • 1.dface\core\models.py文件
    其中PNet、RNet、ONet的 x = self.pre_layer(x),改爲 x = self.pre_layer(x.float())
  • 2.dface\core\image_tools.py文件
    第20行的return transform(image),改爲return transform(image)/255
  • 3.警告
    版本不同的警告

運行代碼:

import cv2
from dface.core.detect import create_mtcnn_net, MtcnnDetector
import dface.core.vision as vision


def display(im_array, dets, landmarks=None):
    im_array = cv2.cvtColor(im_array, cv2.COLOR_RGB2BGR)
    for i in range(dets.shape[0]):
        bbox = dets[i,:4]
        cv2.rectangle(im_array, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255, 0, 0),2)
    
    if landmarks is not None:
        for i in range(landmarks.shape[0]):
            landmarks_one = landmarks[i, :]
            landmarks_one = landmarks_one.reshape((5, 2))         
            for j in range(5):
                cv2.circle(im_array, (int(landmarks_one[j][0]),int(landmarks_one[j][1])), 2, (0,255,0), 2)
    cv2.imshow('demo',im_array)
    cv2.imwrite('demo.jpg',im_array)
    cv2.waitKey(0)

if __name__ == '__main__':

    pnet, rnet, onet = create_mtcnn_net(p_model_path="./model_store/pnet_epoch.pt", r_model_path="./model_store/rnet_epoch.pt", o_model_path="./model_store/onet_epoch.pt", use_cuda=False)
    mtcnn_detector = MtcnnDetector(pnet=pnet, rnet=rnet, onet=onet, min_face_size=44)

    img = cv2.imread("./000016.jpg")
    w,h,c = img.shape
    max_wh = max(w,h)
    if max_wh >3000:
        scale = max_wh/1920
        img = cv2.resize(img,(int(w/scale), int(h/scale)))
    img_bg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    #b, g, r = cv2.split(img)
    #img2 = cv2.merge([r, g, b])

    bboxs, landmarks = mtcnn_detector.detect_face(img)
    print(bboxs)
    print(landmarks)

    display(img_bg,bboxs,landmarks)

輸出效果:

在這裏插入圖片描述
注: 關鍵點的準確性

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章