Esempio n. 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)
Esempio n. 2
0
 def Load_pnet_model(self):
     if self.slide_window:
         PNet = Detector(P_Net, 12, self.batch_size[0], self.model_path[0])
     else:
         PNet = FcnDetector(P_Net, self.model_path[0])
         print(self.model_path[0])
     self.detectors[0] = PNet
Esempio n. 3
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
Esempio n. 4
0
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
Esempio n. 5
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
Esempio n. 6
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 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)
Esempio n. 8
0
def GetModel(target):
    assert target in ['pnet', 'rnet', 'onet']
    paths = GetModelPaths()
    if target == 'pnet':
        return FcnDetector(P_Net, paths[0])
    if target == 'rnet':
        return Detector(R_Net, 24, 1, paths[1])
    if target == 'onet':
        return Detector(O_Net, 48, 1, paths[2])
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)
Esempio n. 11
0
def freeze_graphOri(input_checkpoint, output_graph):
    slide_window = True
    test_mode = "ONet"
    # 指定输出的节点名称,该节点名称必须是原模型中存在的节点
    output_node_names = "cls_fc/Softmax,bbox_fc/BiasAdd,landmark_fc/BiasAdd"# "Squeeze,Squeeze_1,Squeeze_2"
    imgSize = 12
    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, 1, model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    # detectors[0] = PNet

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

    # load onet model
    if test_mode == "ONet":
        imgSize = 48
        ONet = Detector(O_Net, 48, 1, model_path[2])
        # detectors[2] = ONet
    # x = tf.placeholder('float', shape=[None, imgSize, imgSize, 3], name='input_image')
    sess = ONet.sess

    # 写图
    # writer = tf.summary.FileWriter('./logs/', sess.graph)
    # writer.close()
    # exit(1)

    # saver = tf.train.Saver()
    # with tf.Session(config=config) as sess:
        # saver.restore(sess, tf.train.latest_checkpoint(input_checkpoint))  # 恢复图并得到数据

    model_dict = '/'.join(model_path[2].split('/')[:-1])
    ckpt = tf.train.latest_checkpoint(model_dict)
    tmpPB = './model_save/tmpGraph.pb'
    tf.train.write_graph(sess.graph_def, './model_save', 'tmpGraph.pb')
    freeze_graph.freeze_graph(input_graph=tmpPB, input_checkpoint=str(ckpt), output_node_names=output_node_names,
                              output_graph='./model_save/modelTest.pb', input_saver='', input_binary=False,
                              restore_op_name='', filename_tensor_name='', clear_devices=True, initializer_nodes='')
    exit(1)

    output_graph_def = graph_util.convert_variables_to_constants(  # 模型持久化,将变量值固定
        sess=sess,
        input_graph_def=sess.graph_def,  # 等于:sess.graph_def
        output_node_names=output_node_names.split(","))  # 如果有多个输出节点,以逗号隔开

    with tf.gfile.GFile(output_graph, "wb") as f:  # 保存模型
        f.write(output_graph_def.SerializeToString())  # 序列化输出
    print("%d ops in the final graph." % len(output_graph_def.node))  # 得到当前图有几个操作节点
Esempio n. 12
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
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))
Esempio n. 14
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
Esempio n. 15
0
    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)
Esempio n. 16
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()
Esempio n. 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):
    """
    :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)
Esempio n. 18
0
prefix = [
    'X:/deeplearn/mtcnn/MTCNN-Tensorflow-master_change/data/new_model/PNet_NIR_calib_gray/PNet',
    'X:/deeplearn/mtcnn/MTCNN-Tensorflow-master_change/data/new_model/RNet_NIR_calib_gray/RNet',
    'X:/deeplearn/mtcnn/MTCNN-Tensorflow-master_change/data/new_model/test2ONet_NIR_calib_gray/ONet'
]
epoch = [30, 40, 40]  # train select
COLOR_GRAY = 1  #color 0 gray 1

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], COLOR_GRAY)
else:
    #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,
Esempio n. 19
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):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    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"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

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

    basedir = '../../DATA/'
    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)
    print("==================================")

    print('load test data')
    test_data = TestLoader(data['images'])
    print('finish loading')
    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_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)
    save_hard_example(image_size, data, save_path)
Esempio n. 20
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)
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)
Esempio n. 22
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()
Esempio n. 23
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)
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值,
Esempio n. 25
0
    def build_camera(self, camera_id, path):
        count = 500
        thresh = [0.9, 0.9, 0.8]
        min_face_size = 100
        stride = 2
        slide_window = False
        detectors = [None, None, None]
        prefix = [
            'E:/sign_system/execute_system/MTCNN_Tensorflow_fast/data/MTCNN_model_V2/PNet_landmark/PNet',
            'E:/sign_system/execute_system/MTCNN_Tensorflow_fast/data/MTCNN_model_V2/RNet_landmark/RNet',
            'E:/sign_system/execute_system/MTCNN_Tensorflow_fast/data/MTCNN_model_V2/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)
        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
            if success:
                t1 = time.time()
                image = np.array(frame)
                boxes_c, landmarks = mtcnn_detector.detect(image)
                #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)]

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

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

                    f1_emb = self.get_feature(image)
                    f1 = f1_emb.reshape(256)
                    #计算距离
                    d1 = 100
                    show_name = ''
                    embed1 = sklearn.preprocessing.normalize(f1_emb)
                    for n, v in cur.items():
                        v = np.array(v)
                        v_emb = v.reshape(1, 256)
                        embed2 = sklearn.preprocessing.normalize(v_emb)
                        diff = np.subtract(embed1, embed2)
                        dist = np.sum(np.square(diff), 1)

                        d = np.dot(
                            v, f1) / (np.linalg.norm(v) * np.linalg.norm(f1))
                        print(
                            "name: %s total cosin distance %f and Euclidean distance %f"
                            % (n, d, dist))
                        if dist < d1:
                            d1 = dist
                            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, "time cost:" + '%.04f' % 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)
                # 如果输入q则退出循环
                if (k & 0xFF == ord('q') or count == num):
                    break
            else:
                print('device not find')
                break
        cap.release()
        cv2.destroyAllWindows()
Esempio n. 26
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)
Esempio n. 27
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()
Esempio n. 28
0
stride = 2
slide_window = False
shuffle = False
detectors = [None, None, None]
show_predictions = False

#epoch = [30, 22, 22]
epoch = [70, 70, 70]
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(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 in ["RNet", "ONet"]:
    RNet = Detector(RNet_factory, 24, batch_size[1], model_path[1])
    detectors[1] = RNet

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

mtcnn_detector = MtcnnDetector(detectors=detectors,
                               min_face_size=min_face_size,
                               stride=stride,
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, 64, 16]
model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]

test_mode = 'ONet'

# load P-net 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 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,
Esempio n. 30
0
def run(prefix,
        epoch,
        batch_size,
        output_dir,
        test_mode="PNet",
        thresh=[0.3, 0.1, 0.7],
        min_face_size=20,
        stride=2,
        slide_window=False,
        shuffle=False,
        vis=False):
    data_dir = '../../DATA/WIDER_val/images'
    anno_file = 'wider_face_val.txt'
    output_file = output_dir
    if not os.path.exists(output_file):
        os.mkdir(output_file)

    test_mode = test_mode
    thresh = thresh
    min_face_size = min_face_size
    stride = stride
    slide_window = slide_window
    shuffle = shuffle
    vis = vis
    detectors = [None, None, None]
    # prefix is the model path
    prefix = prefix
    epoch = epoch
    batch_size = batch_size
    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)

    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])
            if not os.path.exists(save_path):
                os.mkdir(save_path)
            print('current path:', current_event)

        # generate detection
        img = cv2.imread(image_file_name)
        all_boxes, _ = mtcnn_detector.detect_single_image(img)

        f_name = item[1].split('.jpg')[0]

        dets_file_name = os.path.join(save_path, f_name + '.txt')
        fid = open(dets_file_name, 'w')
        boxes = all_boxes[0]
        if boxes is None:
            fid.write(item[1] + '\n')
            fid.write(str(1) + '\n')
            fid.write('%f %f %f %f %f\n' % (0, 0, 0, 0, 0.99))
            continue
        fid.write(item[1] + '\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]))

        fid.close()
        if idx % 10 == 0:
            print(idx)