Exemple #1
0
        "./results/rnet/log_bs512_lr0.001_072502/check_point/model_050.pth",
        o_model_path=
        "./results/onet/log_bs512_lr0.001_072602/check_point/model_050.pth",
        min_face_size=24,
        use_cuda=True)  # 加载模型参数,构造检测器
    logger.info("Init the MtcnnDetector.")
    project_root = pathlib.Path()
    inputPath = project_root / "data" / "test_images"
    outputPath = project_root / "data" / "you_result"
    outputPath.mkdir(exist_ok=True)

    with torch.no_grad():
        start = time.time()
        for num, input_img_filename in enumerate(inputPath.iterdir()):
            logger.info("Start to predict No.{} image.".format(num))
            img_name = input_img_filename.name
            logger.info("The name of the image is {}.".format(img_name))

            img = cv2.imread(str(input_img_filename))
            RGB_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            bboxs, landmarks = mtcnn_detector.detect_face(
                RGB_image)  # 检测得到bboxs以及特征点
            img = draw_images(img, bboxs, landmarks)  # 得到绘制人脸框及特征点的图片
            savePath = outputPath / img_name  # 图片保存路径
            logger.info("Predict complete. Save image to {}.".format(
                str(savePath)))

            cv2.imwrite(str(savePath), img)  # 保存图片

        logger.info("Finish all the images.")
        logger.info("Cost time: {:.3f}s".format(time.time() - start))
    outputPath = project_root / "data" / "test_new_energy/results"
    outputPath.mkdir(exist_ok=True)

    start = time.time()
    corr = 0
    count = 0
    for num, input_img_filename in enumerate(inputPath.iterdir()):
        logger.info("Start to process No.{} image.".format(num))
        img_name = input_img_filename.name
        logger.info("The name of the image is {}.".format(img_name))

        img = cv2.imread(str(input_img_filename))
        if img is None:
            continue
        RGB_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        bboxs, landmarks, attrs, pboxes, rboxes = mtcnn_detector.detect_face(
            RGB_image)  # 检测得到bboxs以及特征点
        if len(bboxs) > 0:
            corr += 1
        img = draw_images(img, pboxes, [], [], (255, 0, 0))
        img = draw_images(img, rboxes, [], [], (0, 255, 255))
        img = draw_images(img, bboxs, landmarks, attrs)  # 得到绘制人脸框及特征点的图片
        savePath = outputPath / img_name  # 图片保存路径
        logger.info("Process complete. Save image to {}.".format(
            str(savePath)))

        cv2.imwrite(str(savePath), img)  # 保存图片
        count += 1

    print(float(corr) / count)

    logger.info("Finish all the images.")
        use_cuda=False)
    # logger.info("Init the MtcnnDetector.")
    imdb = get_imdb_fddb(data_dir)
    nfold = len(imdb)

    for i in range(nfold):
        image_names = imdb[i]
        dets_file_name = os.path.join(out_dir,
                                      'FDDB-det-fold-%02d.txt' % (i + 1))
        fid = open(dets_file_name, 'w')
        # image_names_abs = [os.path.join(data_dir, 'originalPics', image_name + '.jpg') for image_name in image_names]

        for idx, im_name in enumerate(image_names):
            img_path = os.path.join(data_dir, 'originalPics', im_name + '.jpg')
            img = cv2.imread(img_path)
            boxes, _ = MtcnnDetector.detect_face(img)

            if boxes is None:
                fid.write(im_name + '\n')
                fid.write(str(1) + '\n')
                fid.write('%f %f %f %f %f\n' % (0, 0, 0, 0, 0.99))
                continue

            fid.write(im_name + '\n')
            fid.write(str(len(boxes)) + '\n')

            for box in boxes:
                fid.write(
                    '%f %f %f %f %f\n' %
                    (float(box[0]), float(box[1]), float(box[2] - box[0] + 1),
                     float(box[3] - box[1] + 1), box[4]))