def gen_onet_data(data_dir,
                  anno_file,
                  pnet_model_file,
                  rnet_model_file,
                  prefix_path='',
                  use_cuda=True,
                  vis=False):
    mtcnn_detector = MtcnnDetector(p_model_path=pnet_model_file,
                                   r_model_path=rnet_model_file,
                                   o_model_path=None,
                                   min_face_size=12,
                                   use_cuda=True)

    imagedb = ImageDB(anno_file, mode="test", prefix_path=prefix_path)
    imdb = imagedb.load_imdb()
    image_reader = TestImageLoader(imdb, 1, False)

    all_boxes = list()
    batch_idx = 0

    for databatch in image_reader:
        if batch_idx % 100 == 0:
            print("%d images done" % batch_idx)
        im = databatch
        t = time.time()
        # detect an image by pnet and rnet
        p_boxes, p_boxes_align = mtcnn_detector.detect_pnet(im=im)
        boxes, boxes_align = mtcnn_detector.detect_rnet(im=im,
                                                        dets=p_boxes_align)
        if boxes_align is None:
            all_boxes.append(np.array([]))
            batch_idx += 1
            continue
        if vis:
            vision.vis_face(im, boxes_align)

        t1 = time.time() - t
        print('time cost for image ', batch_idx, '/', image_reader.size, ': ',
              t1)
        all_boxes.append(boxes_align)
        batch_idx += 1

    save_path = config.TRAIN_DATA_DIR
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    save_file = os.path.join(save_path,
                             "pnet_rnet_detections_%d.pkl" % int(time.time()))

    with open(save_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    # save_file = '/home/liujing/Codes/MTCNN/data/pnet_detections_1532582821.pkl'
    get_onet_sample_data(data_dir, anno_file, save_file, prefix_path)
Exemple #2
0
def gen_rnet_data(data_dir,
                  anno_file,
                  pnet_model_file,
                  prefix_path='',
                  use_cuda=True,
                  vis=False):
    # load the pnet and pnet_detector ,利用刚刚训练的pnet网络生成rnet的训练数据
    mtcnn_detector = MtcnnDetector(
        p_model_path=pnet_model_file,  # pnet_model_file自行设置
        r_model_path=None,
        o_model_path=None,
        min_face_size=12,
        use_cuda=True)
    device = mtcnn_detector.device

    # 生成rnet网络的训练集
    imagedb = ImageDB(anno_file, mode="test", prefix_path=prefix_path)
    imdb = imagedb.load_imdb()
    image_reader = TestImageLoader(imdb, 1, False)

    all_boxes = []
    batch_idx = 0

    for databatch in image_reader:
        if batch_idx % 100 == 0:
            print("%d images done" % batch_idx)
        im = databatch
        t = time.time()
        boxes, boxes_align = mtcnn_detector.detect_pnet(im)
        if boxes_align is None:
            all_boxes.append(np.array([]))
            continue
        if vis:
            vision.vis_face(im, boxes_align)

        t1 = time.time() - t
        print('time cost for image {} / {} : {:.4f}'.format(
            batch_idx, image_reader.size, t1))
        all_boxes.append(boxes_align)
        batch_idx += 1

    save_path = config.TRAIN_DATA_DIR
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    save_file = os.path.join(save_path,
                             "pnet_detections_%d.pkl" % int(time.time()))

    with open(save_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    # save_file = '/home/liujing/Codes/MTCNN/data/pnet_detections_1532530263.pkl'
    get_rnet_sample_data(data_dir, anno_file, save_file, prefix_path)