Example #1
0
def t_net(prefix,
          epoch,
          batch_size,
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False,
          im_dir=None,
          filename='imglist_with_gesture.txt'):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)
                  ]  # path model include epoch number
    print(model_path[0])  # load the first model only
    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["RNet", "ONet"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    basedir = im_dir
    #anno_file
    filename = 'imglist_with_gesture.txt'
    #read anotation(type:dict), include 'images' and 'bboxes'
    #data = read_annotation(basedir,filename)
    data = load_annotation(os.path.join(
        basedir, filename))  # modified version of load annotation
    #print('data:',data)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    #print("==================================")
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    print('load test data')
    test_data = TestLoader(data['images'])
    print('finish loading')
    #list
    print('start detecting....')
    detections, _ = mtcnn_detector.detect_face(test_data)
    print('detections:', detections)
    print('finish detecting ')
    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = data_dir
    print('save_path is :')
    print(save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    save_file = os.path.join(save_path, "detections.pkl")
    with open(save_file, 'wb') as f:
        pickle.dump(detections, f, 1)
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data, save_path)
Example #2
0
    '5SingleNine', '6SingleBad', '7SingleGood'
]

img_rows, img_cols = 28, 28

model = load_classification('CNN_Model/kcnn_aug')

stride = 2
slide_window = False
shuffle = False

detectors = [None, None, None]

PNet = FcnDetector(P_Net, model_path[0])
detectors[0] = PNet
RNet = Detector(R_Net, 24, 1, model_path[1])
detectors[1] = RNet
ONet = Detector(O_Net, 48, 1, model_path[2])
detectors[2] = ONet

mtcnn_detector = MtcnnDetector(detectors=detectors,
                               min_face_size=min_face_size,
                               stride=stride,
                               threshold=thresh,
                               slide_window=slide_window)

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
MAX_X = 320
MAX_Y = 240
Example #3
0
    def build_camera(self, camera_id, path):
        count = 10000
        thresh = [0.9, 0.9, 0.8]
        min_face_size = 100
        stride = 2
        slide_window = False
        detectors = [None, None, None]
        path_base = 'E:/sign_system/execute_system/haar_extract'
        paths = [
            os.path.join(path_base, 'pnet.pb'),
            os.path.join(path_base, 'rnet.pb'),
            os.path.join(path_base, 'onet.pb')
        ]
        PNet = FcnDetector(paths[0])
        detectors[0] = PNet
        RNet = Detector(24, 1, paths[1])
        detectors[1] = RNet
        ONet = Detector(48, 1, paths[2])
        detectors[2] = ONet
        mtcnn_detector = MtcnnDetector(detectors=detectors,
                                       min_face_size=min_face_size,
                                       stride=stride,
                                       threshold=thresh,
                                       slide_window=slide_window)
        cap = cv2.VideoCapture(camera_id)
        num = 0
        cur = self.read_feature(path)
        while True:
            success, frame = cap.read()
            thickness = (frame.shape[0] + frame.shape[1]) // 350
            print(frame.shape)
            if success:
                t1 = time.time()
                img = np.array(frame)
                boxes_c, landmarks = mtcnn_detector.detect(img)
                #print(boxes_c)
                for i in range(boxes_c.shape[0]):
                    bbox = boxes_c[i, :4]
                    #score = boxes_c[i, 4]
                    cropbbox = [
                        int(bbox[0]),
                        int(bbox[1]),
                        int(bbox[2]),
                        int(bbox[3])
                    ]
                    W = -int(cropbbox[0]) + int(cropbbox[2])
                    H = -int(cropbbox[1]) + int(cropbbox[3])
                    paddingH = 0.02 * H
                    paddingW = 0.01 * W
                    crop_img = frame[int(cropbbox[1] +
                                         paddingH):int(cropbbox[3] - paddingH),
                                     int(cropbbox[0] -
                                         paddingW):int(cropbbox[2] + paddingW)]

                    if crop_img is None:
                        continue

                    if crop_img.shape[0] < 0 or crop_img.shape[1] < 0:
                        continue

                    image = cv2.resize(crop_img, (96, 96),
                                       interpolation=cv2.INTER_CUBIC)
                    image = image.reshape((1, 96, 96, 3))

                    image = image.astype('float32')
                    image = image - 127.5
                    image = image * 0.0078125
                    f1 = self.get_feature(image)

                    f1 = f1.reshape(256)

                    #计算距离
                    d1 = 0
                    show_name = ''
                    for n, v in cur.items():
                        v = np.array(v)
                        d = np.dot(
                            v, f1) / (np.linalg.norm(v) * np.linalg.norm(f1))
                        #print(d)
                        if d > d1:
                            d1 = d
                            show_name = str(n)
                        else:
                            pass
                    #print(show_name)
                    t2 = time.time()
                    delta_t = t2 - t1
                    text_start = (max(int(cropbbox[0]),
                                      10), max(int(cropbbox[1]), 10))
                    cv2.putText(frame, show_name, text_start,
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 1)
                    cv2.putText(frame, "FPS:" + '%.04f' % (1 / delta_t),
                                (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 1,
                                (255, 0, 255), 2)
                    # rectangle for face area
                    for i in range(thickness):
                        start = (int(cropbbox[0]) + i, int(cropbbox[1]) + i)
                        end = (int(cropbbox[2] - i), int(cropbbox[3]) - i)
                        frame = cv2.rectangle(frame, start, end, (0, 255, 0),
                                              1)

                    # display the landmarks
                    for i in range(landmarks.shape[0]):
                        for j in range(len(landmarks[i]) // 2):
                            cv2.circle(frame,
                                       (int(landmarks[i][2 * j]),
                                        int(int(landmarks[i][2 * j + 1]))), 2,
                                       (0, 0, 255))
                    num = num + 1
                cv2.imshow("Camera", frame)
                k = cv2.waitKey(10)
                if (k & 0xFF == ord('q') or count == num):
                    break
            else:
                print('device not find')
                break
        cap.release()
        cv2.destroyAllWindows()
Example #4
0
def main(test_mode="ONet"):

    # Configuration mode
    print("===============================")
    print("> Test mode is {}".format(test_mode))
    print("===============================")

    # Initialise
    slide_window = False
    detectors = [None, None, None]
    thresh = [0.6, 0.7, 0.7]
    IoU_upper_threshold = 0.65
    IoU_lower_threshold = 0.3
    min_face_size = 20
    stride = 2
    batch_size = [2048, 64, 16]

    # The model path, should be the same in the checkpoint file
    model_path = [
        'Model/PNet/PNet-30', 'Model/RNet/RNet-500', 'Model/ONet/ONet-116'
    ]
    # Test image path
    path = 'Dataset/Training'

    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["RNet", "ONet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    # Initialise detcector
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    # Get the test img list
    img_list, gt_list = get_lists(path)
    print('img number: ', len(img_list), len(gt_list))

    test_data = TestLoader(img_list)

    # Get boxes and landmarks
    all_boxes, landmarks = mtcnn_detector.detect_face(test_data)

    FP_set = []  # False Positive (False Alarm - IoU < IoU_lower_threshold);
    FN_img = [
    ]  # False Negative (Detection Failure - if there is no bbox whose IoU > IoU_upper_threshold);
    TP_set = [
    ]  # True  Positive (Correct Detection - IoU > IoU_upper_threshold);
    NM_set = [
    ]  # Normal bounding boxes (IoU_lower_threshold < IoU < IoU_upper_threshold);
    FPR_set = []  # False Positive Rate (FP / total boxes);
    TPR_set = []  # False Positive Rate (TP / total boxes);
    score_set = []  # Scores (sum(score*IoU) / sum(score)).

    for count, gt in enumerate(gt_list):
        TP_img = []  # True  Positive bbox for each img;
        FP_img = []  # False Positive bbox for each img;
        NM_img = []  # Normal bbox for each img.

        # Evaluate scores per picture
        score = evaluate(all_boxes[count], gt)

        # Draw boxes
        for box_number, bbox in enumerate(all_boxes[count]):
            # Evaluate box
            iou = IoU(bbox[:-1], gt)
            # print(box_number, iou)
            if iou > IoU_upper_threshold:
                TP_img.append(bbox)
            elif iou < IoU_lower_threshold:
                FP_img.append(bbox)
            else:
                NM_img.append(bbox)

            # Skip the landmarks if it is PNet mode
            if test_mode == "PNet":
                continue

        if len(all_boxes[count]) != 0:
            FPR = len(FP_img) / len(all_boxes[count])
            FPR_set.append(FPR)
            TPR = len(TP_img) / len(all_boxes[count])
            TPR_set.append(TPR)
        TP_set.append(TP_img)
        FP_set.append(FP_img)
        NM_set.append(NM_img)
        score_set.append(score)
        if not TP_img:
            FN_img.append(count)

    # print result
    print('=================== Result =====================\n')

    print(" Testing samples: {};\n Testing Model: {};\n Testing Net: {}.\n".
          format(len(img_list), model_path, test_mode))

    print("1. Max FPR: {}, Min FPR: {}, Average FPR: {}".format(
        round(max(FPR_set), 4), round(min(FPR_set), 4),
        round(sum(FPR_set) / len(FPR_set), 4)))
    print(
        "   FPR(False Positive Rate) refers to the ratio of # mistakenly marked\n   bounding boxes and # total boxes for each image.\n   IoU < {}\n"
        .format(IoU_lower_threshold))

    print("2. Max TPR: {}, Min TPR: {}, Average TPR: {}".format(
        round(max(TPR_set), 4), round(min(TPR_set), 4),
        round(sum(TPR_set) / len(TPR_set), 4)))
    print(
        "   TPR(True Positive Rate) refers to the ratio of # successfully marked\n   bounding boxes and # total boxes for each image.\n   IoU > {}\n"
        .format(IoU_upper_threshold))

    print("3. Max score: {}, Min score: {}, Average score: {}".format(
        round(max(score_set), 4), round(min(score_set), 4),
        round(sum(score_set) / len(score_set), 4)))
    print(
        "   Scores are calculated following sum(score*IoU)/sum(score) for each image.\n"
    )

    # print(FN_img)
    print("4. False Negative Rate: {}".format(
        round(len(FN_img) / len(img_list), 4)))
    print(
        "   False Negative Rate refers to the ratio of # images which never have\n   positive boxes and # total images for the whole testing set.\n"
    )

    print(
        "Images whose indices are in the following list do not have positive bounding boxes:"
    )
    print(FN_img)
Example #5
0
def demo(test_mode="ONet"):

    # Configuration mode
    print("===============================")
    print("> Test mode is {}".format(test_mode))
    print("===============================")

    # Initialise
    slide_window = False
    detectors = [None, None, None]
    thresh = [0.6, 0.7, 0.7]
    min_face_size = 20
    stride = 2
    batch_size = [2048, 64, 16]

    # The model path, should be the same in the checkpoint file
    model_path = [
        'Model/PNet/PNet-30', 'Model/RNet/RNet-500', 'Model/ONet/ONet-116'
    ]
    # The sub-folder in the folder Testing_Demo_Data
    TestImage_subfolder = "test"
    # Test image postfix
    Image_postfix = 'jpg'

    TestImage_path = "Testing_Demo_Data/{}/".format(TestImage_subfolder)
    TestResult_path = "MTCNN_demo/{}/ResultImage/{}/".format(
        test_mode, TestImage_subfolder)
    mkdir(TestResult_path)
    if test_mode in ["RNet", "ONet"]:
        mkdir(TestResult_path + 'prediction/')

    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["RNet", "ONet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    # Initialise detcector
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    gt_imdb = []

    # Get the test img list
    _, img_list = scan_file(TestImage_path, Image_postfix)
    for item in img_list:
        gt_imdb.append(os.path.join(TestImage_path, item))
    test_data = TestLoader(gt_imdb)

    # Get boxes and landmarks
    all_boxes, landmarks = mtcnn_detector.detect_face(test_data)

    count = 0

    for image_path in gt_imdb:
        print(image_path)
        image = cv2.imread(image_path)
        image_original = image.copy()
        for box_number, bbox in enumerate(all_boxes[count]):
            # Process img with all boxes
            cv2.putText(image,
                        str(np.round(bbox[4], 2)),
                        (int(bbox[0]), int(bbox[1])),
                        cv2.FONT_HERSHEY_TRIPLEX,
                        1,
                        color=(255, 0, 255))
            cv2.rectangle(image, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (0, 0, 255))

            # Draw Single Box img
            image_single = image_original.copy()
            cv2.rectangle(image_single, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (0, 0, 255))

            # Skip the landmarks if it is PNet mode
            if test_mode == "PNet":
                continue

            class_list = np.array(landmarks[count][box_number])
            pred_index = np.argmax(class_list)
            pred_text = str(pred_index)
            if pred_index == 0:
                pred_text = "One"
            elif pred_index == 1:
                pred_text = 'Fist'
            elif pred_index == 2:
                pred_text = 'Two'
            else:
                # if failed to classify, a txt file will be generated
                with open(
                        "{}/prediction/{}_{}.txt".format(
                            TestResult_path, count, box_number), 'w') as f:
                    f.write('prediction list:{}'.format(class_list))
                print('file saved!')

            # Save img with single boxes in sub-folder prediction
            cv2.putText(image_single,
                        pred_text, (120, 120),
                        cv2.FONT_HERSHEY_TRIPLEX,
                        1,
                        color=(0, 255, 0))

            cv2.imwrite(
                "{}/prediction/{}_{}.png".format(TestResult_path, count,
                                                 box_number), image_single)

        count = count + 1

        # Save img with all boxes.
        cv2.imwrite("{}/{}.png".format(TestResult_path, count - 1), image)