Exemplo n.º 1
0
def test(inoutDir, outputDir, model):  # 原模型的P,R,net + 自行训练后的Onet,展示并保存检测后的图片
    pnet, rnet, onet_jiang = create_mtcnn_net(
        p_model_path="./original_model/pnet_epoch.pt",
        r_model_path="./original_model/rnet_epoch.pt",
        o_model_path="./original_model/" + model + ".pt",
        use_cuda=False)
    mtcnn_detector = MtcnnDetector(pnet=pnet,
                                   rnet=rnet,
                                   onet=onet_jiang,
                                   min_face_size=24)
    files = os.listdir(inoutDir)
    i = 0
    for image in files:
        i += 1
        image = os.path.join("./lfpw_test/", image)

        img = cv2.imread(image)
        img_bg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        bboxs, landmarks1 = mtcnn_detector.detect_face(img)  # 原始的图片用原始网络检测

        vis_face(img_bg, bboxs, landmarks1,
                 outputDir + model + "-" + str(i) + ".jpg")  # 保存图片
Exemplo n.º 2
0
import cv2
from mtcnn.core.detect import create_mtcnn_net, MtcnnDetector
from mtcnn.core.vision import vis_face

if __name__ == '__main__':
    pnet, rnet, onet = create_mtcnn_net(
        p_model_path="./original_model/pnet_epoch.pt",
        r_model_path="./original_model/rnet_epoch.pt",
        o_model_path="./original_model/onet_epoch.pt",
        use_cuda=False)
    mtcnn_detector = MtcnnDetector(pnet=pnet,
                                   rnet=rnet,
                                   onet=onet,
                                   min_face_size=24)

    img = cv2.imread("./s_l.jpg")
    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 box_align
    save_name = 'r_4.jpg'
    vis_face(img_bg, bboxs, landmarks, save_name)
Exemplo n.º 3
0
    def test_Onet_without_PRnet(self, annotation, outputDir, test_moudel, xxyy,
                                savePic):
        imagedb = ImageDB(annotation)
        gt_imdb = imagedb.load_imdb()
        pnet, rnet, onet_jiang = create_mtcnn_net(
            p_model_path="./original_model/pnet_epoch.pt",
            r_model_path="./original_model/rnet_epoch.pt",
            o_model_path="./original_model/" + test_moudel + ".pt",
            use_cuda=False)
        mtcnn_detector = MtcnnDetector(pnet=pnet,
                                       rnet=rnet,
                                       onet=onet_jiang,
                                       min_face_size=24)

        test_data = TrainImageReader(gt_imdb,
                                     48,
                                     batch_size=100,
                                     shuffle=False)  # 读入1个batch的数据
        # train_data.reset()
        total_errors = 0

        cnt = 0
        for i, (images, (gt_labels, gt_bboxes,
                         gt_landmarks)) in enumerate(test_data):  # 取1个batch
            list_imgs = [images[i, :, :, :]
                         for i in range(images.shape[0])]  # 100张图片

            list_bboxes = [gt_bboxes[i, :] for i in range(gt_bboxes.shape[0])]
            list_gt_landmarks = [
                gt_landmarks[i, :] for i in range(gt_landmarks.shape[0])
            ]
            mix = list(zip(list_imgs, list_bboxes, list_gt_landmarks))
            batch_errors = []

            for img, gt_bbox, gt_landmark in mix:  # 取1个图片
                if xxyy:
                    bboxs, landmarks = mtcnn_detector.detect_onet_xxyy(
                        img, gt_bbox)  # 原始的图片用原始网络检测,xxyy
                else:
                    bboxs, landmarks = mtcnn_detector.detect_onet(
                        img, gt_bbox)  # 原始的图片用原始网络检测,xxyy

                if landmarks.size:
                    cnt += 1
                    bboxs = bboxs[:1]  # 多个检测框保留第一个
                    landmarks = landmarks[:1]
                    if savePic:
                        vis_face(img, bboxs, landmarks,
                                 self.output_dir + str(cnt) + ".jpg")  # 保存图片
                    gt_landmark = np.array(gt_landmark).reshape(5, 2)
                    landmarks = np.array(landmarks).reshape(5, 2)

                    normDist = np.linalg.norm(gt_landmark[1] -
                                              gt_landmark[0])  # 左右眼距离
                    error = np.mean(
                        np.sqrt(np.sum(
                            (landmarks - gt_landmark)**2, axis=1))) / normDist

                    batch_errors.append(error)

            batch_errors = np.array(batch_errors).sum()
            total_errors += batch_errors
            print("%s:   %s pics mean error is %s" %
                  (datetime.datetime.now(), cnt, total_errors / cnt))
            if cnt > 999:
                print("%s:%s pics mean error is %s" %
                      (datetime.datetime.now(), cnt, total_errors / cnt))
                f = open("landmark_test.txt", "a+")
                f.write("%s, moudel_name:%s.pt, %s pics mean error is %s\n" %
                        (datetime.datetime.now(), test_moudel, cnt,
                         np.array(total_errors).reshape(1, -1).sum() / cnt))
                f.close()
                return

        print("%s:%s pics mean error is %s" %
              (datetime.datetime.now(), cnt, total_errors / cnt))
Exemplo n.º 4
0
import cv2
from mtcnn.core.detect import create_mtcnn_net, MtcnnDetector
from mtcnn.core.vision import vis_face

import warnings
warnings.filterwarnings("ignore")

if __name__ == '__main__':
    use_cuda = True
    pnet, rnet, onet = create_mtcnn_net(
        p_model_path="./original_model/pnet_epoch.pt",
        r_model_path="./original_model/rnet_epoch.pt",
        o_model_path="./original_model/onet_epoch.pt",
        use_cuda=use_cuda)
    mtcnn_detector = MtcnnDetector(pnet=pnet,
                                   rnet=rnet,
                                   onet=onet,
                                   min_face_size=24)

    img = cv2.imread("./img/part2_002268.jpg")
    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 box_align
    save_file = './img/result.jpg'
    bboxs = mtcnn_detector.box_expand(bboxs, 0.3, 0.25)
    vis_face(img_bg, bboxs, landmarks, save_file)