Ejemplo n.º 1
0
def face_search(request_id, b64_data, group_id='DEFAULT', max_user_num=5):
    # 最多返回5个相似用户
    max_user_num = min(5, max_user_num)

    # 并行获取特征值  --  分别检测人脸, 人脸角度可以分别调整
    #all_encodings, face_locations = predict_parallel(get_features_thread_db, b64_data, group_id, 
    #        request_id=request_id, classifier='api')

    # 并行获取特征值  --  只检测人脸一次,人脸角度要一起调整 (由ALGORITHM['evo']['p_angle']确定)
    all_encodings, face_locations, face_array = verify.get_features_b64_parallel(b64_data, request_id=request_id)

    if len(face_locations)==0: # 未取得特征值
        return []

    # 先使用knn分类器搜索(临时特征库)
    predictions = predict_parallel(predict_thread_db, all_encodings, group_id, 
            request_id=request_id, classifier='knn', data_type='encodings')
    # 如果未找到,再使用深度网络分类器(全量特征库)
    if len(predictions)==0 or predictions[0][0]=='unknown':
        print('search using keras classifier')
        plus_encodings = [all_encodings['vgg']['None']+all_encodings['evo']['None']]
        predictions = predict_K(plus_encodings, group_id,
            model_path=TRAINED_MODEL_PATH, 
            face_algorithm="plus",
            data_type="encodings")

        # 如果仍未识别,返回空
        if len(predictions)==0 or predictions[0][0]=='unknown':
            return []

    # 准备返回结果
    user_list = []
    for i in range(min(max_user_num, len(predictions))):
        user_id, box, score, _ = predictions[i]
        info = user_info(group_id, user_id)
        user_list.append({
            'user_id'     : user_id,
            'mobile_tail' : info['mobile'][:-4], # 手机后4位
            'name'        : info['name'], # 用户姓名
            'location'    : face_locations[0], # 只有一个人脸坐标
            'score'       : score,
        })

    # 只记录有结果的人脸数据,用于后面数据增强, 图片在预测时已保存
    if len(user_list)>0:
        face_save_to_temp(group_id, request_id, 'face_search', user_list, image=face_array[0])
        #face_save_to_temp(group_id, request_id, 'face_search', user_list)

    return user_list
Ejemplo n.º 2
0
        sys.exit(2)

    test_thing = sys.argv[1]

    if os.path.isdir(test_thing):
        images = os.listdir(test_thing)
        images = [os.path.join(test_thing, i) for i in images]
    else:
        images = [test_thing]

    # Using the trained classifier, make predictions for unknown images
    for image_file in images:
        print("Looking for faces in {}".format(image_file))

        # Find all people in the image using a trained classifier model
        # Note: You can pass in either a classifier file name or a classifier model instance

        start_time = datetime.now()
        predictions = predict_parallel(predict_thread, image_file)
        print('[Time taken: {!s}]'.format(datetime.now() - start_time))

        # Print results on the console
        for name, (top, right, bottom, left), distance, count in predictions:
            print("- Found {} at ({}, {}), distance={}, count={}".format(
                name, left, top, distance, count))
        if len(predictions) == 0:
            print('Face not found!')

        # Display results overlaid on an image
        #knn.show_prediction_labels_on_image(image_file, predictions)
Ejemplo n.º 3
0
    # Using the trained classifier, make predictions for unknown images
    for image_file in images:
        print("Looking for faces in {}".format(image_file))

        with open(image_file, 'rb') as f:
            image_data = f.read()

        image_b64 = base64.b64encode(image_data)

        # Find all people in the image using a trained classifier model
        # Note: You can pass in either a classifier file name or a classifier model instance
        
        #predictions = api_func.face_search('', image_b64, group_id)

        #start_time = datetime.now()
        predictions = predict_parallel(predict_thread_db, image_b64, group_id, classifier=classifier)
        #print('[Time taken: {!s}]'.format(datetime.now() - start_time))

        # Print results on the console
        for name, (top, right, bottom, left), distance, count in predictions:
            print("- Found {} at ({}, {}), distance={}, count={}".format(name, left, top, distance, count))
        #for i in predictions:
        #    print("- Found {} at {}, distance={}".format(i['user_id'], i['location'], i['score']))
        if len(predictions)==0:
            print('Face not found!')

        #print(predictions)
        # Display results overlaid on an image
        #knn.show_prediction_labels_on_image(image_file, predictions)

Ejemplo n.º 4
0
        correct = 0
        wrong = 0
        fail = 0
        multi = 0  # 匹配多个结果
        second = 0  # 不是首个匹配结果
        start_time = datetime.now()
        for face in faces_list[-last_face_num:]:  # 指定人的倒数几个人脸,用于使用train做测试
            # 识别每个人脸
            r = dbport.face_info(face)
            if r is None:
                continue

            # 并行识别
            predictions = predict_parallel(predict_thread_db,
                                           r['encodings'],
                                           group_id,
                                           'encodings',
                                           classifier=classifier)

            # Print results on the console
            if len(predictions) == 0:
                fail += 1
            else:
                n = 0
                bingo = 0
                name_list = []
                for name, (top, right, bottom,
                           left), distance, count in predictions:
                    if name == p:
                        if n == 0:
                            correct += 1