def face_recognition_image(model_path, dataset_path, filename, image_path):
    # 加载数据库的数据
    dataset_emb, names_list = load_dataset(dataset_path, filename)
    # 初始化mtcnn人脸检测
    face_detect = face_recognition.Facedetection()
    # 初始化facenet
    face_net = face_recognition.facenetEmbedding(model_path)

    image = image_processing.read_image_gbk(image_path)
    # 获取 判断标识 bounding_box crop_image
    bboxes, landmarks = face_detect.detect_face(image)
    bboxes, landmarks = face_detect.get_square_bboxes(bboxes,
                                                      landmarks,
                                                      fixed="height")
    if bboxes == [] or landmarks == []:
        print("-----no face")
        exit(0)
    print("-----image have {} faces".format(len(bboxes)))
    face_images = image_processing.get_bboxes_image(image, bboxes,
                                                    resize_height,
                                                    resize_width)
    face_images = image_processing.get_prewhiten_images(face_images)
    pred_emb = face_net.get_embedding(face_images)
    pred_name, pred_score = compare_embadding(pred_emb, dataset_emb,
                                              names_list)
    # 在图像上绘制人脸边框和识别的结果
    show_info = [n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score)]
    image_processing.show_image_bboxes_text("face_recognition", image, bboxes,
                                            show_info)
def face_recognition_video(model_path,dataset_path, filename,video_path):
    # 加载数据库的数据
    dataset_emb, names_list = load_dataset(dataset_path, filename)
    # 初始化mtcnn人脸检测
    face_detect = face_recognition.Facedetection()
    # 初始化facenet
    face_net = face_recognition.facenetEmbedding(model_path)

    cap = cv2.VideoCapture(video_path)  # 获取视频数据
    face_cnt = 0  # 控制视频读取时候,每秒截取指定数量图片进行一次识别
    while cap.isOpened():
        ok, frame = cap.read()
        face_cnt += 1
        if not ok:
            break
        if(face_cnt % 5 == 0):
            frame = image_processing.read_image1(frame)
            bboxes, landmarks = face_detect.detect_face(frame)
            bboxes, landmarks = face_detect.get_square_bboxes(bboxes, landmarks, fixed="height")
            if bboxes == [] or landmarks == []:
                print("-----no face")
                continue
            print("-----image have {} faces".format(len(bboxes)))
            face_images = image_processing.get_bboxes_image(frame, bboxes, resize_height, resize_width)
            face_images = image_processing.get_prewhiten_images(face_images)
            pred_emb = face_net.get_embedding(face_images)
            pred_name, pred_score = compare_embadding(pred_emb, dataset_emb, names_list)
            # 在图像上绘制人脸边框和识别的结果
            show_info = [n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score)]
            print("showinfo:", show_info)
            image_processing.show_image_bboxes_text("face_recognition", frame, bboxes, show_info)

    cap.release()
    cv2.destroyWindow("face_recognition")
Beispiel #3
0
def face_recognition_image(model_path, dataset_path, filename, image_path):
    # 加载数据库的数据
    dataset_emb, names_list = load_dataset(dataset_path, filename)

    # 初始化mtcnn人脸检测
    face_detect = face_recognition.FaceDetection()

    # 初始化facenet
    face_net = face_recognition.facenetEmbedding(model_path)

    # 读取待检图片
    image = image_processing.read_image_gbk(image_path)
    print("image_processing.read_image_gbk:", type(image),
          image.shape)  # <class 'numpy.ndarray'>, (616, 922, 3),(高,宽,通道)

    # 获取 判断标识 bounding_box crop_image
    bboxes, landmarks = face_detect.detect_face(image)
    bboxes, landmarks = face_detect.get_square_bboxes(
        bboxes, landmarks, fixed="height")  # 以高为基准,获得等宽的举行
    if bboxes == [] or landmarks == []:
        print("-----no face")
        exit(0)
    print("-----image have {} faces".format(len(bboxes)))

    face_images = image_processing.get_bboxes_image(
        image, bboxes, resize_height, resize_width)  # 按照bboxes截取矩形框
    face_images = image_processing.get_prewhiten_images(face_images)  # 图像归一化
    pred_emb = face_net.get_embedding(face_images)  # 获取facenet特征
    pred_name, pred_score = compare_embadding(pred_emb, dataset_emb,
                                              names_list)

    # 在图像上绘制人脸边框和识别的结果
    show_info = [n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score)]
    image_processing.show_image_bboxes_text("face_reco", image, bboxes,
                                            show_info)
def batch_detect_image(image_list, class_names, prob_threshold, show=False):
    predictor = select_net(model_path, net_type, len(class_names))
    for image_path in image_list:
        orig_image = cv2.imread(image_path)
        rgb_image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
        boxes, labels, probs = predict_image(predictor, rgb_image,
                                             prob_threshold)
        label_names = file_processing.decode_label(labels,
                                                   name_table=class_names)
        info = image_processing.combile_label_prob(label_names, probs)
        print("predict:{}".format(info))
        if show:
            image_processing.show_image_bboxes_text("image", rgb_image, boxes,
                                                    info)
Beispiel #5
0
def linedataset_test(filename, image_dir=None, show=True):
    '''
    label_data:[image_path,boxes_nums,x1, y1, w, h, label_id,x1, y1, w, h, label_id]
    :param filename:
    :param image_dir:
    :param show:
    :return:
    '''
    with open(filename) as f:
        lines = f.readlines()
        for line in lines:
            image_id, box, label = face_body.read_line_image_label(line)
            if show:
                if image_dir:
                    image_path = os.path.join(image_dir, image_id)
                else:
                    image_path = image_id
                image = image_processing.read_image(image_path)
                image_processing.show_image_bboxes_text(
                    "image_dict", image, box, label)