Beispiel #1
0
def Run(target):
    assert target in ['rnet', 'onet']

    model_path = GetModelPaths()
    batch_size = [2048, 256, 16]
    thresh = [0.4, 0.05, 0.7]
    min_face_size = 24

    detectors = [None, None, None]
    detectors[0] = FcnDetector(P_Net, model_path[0])

    if target == 'onet':
        detectors[1] = Detector(R_Net, 24, batch_size[1], model_path[1])

    data = read_annotation()

    if cfg.debug:
        data['bboxes'] = data['bboxes'][:20]
        data['images'] = data['images'][:20]

    test_data = TestLoader(data['images'])

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

    detections, _ = mtcnn_detector.detect_face(test_data)

    save_file = join(cfg.path_output_files, 'detections_%s.pkl' % target)
    with open(save_file, 'wb') as f:
        pickle.dump(detections, f, 1)
    save_hard_example(target, data, save_file)
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):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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 = '.'    
    #anno_file
    filename = './wider_face_train_bbx_gt.txt'
    #read annatation(type:dict)
    data = read_annotation(basedir,filename)
    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()
    test_data = TestLoader(data['images'])
    #list
    detections,_ = mtcnn_detector.detect_face(test_data)

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = os.path.join(data_dir, save_net)
    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)
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):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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 = '.'    
    #anno_file
    filename = './wider_face_train_bbx_gt.txt'
    #read annatation(type:dict)
    data = read_annotation(basedir,filename)
    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()
    test_data = TestLoader(data['images'][:100])
    #list
    detections,_ = mtcnn_detector.detect_face(test_data)

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = os.path.join(data_dir, save_net)
    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)
def t_net(prefix,
          epoch,
          data_dir,
          batch_size,
          PNet_factory,
          RNet_factory,
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):

    # load detectors
    detectors = [None, None, None]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]

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

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

    # Get detections
    basedir = '.'
    filename = './wider_face_train_bbx_gt.txt'
    data = read_annotation(basedir, filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    test_data = TestLoader(data['images'])
    print("Getting detections for {} images.".format(test_data.size))
    detections, _ = mtcnn_detector.detect_face(test_data)

    # Save detections
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    save_path = os.path.join(data_dir, save_net)
    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)

    save_hard_example(image_size, data, save_path)
def t_net(prefix, epoch, batch_size, test_mode, thresh, min_face_size=20,
          stride=2, slide_window=False):
    detectors = [None, None, None]
    # 生成指定模型的困难样本
    print("Test model: ", test_mode)
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    # PNet模型
    print(model_path[0])
    if slide_window:
        p_net = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        p_net = FcnDetector(P_Net, model_path[0])
    detectors[0] = p_net
    
    # RNet模型
    if test_mode in ["RNet", "ONet"]:
        print("=================   {}   =================".format(test_mode))
        r_net = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = r_net
    
    # ONet模型,这个模式下生成的样本主要用来观察,而不是训练
    if test_mode == "ONet":
        print("==================   {}   ================".format(test_mode))
        o_net = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = o_net
    
    # 读取bounding box的ground truth及图片,type:dict,include key 'images' and 'bboxes'
    data = read_bboxes_data(path_config.point2_train_txt_path, path_config.images_dir)
    
    mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                   stride=stride, threshold=thresh, slide_window=slide_window)
    
    print('加载原始图片数据,以进行检测及生成困难样本...')
    test_data = TestLoader(data['images'])
    print('加载完成, 开始检测...')
    detections, _ = mtcnn_detector.detect_images(test_data)
    print('检测完成!')
    
    # 保存检测结果
    if test_mode == "PNet":
        save_path = path_config.rnet_save_hard_path
    elif test_mode == "RNet":
        save_path = path_config.onet_save_hard_path
    else:
        raise ValueError('网络类型(--test_mode)错误!')
    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测试完成,开始生成困难样本..." % test_mode)
    save_hard_example(data, test_mode, save_path)
Beispiel #6
0
def GetDetector(
    model_path,
    minsize=20,
    factor=0.709,
    thresh_pred_p=0.6,
    thresh_pred_r=0.7,
    thresh_pred_o=0.8,
    thresh_nms_p=0.5,
    thresh_nms_r=0.7,
    thresh_nms_o=0.7,
    mode_p=nms_mode['Union'],
    mode_r=nms_mode['Union'],
    mode_o=nms_mode['Minimum'],
    thresh_merge=0.7,
    mode_merge=nms_mode['Union'],
):

    paths = GetModelPaths(model_path)
    detectors = [None, None, None]
    detectors[0] = FcnDetector(P_Net, paths[0])
    detectors[1] = Detector(R_Net, 24, 1, paths[1])
    detectors[2] = Detector(O_Net, 48, 1, paths[2])

    detector = MtcnnDetector(
        detectors=detectors,
        minsize=minsize,
        factor=factor,
        thresh_prediction=[thresh_pred_p, thresh_pred_r, thresh_pred_o],
        thresh_nms=[thresh_nms_p, thresh_nms_r, thresh_nms_o],
        modes=[mode_p, mode_r, mode_o],
        thresh_merge=thresh_merge,
        mode_merge=mode_merge)
    return detector
Beispiel #7
0
def mtcnn_detector_init():

    pnet_model_path = '../data/MTCNN_model/PNet_landmark/PNet'
    rnet_model_path = '../data/MTCNN_model/RNet_landmark/RNet'
    onet_model_path = '../data/MTCNN_model/ONet_landmark/ONet'
    test_mode = "onet"
    thresh = [0.9, 0.6, 0.7]
    min_face_size = 24
    stride = 2
    slide_window = False
    shuffle = False
    #vis = True
    detectors = [None, None, None]
    prefix = [pnet_model_path, rnet_model_path, onet_model_path]
    epoch = [18, 14, 16]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    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)

    return mtcnn_detector
Beispiel #8
0
def load_mtcnn(conf):
    # load mtcnn model
    MODEL_PATH = conf.get("MTCNN", "MODEL_PATH")
    MIN_FACE_SIZE = int(conf.get("MTCNN", "MIN_FACE_SIZE"))
    STEPS_THRESHOLD = [
        float(i) for i in conf.get("MTCNN", "STEPS_THRESHOLD").split(",")
    ]

    detectors = [None, None, None]
    prefix = [
        MODEL_PATH + "/PNet_landmark/PNet", MODEL_PATH + "/RNet_landmark/RNet",
        MODEL_PATH + "/ONet_landmark/ONet"
    ]
    epoch = [18, 14, 16]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    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,
                                   threshold=STEPS_THRESHOLD)
    return mtcnn_detector
def mtcnn_detector_init():
    test_mode = "ONet"
    thresh = [0.9, 0.6, 0.7]
    min_face_size = 24
    stride = 2
    slide_window = False
    shuffle = False
    detectors = [None, None, None]
    prefix = ['../data/MTCNN_model/PNet_landmark/PNet', '../data/MTCNN_model/RNet_landmark/RNet', '../data/MTCNN_model/ONet_landmark/ONet']
    epoch = [18, 14, 16]
    batch_size = [2048, 256, 16]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    # 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

    mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                stride=stride, threshold=thresh, slide_window=slide_window)
    
    return mtcnn_detector
Beispiel #10
0
 def Load(self):
     self.Load_pnet_model()
     self.Load_rnet_model()
     self.Load_onet_model()
     return MtcnnDetector(detectors=self.detectors,
                          min_face_size=self.min_face_size,
                          stride=self.stride,
                          threshold=self.thresh,
                          slide_window=self.slide_window)
class MTCNN_Crop_Face(object):
    def __init__(self):
        test_mode = "ONet"
        thresh = [0.9, 0.7, 0.7]
        min_face_size = 32
        stride = 2
        slide_window = False
        shuffle = False

        batch_size = [2048, 64, 16]
        detectors = [None, None, None]
        prefix = ['./data2/MTCNN_model/PNet_landmark/PNet', './data2/MTCNN_model/RNet_landmark/RNet', './data2/MTCNN_model/ONet_landmark/ONet']
        epoch = [18, 14, 16]
        model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
        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

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

    def cropface(self, frame):
        cropped_list = []

        i = 0

        image = np.array(frame)
        all_boxes, landmarks = self.mtcnn_detector.detect(image)
        # all_boxes,landmarks = self.mtcnn_detector.detect_face(test_data)
        # image = cv2.imread(imagepath)
        for bbox in all_boxes:
            corpbbox = [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])]
            if abs(corpbbox[3] - corpbbox[1]) > abs(corpbbox[2] - corpbbox[0]):
                n0 = int(0.5 * (corpbbox[2] + corpbbox[0]) - 0.5 * abs(corpbbox[3] - corpbbox[1]))
                n1 = corpbbox[1]
                n2 = int(0.5 * (corpbbox[2] + corpbbox[0]) + 0.5 * abs(corpbbox[3] - corpbbox[1]))
                n3 = corpbbox[3]
            else:
                n0 = corpbbox[0]
                n1 = int(0.5 * (corpbbox[3] + corpbbox[1]) - 0.5 * abs(corpbbox[2] - corpbbox[0]))
                n2 = corpbbox[2]
                n3 = int(0.5 * (corpbbox[3] + corpbbox[1]) + 0.5 * abs(corpbbox[2] - corpbbox[0]))
            # cropped = image[n1:n3, n0:n2]
            cropped = [int(n0), int(n1), int(n2), int(n3)]
            cropped_list.append(cropped)

            # cv2.rectangle(frame, (int(bbox[0]),int(bbox[1])),(int(bbox[2]),int(bbox[3])),(0,0,255))

        # cv2.imwrite("../data/test_result/%d.png" %(count),image)
        # cv2.imshow("image",frame)
        # cv2.waitKey(0)
        # return all_boxes
        return cropped_list
Beispiel #12
0
 def __init__(self, models_path, image_path, ground_truth_file):
     """ 初始化:加载PNet模型、待测试图片路径及对应ground truth
     :param models_path: [PNet, RNet, ONet]模型路径
     :param image_path: 待测试图片路径
     :param ground_truth_file: 待测图片目标ground_truth文件
     """
     # 初始化PNet模型检测器
     pnet_model = None
     rnet_model = None
     onet_model = None
     if models_path[0] is not None:
         pnet_model = FcnDetector(P_Net, models_path[0])
     if models_path[1] is not None:
         rnet_model = Detector(R_Net, 24, 256, models_path[1])
     if models_path[2] is not None:
         onet_model = Detector(O_Net, 48, 16, models_path[2])
         
     self.detector = MtcnnDetector([pnet_model, rnet_model, onet_model], min_face_size=20, stride=2,
                                   threshold=[0.7, 0.7, 0.7], scale_factor=0.79, slide_window=False)
     # 初始化ground truth
     self.ground_map = dict()
     valid_image_path = list()
     with open(ground_truth_file, 'r') as truth_file:
         for ground_truth in truth_file:
             ground_truth = ground_truth.strip().split(' ')
             self.ground_map[ground_truth[0]] = np.array([float(_) for _ in ground_truth[1:]])
             valid_image_path.append(ground_truth[0])
             
     # 初始化图片加载器
     if os.path.isdir(image_path):
         images_path = PNetTester.search_file(image_path)
     elif os.path.isfile(image_path) and image_path.endswith('.jpg'):
         images_path = [image_path]
     
     self.images_path = list()
     for image_path in images_path:
         if os.path.basename(image_path) in valid_image_path:
             self.images_path.append(image_path)
     print('待检测图片数量:', len(self.images_path))
     self.test_loader = TestLoader(self.images_path)
     
     return
    def __init__(self):
        test_mode = "ONet"
        thresh = [0.9, 0.7, 0.7]
        min_face_size = 32
        stride = 2
        slide_window = False
        shuffle = False

        batch_size = [2048, 64, 16]
        detectors = [None, None, None]
        prefix = ['./data2/MTCNN_model/PNet_landmark/PNet', './data2/MTCNN_model/RNet_landmark/RNet', './data2/MTCNN_model/ONet_landmark/ONet']
        epoch = [18, 14, 16]
        model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
        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

        self.mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                       stride=stride, threshold=thresh, slide_window=slide_window)
def load_mtcnn(conf):
    # load mtcnn model
    MODEL_PATH = conf.get("MTCNN", "MODEL_PATH")
    MIN_FACE_SIZE = int(conf.get("MTCNN", "MIN_FACE_SIZE"))
    STEPS_THRESHOLD = [
        float(i) for i in conf.get("MTCNN", "STEPS_THRESHOLD").split(",")
    ]

    detectors = [None, None, None]
    prefix = [
        MODEL_PATH + "/PNet_landmark/PNet", MODEL_PATH + "/RNet_landmark/RNet",
        MODEL_PATH + "/ONet_landmark/ONet"
    ]
    epoch = [18, 14, 16]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    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,
                                   threshold=STEPS_THRESHOLD)
    return mtcnn_detector

    # Check if the model is a model directory (containing a metagraph and a checkpoint file)
    #  or if it is a protobuf file with a frozen graph
    model_exp = os.path.expanduser(model)
    if (os.path.isfile(model_exp)):
        print('Model filename: %s' % model_exp)
        with tf.gfile.FastGFile(model_exp, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='')
    else:
        print('Model directory: %s' % model_exp)
        meta_file, ckpt_file = get_model_filenames(model_exp)
        print('Metagraph file: %s' % meta_file)
        print('Checkpoint file: %s' % ckpt_file)
        saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file))
        saver.restore(tf.get_default_session(),
                      os.path.join(model_exp, ckpt_file))
Beispiel #15
0
def process_img():
    param = parameter()
    min_size = param.min_size
    score_threshold = param.threshold
    slid_window = param.slid_window
    if test_relu == 100:
        batch_size = [1, 1, 1]
    else:
        batch_size = param.batch_size
    epoch_load = param.epoch_load
    multi_detector = [None, None, None]
    #load paramter path
    model_path = load_model(epoch_load)
    #load net result
    if config.train_face:
        Pnet_det = FcnDetector(P_Net, model_path[0])
    else:
        Pnet_det = FcnDetector(P_Net_W, model_path[0])
    Rnet_det = Detector(R_Net,
                        data_size=24,
                        batch_size=batch_size[1],
                        model_path=model_path[1])
    if len(model_path) == 3:
        Onet_det = Detector(O_Net,
                            data_size=48,
                            batch_size=batch_size[2],
                            model_path=model_path[2])
        multi_detector = [Pnet_det, Rnet_det, Onet_det]
    else:
        multi_detector = [Pnet_det, Rnet_det, None]
    #get bbox and landmark
    Mtcnn_detector = MtcnnDetector(multi_detector,
                                   min_size,
                                   threshold=score_threshold)
    #bboxs,bbox_clib,landmarks = Mtcnn_detector.detect(img)
    return Mtcnn_detector
Beispiel #16
0
    def build_camera(self, camera_id, path_name):
        
        #print("Loading Deep Face Detector(MTCNN) recognition model ............")        
        #test_mode = "onet"
        thresh = [0.9, 0.9, 0.8]
        min_face_size = 81 
        stride = 2
        slide_window = False
        #shuffle = False
        #vis = True
        detectors = [None, None, None]
        prefix = ['../execute_system/MTCNN_Tensorflow_fast/data/MTCNN_model/PNet_landmark/PNet', 
                  '../execute_system/MTCNN_Tensorflow_fast/data/MTCNN_model/RNet_landmark/RNet', 
                  '../execute_system/MTCNN_Tensorflow_fast/data/MTCNN_model/ONet_landmark/ONet']
        
        epoch = [40, 36, 36]
        model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
        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)
                
        # read names from dataset
        name_list = read_name_list(path_name)        
        cap = cv2.VideoCapture(camera_id)    
        
        #fps1 = cap.get(cv2.CAP_PROP_FPS)
        #size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        #        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        #size = 640 x 480
       
        cap.set(cv2.CAP_PROP_FRAME_WIDTH,528)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT,384)
   
        while True:            

            t1 = cv2.getTickCount()

            success, frame = cap.read() 
            if success:                                     
                thickness = (frame.shape[0] + frame.shape[1]) // 350
                image = np.array(frame)
                
                boxes_c,landmarks = mtcnn_detector.detect(image)
                #print(landmarks.shape)
                
                t2 = cv2.getTickCount()             
                t = (t2 - t1) / cv2.getTickFrequency()
                fps = 1.0 / t            
                print('fps:',fps)
                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
    
                    label,prob = self.model.face_predict(crop_img) 
                         
                    if prob > 0.7:    
                        show_name = name_list[label]                     
                    else:
                        show_name = 'Stranger'
                                                                       
                    person_tag = "%s: %.2f" %(show_name, prob)
                    #text_end = (int(cropbbox[0]) + len(person_tag) * 10,int(cropbbox[1]) - 20 )
                    text_start = (max(int(cropbbox[0]), 10), max(int(cropbbox[1]), 10))
                    #cv2.rectangle(draw, text_end, text_start, (255, 255, 0), -1, cv2.LINE_AA )
                    cv2.putText(frame, person_tag, text_start, cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,255), 1)  
                                                                              
                     # 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)) 
                cv2.imshow("Camera", frame)
             
                k = cv2.waitKey(10)
                if k & 0xFF == ord('q'):
                    break        
            else:
                print ('device not find')
                break
        cap.release()
        cv2.destroyAllWindows()
Beispiel #17
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):
    """
    Parameter
    ----------------
    



    """
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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
    pnet_detections = None
    if test_mode in ["RNet", "ONet"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet
        print("load pnet predictions...")
        pnet_detections = pickle.load(
            open(os.path.join("../data/24/RNet", 'detections.pkl'), 'rb'))
        print("load pnet predictions done.")

    # load onet model
    rnet_detections = None
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet
        print("load rnet predictions...")
        rnet_detections = pickle.load(
            open(os.path.join("../data/48/ONet", 'detections.pkl'), 'rb'))
        print("load rnet predictions done.")

    # detec的数据源始终是一样的
    basedir = 'E:/Document/Datasets/Wider Face'
    #anno_file
    filename = 'wider_face_train_bbx_gt.txt'
    #data是一个dict,data["images"]为所有照片路径list,data["bboxes"]是所有照片bboxes的值,一一对应
    data = read_annotation(basedir, filename)
    # 初始化mtcnn_detector,就是训练好的模型
    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')
    #所有照片,训练时为12880张
    test_data = TestLoader(data['images'])
    print('finish loading')
    #list
    print('start detecting....')
    detections, _ = mtcnn_detector.detect_face(
        test_data, pnet_detections, rnet_detections)  #调用训练好的pnet,返回的是candidate
    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 = os.path.join(data_dir, save_net)
    print('save_path is :')
    print(save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    # 把所有candidate写到文件中序列化
    save_file = os.path.join(save_path, "detections.pkl")
    with open(save_file, 'wb') as f:
        pickle.dump(detections, f, 1)
    print("save done.")
    print("%s测试完成开始OHEM" % image_size)
    # 然后根据candidate和gt的Iou把每个candidate分类并保存到文本和相应的照片
    save_hard_example(image_size, data, save_path)
Beispiel #18
0
def RTrecognization(facenet_model_path, SVCpath, database_path):
    #facenet_model_path为facenet模型路径
    #SVCpath为SVM分类模型路径
    #database_path为人脸库数据
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(facenet_model_path)
            with open(SVCpath, 'rb') as infile:
                (classifymodel, class_names) = pickle.load(infile)
            print('Loaded classifier model from file "%s"' % SVCpath)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]
            Database = np.load(database_path)

            test_mode = "onet"
            thresh = [0.9, 0.6, 0.7]
            min_face_size = 24
            stride = 2
            slide_window = False
            shuffle = False
            #vis = True
            detectors = [None, None, None]
            prefix = [
                '../data/MTCNN_model/PNet_landmark/PNet',
                '../data/MTCNN_model/RNet_landmark/RNet',
                '../data/MTCNN_model/ONet_landmark/ONet'
            ]
            epoch = [18, 14, 16]
            model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
            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)
            video_capture = cv2.VideoCapture(0)
            # video_capture.set(3, 340)
            # video_capture.set(4, 480)
            video_capture.set(3, 800)
            video_capture.set(4, 800)
            corpbbox = None
            while True:
                t1 = cv2.getTickCount()
                ret, frame = video_capture.read()
                if ret:
                    image = np.array(frame)
                    img_size = np.array(image.shape)[0:2]
                    boxes_c, landmarks = mtcnn_detector.detect(image)
                    # print(boxes_c.shape)
                    # print(boxes_c)
                    # print(img_size)
                    t2 = cv2.getTickCount()
                    t = (t2 - t1) / cv2.getTickFrequency()
                    fps = 1.0 / t
                    for i in range(boxes_c.shape[0]):
                        bbox = boxes_c[i, :4]  #检测出的人脸区域,左上x,左上y,右下x,右下y
                        score = boxes_c[i, 4]  #检测出人脸区域的得分
                        corpbbox = [
                            int(bbox[0]),
                            int(bbox[1]),
                            int(bbox[2]),
                            int(bbox[3])
                        ]

                        x1 = np.maximum(int(bbox[0]) - 16, 0)
                        y1 = np.maximum(int(bbox[1]) - 16, 0)
                        x2 = np.minimum(int(bbox[2]) + 16, img_size[1])
                        y2 = np.minimum(int(bbox[3]) + 16, img_size[0])
                        crop_img = image[y1:y2, x1:x2]
                        scaled = misc.imresize(crop_img, (160, 160),
                                               interp='bilinear')
                        img = load_image(scaled, False, False, 160)
                        img = np.reshape(img, (-1, 160, 160, 3))
                        feed_dict = {
                            images_placeholder: img,
                            phase_train_placeholder: False
                        }
                        embvecor = sess.run(embeddings, feed_dict=feed_dict)
                        embvecor = np.array(embvecor)
                        #利用人脸特征与数据库中所有人脸进行一一比较的方法
                        # tmp=np.sqrt(np.sum(np.square(embvecor-Database['emb'][0])))
                        # tmp_lable=Database['lab'][0]
                        # for j in range(len(Database['emb'])):
                        #     t=np.sqrt(np.sum(np.square(embvecor-Database['emb'][j])))
                        #     if t<tmp:
                        #         tmp=t
                        #         tmp_lable=Database['lab'][j]
                        # print(tmp)

                        #利用SVM对人脸特征进行分类
                        predictions = classifymodel.predict_proba(embvecor)
                        best_class_indices = np.argmax(predictions, axis=1)
                        tmp_lable = class_names[best_class_indices]
                        best_class_probabilities = predictions[
                            np.arange(len(best_class_indices)),
                            best_class_indices]
                        print(best_class_probabilities)
                        if best_class_probabilities < 0.4:
                            tmp_lable = "others"
                        cv2.rectangle(frame, (corpbbox[0], corpbbox[1]),
                                      (corpbbox[2], corpbbox[3]), (255, 0, 0),
                                      1)
                        cv2.putText(frame, '{0}'.format(tmp_lable),
                                    (corpbbox[0], corpbbox[1] - 2),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255),
                                    2)
                    cv2.putText(
                        frame, '{:.4f}'.format(t) + " " + '{:.3f}'.format(fps),
                        (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255),
                        2)
                    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))
        # time end
                    cv2.imshow("", frame)
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break
                else:

                    print('device not find')
                    break
            video_capture.release()
            cv2.destroyAllWindows()
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):
    """
    :param prefix:
    :param epoch:
    :param batch_size:
    :param test_mode:
    :param thresh:
    :param min_face_size:
    :param stride:
    :param slide_window:
    :param shuffle:
    :param vis:
    :return:
    """
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    # PNet-echo
    model_path = []
    for x, y in zip(prefix, epoch):
        model_path.append(os.path.join(x, test_mode.lower() + '.ckpt-%s' % y))
    # model_path = ['%s/pnet.ckpt-%s' % (x, y) for x, y in zip(prefix, epoch)]
    # 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 = '../../DATA/'
    # anno_file
    filename = './wider_face_train_bbx_gt.txt'
    # read anotation(type:dict), include 'images' and 'bboxes'
    data = read_annotation(basedir, filename)  # 12880张图片
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    # 注意是在“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('finish detecting ')
    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    # save detect result
    save_path = os.path.join(data_dir, save_net)
    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)
Beispiel #20
0
def RTrecognization(facenet_model_path,SVCpath,database_path):
    #facenet_model_path为facenet模型路径
    #SVCpath为SVM分类模型路径
    #database_path为人脸库数据
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(facenet_model_path)
            with open(SVCpath, 'rb') as infile:
                    (classifymodel, class_names) = pickle.load(infile)
            print('Loaded classifier model from file "%s"' % SVCpath)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]
            Database=np.load(database_path)

            """
                model_folder : string
                    path for the models  
                    训练好的模型路径
                minsize : float number
                    minimal face to detect
                threshold : float number
                    detect threshold for 3 stages  
                    threshold参数是一个包含3个值的列表,这3个值在算法的3个stage中将分别用到,
                    可以看到这3个threshold值是递增的,是因为在3个stage中对一个bbox是否是人脸的置信度要求越来越高。
                factor: float number
                    scale factor for image pyramid
                    factor表示和图像金字塔相关的一个参数,表示图像金字塔的每相邻两层之间的倍数关系是factor。
                num_worker: int number
                    number of processes we use for first stage
                accurate_landmark: bool
                    use accurate landmark localization or not
            """
            test_mode = "onet"
            thresh = [0.9, 0.6, 0.7]
            min_face_size = 24
            stride = 2
            slide_window = False
            shuffle = False
            #vis = True
            detectors = [None, None, None]
            prefix = ['../data/MTCNN_model/PNet_landmark/PNet', '../data/MTCNN_model/RNet_landmark/RNet',
                      '../data/MTCNN_model/ONet_landmark/ONet']
            epoch = [18, 14, 16]
            model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
            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)
            video_capture = cv2.VideoCapture(0)
            # video_capture.set(3, 340)
            # video_capture.set(4, 480)
            video_capture.set(3, 800)
            video_capture.set(4, 800)
            corpbbox = None
            while True:
                 t1 = cv2.getTickCount()
                 ret, frame = video_capture.read()
                 if ret:
                    image = np.array(frame)
                    img_size=np.array(image.shape)[0:2]
                    boxes_c,landmarks = mtcnn_detector.detect(image)
                    # print(boxes_c.shape)
                    # print(boxes_c)
                    # print(img_size)
                    t2 = cv2.getTickCount()
                    t = (t2 - t1) / cv2.getTickFrequency()
                    fps = 1.0 / t
                    for i in range(boxes_c.shape[0]):
                        bbox = boxes_c[i, :4]#检测出的人脸区域,左上x,左上y,右下x,右下y
                        score = boxes_c[i, 4]#检测出人脸区域的得分
                        corpbbox = [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])]
                        
                        x1=np.maximum(int(bbox[0])-16,0)
                        y1=np.maximum(int(bbox[1])-16,0)
                        x2=np.minimum( int(bbox[2])+16,img_size[1])
                        y2=np.minimum( int(bbox[3])+16,img_size[0])
                        crop_img=image[y1:y2,x1:x2]
                        scaled=misc.imresize(crop_img,(160,160),interp='bilinear')
                        img=load_image(scaled,False, False,160)
                        img=np.reshape(img,(-1,160,160,3))
                        feed_dict = { images_placeholder:img, phase_train_placeholder:False }
                        embvecor=sess.run(embeddings, feed_dict=feed_dict)
                        embvecor=np.array(embvecor)
                        #利用人脸特征与数据库中所有人脸进行一一比较的方法
                        # tmp=np.sqrt(np.sum(np.square(embvecor-Database['emb'][0])))
                        # tmp_lable=Database['lab'][0]
                        # for j in range(len(Database['emb'])):
                        #     t=np.sqrt(np.sum(np.square(embvecor-Database['emb'][j])))
                        #     if t<tmp:
                        #         tmp=t
                        #         tmp_lable=Database['lab'][j]
                        # print(tmp)
    
                        # 利用SVM对人脸特征进行分类
                        # 通过输入的图片的编码,通过此分类器预测模型的名称
                        # 得到的结果是对于每张图片,都进行预测,得出可能是某个人物的可能性
                        predictions = classifymodel.predict_proba(embvecor)
                        # 返回每行最大值的索引,也就是返回这张图片对应的人物的可能性最大的姓名的索引
                        best_class_indices = np.argmax(predictions, axis=1)
                        tmp_lable=class_names[best_class_indices]
                        # 行索引,加列索引,就得到了所有图片预测的可能性
                        best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                        print(best_class_probabilities)

                        if best_class_probabilities<0.4:
                            tmp_lable="unknown"
                        cv2.rectangle(frame, (corpbbox[0], corpbbox[1]),
                          (corpbbox[2], corpbbox[3]), (255, 0, 0), 1)
                        cv2.putText(frame, '{0}'.format(tmp_lable), (corpbbox[0], corpbbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (0, 0, 255), 2)
                    cv2.putText(frame, '{:.4f}'.format(t) + " " + '{:.3f}'.format(fps), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (255, 0, 255), 2)
                    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))            
        # time end
                    cv2.imshow("", frame)
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break

                 else:
                    print('device not find')
                    break
            video_capture.release()
            cv2.destroyAllWindows()
Beispiel #21
0
detectors = [None, None, None]
prefix = ['../data/MTCNN_model/PNet/model', '../data/MTCNN_model/RNet/model', '../data/MTCNN_model/ONet/model']
epoch = [30, 22, 22]
batch_size = [2048, 256, 16]
model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]

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

RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
detectors[1] = RNet

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

data, num = tools.get_val(label_path)
f = open('../data/val_rnet.txt', 'w')
print('total_images = ' + str(num))
for id in range(num):
    mean_iou = 0
    im = cv2.imread(data['images'][id])
    # cv2.imshow('test', im)
    # cv2.waitKey(0)
    print im.shape
    labels = data['bboxes'][id]
    _, all_boxes, _ = mtcnn_detector.detect_pnet(im)
    if all_boxes is None:
        f.write('%s : num_boxes = %s ;  mean_iou = %s ;  accuracy = %s\n' % (data['images'][id], 0, 0, 0))
        print(data['images'][id] + '  : No face after PNet!')
Beispiel #22
0
def t_net(prefix, epoch,batch_size, img_saved_dir,anno_file,gen_anno_file,gen_imgs_dir,data_set_name,ignore_det=False,test_mode="PNet",thresh=[0.6, 0.6, 0.7], min_face_size=25,\
             stride=2):
    slide_window = False
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    print("epoch num ", epoch[0])
    ''' #for Pnet test
    epoch_num = epoch[0]
    epoch_c = np.arange(2,epoch_num,2)
    prefix_c = []
    prefix = prefix[0]
    [prefix_c.append(prefix) for i in range(len(epoch_c))]
    '''
    print("prefixs is ", prefix)
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    #print("after zip model_path is ",model_path)
    #model_path[0] = prefix + '-'+str(epoch_num) #for Pnet test
    print("model_path 0 is ", model_path[0])
    # 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
    #read annatation(type:dict)
    #img_box_dic = read_annotation(img_saved_dir,anno_file)
    img_box_dic = rd_anotation(img_saved_dir, anno_file, data_set_name)
    print("gen_hardexample  threshold ", thresh)
    if not ignore_det:
        mtcnn_detector = MtcnnDetector(detectors=detectors,
                                       min_face_size=min_face_size,
                                       stride=stride,
                                       threshold=thresh)
    print("==================================")
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    test_data = TestLoader(img_box_dic['images'])
    #list
    if not ignore_det:
        detections, _ = mtcnn_detector.detect_face(test_data)
    if test_mode == "PNet":
        save_net = "RNet"
        save_path = '24/RNet'
    elif test_mode == "RNet":
        save_net = "ONet"
        save_path = "48/ONet"
    #save detect result
    #save_path = os.path.join(data_dir, save_net)
    print("save path is", save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file = os.path.join(save_path, "detections.pkl")
    if not ignore_det:
        with open(save_file, 'wb') as f:
            pickle.dump(detections, f, 1)
        f.close()
    print("%s Test is Over and begin OHEM" % image_size)
    save_hard_example(gen_anno_file, gen_imgs_dir, img_box_dic, save_path,
                      test_mode)
Beispiel #23
0
        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

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

    image_info = get_image_info(anno_file)

    current_event = ''
    save_path = ''
    idx = 0
    for item in image_info:
        idx += 1
        image_file_name = os.path.join(data_dir, item[0], item[1])
        if current_event != item[0]:

            current_event = item[0]
            save_path = os.path.join(output_file, item[0])
def t_net(
        prefix,
        epoch,
        batch_size,
        test_mode="PNet",
        thresh=[0.6, 0.6, 0.7],
        min_face_size=25,  # prefix保存模型文件路径
        stride=2,
        slide_window=False,
        shuffle=False,
        vis=False):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = [
        '%s-%s' % (x, y) for x, y in zip(prefix, epoch)
    ]  # model_path == <class 'list'>: ['../data/MTCNN_model/PNet_landmark/PNet-16', '../data/MTCNN_model/RNet_landmark/RNet-6', '../data/MTCNN_model/ONet/ONet-22']

    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(
            P_Net, model_path[0]
        )  # '../data/MTCNN_model/PNet_landmark/PNet-16', 生成全连接detection的检测对象P_Net
    detectors[0] = PNet  # 将PNet对象放入检测器的第1个位

    # 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 = '.'
    filename = './wider_face_train_bbx_gt.txt'  # 获取检测框的ground truth值
    data = read_annotation(
        basedir, filename
    )  # 读pic的文件名,和box的ground truth值,data['images']==all image pathes
    #  data['bboxes'] =all image bboxes
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    print("==================================")

    test_data = TestLoader(
        data['images']
    )  # 生成输入图片的管理对象test_data, # 注意是在“test”模式下,  imdb = IMDB("wider", image_set, root_path, dataset_path, 'test'),  gt_imdb = imdb.gt_imdb()
    detections, _ = mtcnn_detector.detect_face(
        test_data)  # 从test_data输入人脸pixel,返回detection和cls

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = os.path.join(data_dir, save_net)  # save_path == “24 / Rnet”
    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:  # save_file == detections.pkl
        pickle.dump(detections, f, 1)  # 将detection结果写入文件
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data,
                      save_path)  # data,读pic的文件名,和box的ground truth值,
Beispiel #25
0
    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

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


def extract_face(avideo_path, num_share, total_share):
    # set_trace(term_size=(80, 24))
    # print('run task %s (%s)...' % (avideo_path, os.getpid()))
    # 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"]:
    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

mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                               stride=stride, threshold=thresh, slide_window=slide_window)
gt_imdb = []
#gt_imdb.append("35_Basketball_Basketball_35_515.jpg")
#imdb_ = dict()"
#imdb_['image'] = im_path
#imdb_['label'] = 5
path = "lala"
for item in os.listdir(path):
    gt_imdb.append(os.path.join(path,item))
test_data = TestLoader(gt_imdb)
all_boxes,landmarks = mtcnn_detector.detect_face(test_data)
count = 0
for imagepath in gt_imdb:
    print imagepath
    image = cv2.imread(imagepath)
    for bbox in all_boxes[count]:
prefix = [
    'data/MTCNN_model/PNet_landmark/PNet',
    'data/MTCNN_model/RNet_landmark/RNet',
    'data/MTCNN_model/ONet_landmark/ONet'
]
epoch = [18, 14, 16]
model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
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)

camera = cv2.VideoCapture(1)
name = 'test'
count = 0

path = os.getcwd() + '/data-face/' + name
if not os.path.exists(path):
    os.makedirs(path)
while True:
    _, frame = camera.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    boxes_c, _ = mtcnn_detector.detect(frame)
    for i in range(boxes_c.shape[0]):
Beispiel #28
0
    #PNet = FcnDetector(P_Net_new, model_path[0])
    PNet = FcnDetector(P_Net, model_path[0], COLOR_GRAY)
detectors[0] = PNet
# load rnet model
if test_mode in ["RNet", "ONet"]:
    RNet = Detector(R_Net, 24, batch_size[1], model_path[1], COLOR_GRAY)
    detectors[1] = RNet

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

mtcnn_detector = MtcnnDetector(detectors=detectors,
                               min_face_size=min_face_size,
                               stride=stride,
                               threshold=thresh,
                               slide_window=slide_window,
                               COLO_GRA=COLOR_GRAY)
gt_imdb = []
#gt_imdb.append("35_Basketball_Basketball_35_515.jpg")
#imdb_ = dict()"
#imdb_['image'] = im_path
#imdb_['label'] = 5
path = "X:\\deeplearn\\mtcnn\\MTCNN-Tensorflow-master_change\\test\\lala"
for item in os.listdir(path):
    if item.find('.png') == -1 and item.find('.bmp') == -1 and item.find(
            '.jpg') == -1:
        continue
    gt_imdb.append(os.path.join(path, item))
test_data = TestLoader(gt_imdb,
                       batch_size=1,
Beispiel #29
0
        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

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

    imdb = get_imdb_fddb(data_dir)
    nfold = len(imdb)
    for i in xrange(nfold):
        image_names = imdb[i]
        print image_names
        dets_file_name = os.path.join(out_dir,
                                      'FDDB-det-fold-%02d.txt' % (i + 1))
        fid = open(dets_file_name, 'w')
        sys.stdout.write('%s ' % (i + 1))
        image_names_abs = [
            os.path.join(data_dir, image_name + '.jpg')
            for image_name in image_names
     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
 
 mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                stride=stride, threshold=thresh, slide_window=slide_window)
 
 
 
 imdb = get_imdb_fddb(data_dir)
 nfold = len(imdb)    
 for i in xrange(nfold):
     image_names = imdb[i]
     print image_names
     dets_file_name = os.path.join(out_dir, 'FDDB-det-fold-%02d.txt' % (i + 1))
     fid = open(dets_file_name,'w')
     sys.stdout.write('%s ' % (i + 1))
     image_names_abs = [os.path.join(data_dir,'originalPics',image_name+'.jpg') for image_name in image_names]
     test_data = TestLoader(image_names_abs)
     all_boxes,_ = mtcnn_detector.detect_face(test_data, vis=vis)
     
def t_net(prefix,
          epoch,
          batch_size,
          test_mode='PNet',
          thresh=[0.6, 0.6, 0.6],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):
    """
    :param prefix: 模型名字的前缀
    :param epoch:上一个模型的训练次数
    :param batch_size:用在预测中的批的尺寸
    :param test_mode:测试模式
    :param thresh:阈值
    :param min_face_size:最小检测人脸大小
    :param stride:
    :param slide_window:是否在pnet中应用了sliding window
    :param shuffle:
    :param vis:
    :return:
    """
    detectors = [None, None, None]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    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

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

    basedir = '.'
    # 注解文件
    filename = './wider_face_train_bbx_gt.txt'

    data = read_annotation(basedir, filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    test_data = TestLoader(data['images'])
    detections, _ = mtcnn_detector.detect_face(test_data)

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    # 保存探测结果
    save_path = os.path.join(data_dir, save_net)

    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)
Beispiel #32
0
def t_net(data_dir,
          prefix=[
              './modelfiles/PNet/PNet', './modelfiles/RNet/RNet',
              './modelfiles/ONet/ONet'
          ],
          epoch=[18, 14, 16],
          batch_size=[2048, 256, 16],
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):

    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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

    # anno_file
    # read anotation(type:dict), include 'images' and 'bboxes'
    data = read_annotation(data_dir,
                           './prepare_data/wider_face_train_bbx_gt.txt')
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    print("==================================")
    print('load test data')
    test_data = TestLoader(data['images'])
    print('finish loading')

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
        image_size = 24
    elif test_mode == "RNet":
        save_net = "ONet"
        image_size = 48

    # save detect result
    save_path = os.path.join(data_dir, save_net)
    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")

    print('start detecting....')
    if not os.path.exists(save_file):
        detections, _ = mtcnn_detector.detect_face(test_data)
        with open(save_file, 'wb') as f:
            pickle.dump(detections, f, 1)
    print('finish detecting ')

    print("%s测试完成开始OHEM" % image_size)
    PNet = FcnDetector(P_Net, model_path[0])
detectors[0] = PNet

# load R-net model
if test_mode in ['RNet', 'ONet']:
    RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
    detectors[1] = RNet

# load O-net model
if test_mode == 'ONet':
    ONet = Detector(O_Net, 48, batch_size[2], 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)

path = os.path.join(os.pardir, 'data/test/lfpw_testImage')
gt_imdb = [os.path.join(path, item) for item in os.listdir(path)]

test_data = TestLoader(gt_imdb)
# boxes, landmarks = mtcnn_detector.detect_face(test_data)

out_dir = os.path.dirname(gt_imdb[0]) + '_out'
if not os.path.exists(out_dir):
    os.makedirs(out_dir)

for path in gt_imdb:
    print(path)
stride = 2
slide_window = False
shuffle = False
#vis = True
detectors = [None, None, None]
prefix = ['../data/MTCNN_model/PNet_landmark/PNet', '../data/MTCNN_model/RNet_landmark/RNet', '../data/MTCNN_model/ONet_landmark/ONet']
epoch = [18, 14, 16]
model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
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
videopath = "./video_test.avi"
mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                               stride=stride, threshold=thresh, slide_window=slide_window)

video_capture = cv2.VideoCapture(videopath)
video_capture.set(3, 340)
video_capture.set(4, 480)
corpbbox = None
while True:
    # fps = video_capture.get(cv2.CAP_PROP_FPS)
    t1 = cv2.getTickCount()
    ret, frame = video_capture.read()
    if ret:
        image = np.array(frame)
        boxes_c,landmarks = mtcnn_detector.detect(image)
        
        print landmarks.shape
        t2 = cv2.getTickCount()