Пример #1
0
def get_feature(img):
    parser = argparse.ArgumentParser(description='Create user')
    parser.add_argument('--image-size', default='112,112', help='')
    parser.add_argument('--model',
                        default='./models/model-r50-am-lfw/model,0',
                        help='path to load model.')
    parser.add_argument('--gpu', default=0, type=int, help='gpu id')
    parser.add_argument('--det',
                        default=2,
                        type=int,
                        help='mtcnn option, 2 means using R+O, else using O')
    parser.add_argument('--flip',
                        default=0,
                        type=int,
                        help='whether do lr flip aug')
    parser.add_argument('--threshold',
                        default=1.24,
                        type=float,
                        help='ver dist threshold')

    args = parser.parse_args()
    model = face_embedding.FaceModel(args)

    feature = model.get_feature(img)
    return feature
Пример #2
0
def getDistSim(args):
    model = face_embedding.FaceModel(args.gpuid)
    img = cv2.imread(args.image1)
    f1 = getEmbedding(model, img)
    img = cv2.imread(args.image2)
    f2 = getEmbedding(model, img)
    dist = np.sum(np.square(f1 - f2))
    sim = np.dot(f1, f2.T)
    return dist, sim
Пример #3
0
def getDistSim(args):
    model = face_embedding.FaceModel(args.gpuid)
    img = cv2.imread(args.image1)
    f1 = model.get_initial_feature(img)
    img = cv2.imread(args.image2)
    f2 = model.get_initial_feature(img)
    dist = np.sum(np.square(f1-f2))
    sim = np.dot(f1, f2.T)
    return dist, sim
Пример #4
0
    def loadModel(self):
        self.model = face_embedding.FaceModel(self.args)
        detectors = [None, None, None]
        ctx = mx.gpu(0)
        prefix = ['mtcnnmodel/pnet', 'mtcnnmodel/rnet', 'mtcnnmodel/onet']
        epoch = [16, 16, 16]
        batch_size = [2048, 256, 16]
        thresh = [0.6, 0.6, 0.7]
        min_face_size = 24
        stride = 2
        slide_window = False
        # load pnet model
        args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
        if slide_window:
            PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
        else:
            PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
        detectors[0] = PNet

        # load rnet model
        args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
        RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
        detectors[1] = RNet

        # load onet model
        args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
        ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
        detectors[2] = ONet

        self.mtcnn_detector = MtcnnDetector(detectors=detectors,
                                            ctx=ctx,
                                            min_face_size=min_face_size,
                                            stride=stride,
                                            threshold=thresh,
                                            slide_window=slide_window)
        #print (self.model)
        self.id_dataset, self.idnums = self.get_id_data(self.args.id_dir)
Пример #5
0
parser.add_argument('--model',
                    default='../models/model-r34-amf/model,0',
                    help='path to load model.')
parser.add_argument('--gpu', default=0, type=int, help='gpu id')
parser.add_argument('--det',
                    default=2,
                    type=int,
                    help='mtcnn option, 2 means using R+O, else using O')
parser.add_argument('--flip',
                    default=0,
                    type=int,
                    help='whether do lr flip aug')
parser.add_argument('--threshold',
                    default=1.24,
                    type=float,
                    help='ver dist threshold')
args = parser.parse_args()

model = face_embedding.FaceModel(args)
#img = cv2.imread('/raid5data/dplearn/lfw/Jude_Law/Jude_Law_0001.jpg')
img = cv2.imread(
    '/raid5data/dplearn/megaface/facescrubr/112x112/Tom_Hanks/Tom_Hanks_54745.png'
)

time_now = datetime.datetime.now()
for i in xrange(3000):
    f1 = model.get_feature(img)
time_now2 = datetime.datetime.now()
diff = time_now2 - time_now
print(diff.total_seconds() / 3000)
Пример #6
0
    parser = argparse.ArgumentParser(description='mapr consumer settings')
    parser.add_argument('--groupid', default='dong00', help='mapr consumer to read from')
    parser.add_argument('--gpuid', default='0', type=int, help='')
    parser.add_argument('--readstream', default='/tmp/rawvideostream', help='')
    parser.add_argument('--writestream1', default='/tmp/processedvideostream', help='')
    parser.add_argument('--writestream2', default='/tmp/identifiedstream', help='')
    parser.add_argument('--writetopic1', default='topic1', help='topic to write to')
    parser.add_argument('--writetopic2', default='all', help='topic to write to')
    parser.add_argument('--readtopic', default='topic1', help='topic to write to')
    args = parser.parse_args()

    ctx = mx.gpu(args.gpuid)
    _, arg_params, aux_params = mx.model.load_checkpoint('mxnet-face-fr50', 0)
    arg_params, aux_params = ch_dev(arg_params, aux_params, ctx)
    sym = resnet_50(num_class=2)
    model = face_embedding.FaceModel(args.gpuid)

    c = Consumer({'group.id': args.groupid,
              'default.topic.config': {'auto.offset.reset': 'latest', 'enable.auto.commit': 'false'}})
    c.subscribe([args.readstream+':'+args.readtopic])
    running = True
    p = Producer({'streams.producer.default.stream': args.writestream2})
    p_orig = Producer({'streams.producer.default.stream': args.writestream1})

    while running:
        msg = c.poll(timeout=0)
        if msg is None: continue
        if not msg.error():
            nparr = np.fromstring(msg.value(), np.uint8)
            img_orig = cv2.imdecode(nparr, 1)
            img, scale = resize(img_orig.copy(), 600, 1000)
Пример #7
0
def kafkastream():
    if args.gpuid >= 0:
        ctx = mx.gpu(args.gpuid)
    else:
        ctx = mx.cpu()
    _, arg_params, aux_params = mx.model.load_checkpoint('mxnet-face-fr50', 0)
    arg_params, aux_params = ch_dev(arg_params, aux_params, ctx)
    sym = resnet_50(num_class=2)
    model = face_embedding.FaceModel(args.gpuid)

    f1T = get_face_embedding(args.filename, arg_params, aux_params, sym, model,
                             ctx)

    c = Consumer({
        'group.id': args.groupid,
        'default.topic.config': {
            'auto.offset.reset': 'earliest',
            'enable.auto.commit': 'false'
        }
    })
    c.subscribe([args.readstream + ':' + args.readtopic])
    running = True
    p = Producer({'streams.producer.default.stream': args.writestream})

    while running:
        msg = c.poll(timeout=0)
        if msg is None: continue
        if not msg.error():
            pickle_vector = pickle.loads(msg.value())
            nparr = np.fromstring(pickle_vector[0], np.uint8)
            img_orig = cv2.imdecode(nparr, 1)

            bbox_vector = pickle_vector[1]
            print(len(bbox_vector))
            embedding_vector = pickle_vector[2]
            if len(embedding_vector) > 0:
                sim_vector = [np.dot(f, f1T) for f in embedding_vector]
                idx = sim_vector.index(max(sim_vector))
                bbox = bbox_vector[idx]
                sim = sim_vector[idx]
                if sim > args.threshold:
                    img = cv2.cvtColor(img_orig, cv2.COLOR_RGB2BGR)
                    cv2.rectangle(img,
                                  (int(round(bbox[0])), int(round(bbox[1]))),
                                  (int(round(bbox[2])), int(round(bbox[3]))),
                                  (0, 255, 0), 2)
                    ret, jpeg = cv2.imencode('.png', img)
                    bytecode = jpeg.tobytes()
                    time.sleep(args.timeout)
                    yield (b'--frame\r\n'
                           b'Content-Type: image/png\r\n\r\n' + bytecode +
                           b'\r\n\r\n')
                    if args.writetostream:
                        p.produce(args.writetopic, jpeg.tostring())
                        print(args.writetopic)
        elif msg.error().code() != KafkaError._PARTITION_EOF:
            print(msg.error())
            running = False

    c.close()
    p.flush()
Пример #8
0
        if not label in ret_map:
            ret_map[label] = []
        for name in vec[1:]:
            assert name not in gt_label
            assert name in valid_name
            if name not in name2path:
                continue
            gt_label[name] = label
            gt_map[label].append(name)

    train_filename = args.output
    assert not os.path.exists(train_filename)
    f = open(train_filename, 'wb')
    if model is None:
        model = face_embedding.FaceModel(model=args.model,
                                         gpu_id=args.gpu,
                                         feature_norm=True)
    vid = 0
    for line in open(os.path.join(args.dataset, 'gt_v2', 'train_v2.txt'), 'r'):
        name, label = line.strip().split()
        label = int(label)
        if label > MAX_LABEL:
            continue
        #if name not in valid_name:
        #  continue
        if name not in name2path:
            continue
        vid += 1
        namehash = hash(name)
        mod = namehash % SPLIT[1]
        #print(namehash, mod)
Пример #9
0
def main(_):
    # tf.logging.set_verbosity(tf.logging.INFO)
    with tf.Graph().as_default():
        # tf_global_step = slim.get_or_create_global_step()

        ####################
        # Select the model #
        ####################
        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=(FLAGS.num_classes),
            is_training=False)

        #####################################
        # Select the preprocessing function #
        #####################################
        preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
        image_preprocessing_fn = preprocessing_factory.get_preprocessing(
            preprocessing_name,
            is_training=False)
        test_image_size = FLAGS.test_image_size or network_fn.default_image_size

        if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
            checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
        else:
            checkpoint_path = FLAGS.checkpoint_path

        tensor_input = tf.placeholder(tf.float32, [1, test_image_size, test_image_size, 3])
        logits, _ = network_fn(tensor_input)
        logits = tf.nn.top_k(logits, 1)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        # test_ids = [line.strip() for line in open(FLAGS.test_list)]
        # tot = len(test_ids)
        # results = list()

        #################################
        # Load face detector model RFCN #
        #################################
        prototxt = '/home/disk3/py/py-R-FCN/models/pascal_voc/ResNet-101/rfcn_end2end/test_agonistic_face.prototxt'
        #caffemodel = os.path.join( args.demo_net )
        caffemodel = '/home/disk3/py/py-R-FCN/data/rfcn_models/resnet101_rfcn_ohem_iter_40000.caffemodel'
        detect_cfg.TEST.HAS_RPN = True
        caffe.set_mode_gpu()
        caffe.set_device(0)
        detect_cfg.GPU_ID = 0
        net = caffe.Net(prototxt, caffemodel, caffe.TEST)
        print ('\n\nLoaded network {:s}'.format(caffemodel))

        ##################################
        # Load landmark detect and align #
        ##################################
        fa = face_align.FaceAlignment(face_align.LandmarksType._2D, enable_cuda=True)

        ####################
        # Load InsightFace #
        ####################
        parser = argparse.ArgumentParser(description='face model test')
        parser.add_argument('--image-size', default='112,112', help='')
        parser.add_argument('--model', default='./model-r50-am-lfw/model,0', help='path to load model.')
        parser.add_argument('--gpu', default=0, type=int, help='gpu id')
        parser.add_argument('--det', default=2, type=int, help='mtcnn option, 2 means using R+O, else using O')
        parser.add_argument('--flip', default=0, type=int, help='whether do lr flip aug')
        args = parser.parse_args()
        model = face_embedding.FaceModel(args)

        ###############################
        # Deploy file path and params #
        ###############################
        cfg = Config()
        # feat_box = dict()
        # output = open('result_txt/result_align_mul_2.txt', 'w')
        # output.write('total leaders: {}'.format(len(cfg.leaders_sml[:-1])))
        # countDet = 0
        # countData = [0, 0]
        # no_label = 0
        # right = dict()
        # FN = dict()
        # FP = dict() 
        # precision = dict()
        # recall = dict()
        # score = dict()
        # result = dict()
        # num = 0

        # true_profile = dict() 
        # true_frontal = dict()
        # false_profile = dict()
        # false_frontal = dict()

        for leader in cfg.leaders_sml[:-1]:
            save_root = '{}/{}/'.format(cfg.save_root, leader)
            f1 = cfg.BaseFeat(leader)
            feat_box[leader] = f1

        for threshold in range(60, 130, 5):
            threshold = threshold * 0.01
            right[threshold] = [0, 0]
            FN[threshold] = 0
            FP[threshold] = 0
            score[threshold] = 0
            precision[threshold] = 0
            recall[threshold] = 0
            true_frontal[threshold] = 0
            false_frontal[threshold] = 0
            true_profile[threshold] = 0
            false_profile[threshold] = 0
            result[threshold] = 'OTHERS'

        with tf.Session(config=config) as sess:
            sess.run(tf.global_variables_initializer())
            saver = tf.train.Saver()
            saver.restore(sess, checkpoint_path)

            ###############################
            # Detect image Loop by leader #
            ###############################
            for leader in ['DXP']:
                output.write(leader)
                # result2/DXP/DXP.txt
                label_file_path = '{0}/{1}/{1}.txt'.format(cfg.label_path_root, leader)
                # txts/DXP/
                bbs_path = '{}/{}/'.format(cfg.bbs_path_root, leader)
                # raw/DXP-craw/
                img_path = '{}/{}-craw/'.format(cfg.img_path_root, leader)
                # detect_result/DXP/
                save_root = '{}/{}/'.format(cfg.save_root, leader)
                for label in open(label_file_path):
                    if len(label) <= 10:
                        continue
                    print(label)
                    img_name = label.split(';')[0]+'.jpg'
                    # load image
                    img_cv = cv2.imread(img_path + img_name)
                    if img_cv is None:
                        img_name = img_name[:-4]+'.JPG'
                        img_cv = cv2.imread(img_path + img_name)
                    img_tf = open(img_path + img_name, 'rb').read()
                    img_tf = tf.image.decode_jpeg(img_tf, channels=3)
                    ###############################
                    # Detect all faces in a image #
                    ###############################
                    scores, boxes = im_detect(net, img_cv)
                    height, width, c = img_cv.shape
                    thresh = 0.8
                    bbox = []
                    for cls_ind, cls in enumerate(['face']):
                        cls_ind += 1 # because we skipped background
                        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
                        cls_scores = scores[:, cls_ind]
                        dets = np.hstack((cls_boxes,
                                          cls_scores[:, np.newaxis])).astype(np.float32)
                        keep = nms(dets, 0.3)
                        dets = dets[keep, :]
                        inds = np.where(dets[:, -1] >= thresh)[0]
                        for i in inds:
                            bbox.append(dets[i, :4])
                    num += len(bbox)

                    # Get groundtruth of one image 
                    # bbs: '[x, y, w, h, num, leader]'
                    bbs, bbs_other = utils.get_bbox(bbs_path, label, default_leader = leader)
                    countData[0] += len(bbs) # 
                    countData[1] += len(bbs_other)
                    # new_bbs_txt = open(bbs_path + img_name[:-4] + '_new.txt', 'w')

                    for d in bbox:
                        bound1 = (float(d[0]), float(d[1]), float(d[0])+float(d[2]), float(d[1])+ float(d[3]))
                        ## face landmark detection
                        preds = fa.get_landmarks(img_path + img_name, bound1)[-1]
                        ## face embedding 512d
                        f = model.get_feature_by_landmark(img_cv, bound1, preds)

                        # d = [int(round(d[0])), int(round(d[1])), int(round(d[2])), int(round(d[3]))]
                        bound1 = (max(0, bound1[0]), max(0, bound1[1]),
                                  min(bound1[2], width), min(bound1[3], height))

                        countDet += 1
                        for s in score:
                            score[s] = 999
                            result[s] = 'OTHERS'
                        for feat_leader in feat_box:
                            for f2 in feat_box[feat_leader]:
                                dist = np.sum(np.square(f - f2))
                                for threshold in range(60, 130, 5):
                                    threshold = threshold * 0.01
                                    if dist < threshold and dist < score[threshold]:
                                        score[threshold] = dist
                                        result[threshold] = feat_leader
                        iou = 0
                        positive = 'OTHERS'
                        pose = -1
                        for bb in (bbs + bbs_other):
                            bound2 = (float(bb[0]), float(bb[1]),
                                      float(bb[0]) + float(bb[2]), float(bb[1])+float(bb[3]))
                            iou = calculateIoU(bound1, bound2)
                            if iou >= 0.5:
                                positive = bb[5]
                                pose = bb[6]
                                break
                        if iou < 0.5:
                            print("the face no label")
                            # cv2.putText(img_cv, str(i) + 'no label', (bound1[0], bound1[1]), 0, 1, (255, 0, 0))
                            # cv2.imwrite(save_root +'no_label/' +img_name + '.jpg', img_cv)
                            no_label += 1
                            continue
                        for threshold in range(60, 130, 5):
                        threshold = threshold * 0.01
                        if result[threshold] == positive: 
                            if result[threshold] == 'OTHERS':
                                right[threshold][1] += 1
                                if pose == 0:
                                    true_frontal[threshold] += 1
                                else pose == 1:
                                    true_profile[threshold] += 1
                            else:
                                right[threshold][0] += 1
                                if pose == 0:
                                    true_frontal[threshold] += 1
                                else pose == 1:
                                    true_profile[threshold] += 1

                                
                        elif positive != 'OTHERS': 
                            # cv2.putText(img_cv, str(i) + result[threshold] + ' ' + positive, (bound1[0], bound1[1]), 0, 0.5, (0, 255, 0))
                            # cv2.imwrite(save_root + 'falsePositive/' + img_name + '.jpg', img_cv)
                            FP[threshold] += 1
                            if pose == 0:
                                false_frontal[threshold] += 1
                            else pose == 1:
                                false_profile[threshold] += 1
                        else: # result != 'OTHERS': 
                            FN[threshold] += 1
                            if pose == 0:
                                false_frontal[threshold] += 1
                            else pose == 1:
                                false_profile[threshold] += 1