def upload():
    if request.method == 'POST':
        f = request.files['file']
        cont = f.read()
        buf = np.frombuffer(cont, dtype=np.byte)
        # print('buf:size is {}:{} '.format(buf, np.shape(buf)))
        print('buf {} and buf size {}'.format(buf, np.shape(buf)))
        img = np.expand_dims(cv2.imdecode(buf, cv2.IMREAD_COLOR), axis=0)
        print('input image shape ', np.shape(img))
        emb = faceNet_serving_V0.img_to_emb_feature(img, FACENET_CHANNEL)

        print('emb size ', len(emb))
        ret = {"file": f.filename, "emb": list(emb)}
        # print('return: ', ret)

    return json.dumps(ret)
Exemplo n.º 2
0
def upload():
    if request.method == 'POST':
        try:
            f = request.files['file']  # 301 file error
            cont = f.read()
            buf = np.frombuffer(cont, dtype=np.byte)
            img = cv2.imdecode(buf, cv2.IMREAD_COLOR)
            # print('image size', np.shape(img))
            faces, _, det_arr = load_and_align_data(img)
            print('face shape', np.shape(faces))
            print('bounding boxes', det_arr)
            if faces is None:
                print('No Faces in this image!')
                ret = {
                    "file": f.filename,
                    "code": 200,
                    "message": "Has_no_faces",
                    "result": "合规"
                }
            else:
                det_arr_ser = np.reshape(det_arr, (1, np.size(det_arr)))
                emb = faceNet_serving_V0.img_to_emb_feature(faces, FACENET_CHANNEL)
                print('embedding shape ', np.shape(emb))
                emb = list(emb)
                num_face = int(len(emb)/128)
                ret_not_pass = {}
                data = []

                for i in range(num_face):
                    emb_face = emb[i*128: (1+i)*128]
                    likely = cal_sim_new(emb_face, emb_dict)
                    theta = {'xijinping': [0.75],
                             'hujintao': [0.73],
                             'jiangzemin': [0.72],
                             'dengxiaoping': [0.60],
                             'wenjiabao': [0.71],
                             'maozedong': [0.85],
                             'zhouenlai': [0.78]}
                    diff_name = list([i for i, _ in theta.items()])
                    value = []
                    for name in diff_name:
                        theta[name].append(likely[name])
                        value.append(theta[name])
                    value = np.squeeze(value)
                    # print('dictionary theta', theta)
                    Flag_all = listsbigger(value[:,0], value[:,1])
                    if all(Flag_all):
                        value_max = np.max(value[:,1])
                        value_max_indice = np.argmax(value[:,1])
                        value_max_name = diff_name[value_max_indice]
                        ret_pass = {
                            "file": f.filename,
                            "code": 300,
                            "message": "Has_face_pass",
                            "result":  "合规",
                            "user_name": str(value_max_name),
                            "score": str(value_max),
                            "det_arr": det_arr_ser.tolist()
                        }
                    else:
                        index_False = [i for i, x in enumerate(Flag_all) if not x]
                        max_term_tmp = value[index_False, :]
                        delta_score = max_term_tmp[:, 1] - max_term_tmp[:, 0]
                        delta_score_max_indice = np.argmax(delta_score)
                        max_term = max_term_tmp[delta_score_max_indice, 1]
                        max_name_tmp = [diff_name[i] for i in index_False]
                        max_name = max_name_tmp[delta_score_max_indice]
                        # print('max_name {} and its value {}'.format(max_name, max_term))
                        det_arr_tmp = np.array(det_arr)[i, :]
                        det_arr_ser = np.reshape(det_arr_tmp, (1, np.size(det_arr_tmp)))

                        data.append({
                                "face_id": str(i),
                                "user_name": str(max_name),
                                "score": str(max_term),
                                "det_arr": det_arr_ser.tolist()
                        })

                        ret_not_pass = {
                                "file": f.filename,
                                "code": 301,
                                "message": "敏感人物",
                                "result": "不合规",
                                "data": data,
                        }

                if not bool(ret_not_pass):
                    ret = ret_pass
                else:
                    ret = ret_not_pass
                print(ret)
        except Exception as e:
            print('#cv encoder error!')
            ret = {"file": f.filename,
                   "code": 301,
                   "message": "encoder_failed",
                   "result": "PASS"}
        return json.dumps(ret)
Exemplo n.º 3
0
def main():
    # img = cv2.imdecode(buf, cv2.IMREAD_COLOR)
    img = cv2.imread('F:/peoples_baidu/xijinping_baidu/10.jpg', cv2.IMREAD_COLOR)

    faces, det_arr, bb_int = load_and_align_data(img)

    for i in range(np.shape(faces)[0]):
        # det_arr_plt = np.zeros((1,4), dtype=np.int32)
        det_arr_plt = det_arr[i].astype(np.int32)
        bb_int = bb_int[i]
        cv2.imshow('img', img)
        cv2.rectangle(img, (det_arr_plt[0],det_arr_plt[1]), (det_arr_plt[2],det_arr_plt[3]), (0,255,0))
        cv2.rectangle(img, (bb_int[0], bb_int[1]),(bb_int[2],bb_int[3]), (255, 0, 255))
        cv2.waitKey()

        cv2.imshow('face', faces[i, :, : ,:])
        cv2.waitKey()

    if faces is None:
        print('No Faces in this image!')
        ret = {
            "code": 200,
            "message": "Has_no_faces",
            "result": "合规",
        }
    else:
        det_arr_ser = np.reshape(det_arr, (1, np.size(det_arr)))
        print('input face shape ', np.shape(faces))

        emb = faceNet_serving_V0.img_to_emb_feature(faces, FACENET_CHANNEL)

        print('emb size', len(emb))
        emb = list(emb)
        num_face = int(len(emb)/512)
        ret = {}
        maximum = []
        maximum_name = ['test']
        for i in range(num_face):
            emb_face = emb[i*512: (1+i)*512]
            likely = cal_sim_new(emb_face, data_ave)
            print('Likely ', likely)
            maximum_name.append(max(likely, key=likely.get))
            maximum.append(likely[max(likely, key=likely.get)])
            th = 0.40

            if max(maximum) < th:
                ret = {
                    "code": 300,
                    "message": "Has_face_pass",
                    "result":  "合规",
                    "det_arr": det_arr_ser.tolist(),
                }
            else:
                index = list(np.where(np.array(maximum) > th)[0])
                det_arr_tmp = np.array(det_arr)[index, :]
                det_arr_ser = np.reshape(det_arr_tmp, (1, np.size(det_arr_tmp)))

                data = []
                for ii in index:
                    data.append(
                        {
                            "face_id": str(ii),
                            "user_name": maximum_name[ii+1],
                            "score": maximum[ii]
                        })

                ret = {
                        "code": 301,
                        "message": "敏感人物",
                        "result": "不合规",
                        "data": data,
                        "det_arr": det_arr_ser.tolist()
                }
    return json.dumps(ret)
Exemplo n.º 4
0
def upload():
    if request.method == 'POST':
        f = request.files['file']  # 301 file error
        cont = f.read()
        buf = np.frombuffer(cont, dtype=np.byte)
        img = cv2.imdecode(buf, cv2.IMREAD_COLOR)

        faces, det_arr = load_and_align_data(img)
        print('file {} have #{} faces'.format(f, len(det_arr)))
        if faces is None:
            print('No Faces in this image!')
            ret = {
                "file": f.filename,
                "code": 200,
                "message": "Has_no_faces",
                "result": "合规"
            }
        else:
            det_arr_ser = np.reshape(det_arr, (1, np.size(det_arr)))
            emb = faceNet_serving_V0.img_to_emb_feature(faces, FACENET_CHANNEL)
            emb = list(emb)
            num_face = int(len(emb) / 128)
            ret = {}
            maximum = []
            maximum_name = ['test']
            for i in range(num_face):
                emb_face = emb[i * 128:(1 + i) * 128]
                likely = cal_sim_new(emb_face, emb_data)
                maximum_name.append(max(likely, key=likely.get))
                maximum.append(likely[max(likely, key=likely.get)])
                th = 0.40

                if max(maximum) < th:
                    ret = {
                        "file": f.filename,
                        "code": 300,
                        "message": "Has_face_pass",
                        "result": "合规",
                        "det_arr": det_arr_ser.tolist()
                    }
                else:
                    index = list(np.where(np.array(maximum) > th)[0])
                    det_arr_tmp = np.array(det_arr)[index, :]
                    det_arr_ser = np.reshape(det_arr_tmp,
                                             (1, np.size(det_arr_tmp)))

                    data = []
                    for ii in index:
                        data.append({
                            "face_id": str(ii),
                            "user_name": maximum_name[ii + 1],
                            "score": maximum[ii]
                        })

                    ret = {
                        "file": f.filename,
                        "code": 301,
                        "message": "敏感人物",
                        "result": "不合规",
                        "data": data,
                        "det_arr": det_arr_ser.tolist()
                    }
    return json.dumps(ret)