示例#1
0
def recognize_face(sess, pnet, rnet, onet, feature_array):
    # Get input and output tensors
    images_placeholder = sess.graph.get_tensor_by_name("input:0")
    images_placeholder = tf.image.resize_images(images_placeholder, (160, 160))
    embeddings = sess.graph.get_tensor_by_name("embeddings:0")
    phase_train_placeholder = sess.graph.get_tensor_by_name("phase_train:0")

    image_size = args.image_size
    embedding_size = embeddings.get_shape()[1]

    cap = cv2.VideoCapture(-1)

    while (True):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, 0)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break
        if (gray.size > 0):
            print(gray.size)
            response, faces, bboxs = align_face(gray, pnet, rnet, onet)
            print(response)
            if (response == True):
                for i, image in enumerate(faces):
                    bb = bboxs[i]
                    images = load_img(image, False, False, image_size)
                    feed_dict = {
                        images_placeholder: images,
                        phase_train_placeholder: False
                    }
                    feature_vector = sess.run(embeddings, feed_dict=feed_dict)
                    result, accuracy = identify_person(feature_vector,
                                                       feature_array, 8)
                    print(result.split("/")[2])
                    print(accuracy)

                    if accuracy < 0.9:
                        cv2.rectangle(gray, (bb[0], bb[1]), (bb[2], bb[3]),
                                      (255, 255, 255), 2)
                        W = int(bb[2] - bb[0]) // 2
                        H = int(bb[3] - bb[1]) // 2
                        cv2.putText(gray, "Hello " + result.split("/")[2],
                                    (bb[0] + W - (W // 2), bb[1] - 7),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                    (255, 255, 255), 1, cv2.LINE_AA)
                    else:
                        cv2.rectangle(gray, (bb[0], bb[1]), (bb[2], bb[3]),
                                      (255, 255, 255), 2)
                        W = int(bb[2] - bb[0]) // 2
                        H = int(bb[3] - bb[1]) // 2
                        cv2.putText(gray, "WHO ARE YOU ?",
                                    (bb[0] + W - (W // 2), bb[1] - 7),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                    (255, 255, 255), 1, cv2.LINE_AA)
                    del feature_vector

            cv2.imshow('img', gray)
        else:
            continue
示例#2
0
def recognize_async(images_placeholder, phase_train_placeholder, embeddings,
                    sess, feature_array, image, faces, bboxes):
    #response, faces,bboxs = align_face(image, pnet, rnet, onet)
    #response, faces, bboxs = align_face_roi(image, detector)
    result_list = []
    for i, image in enumerate(faces):
        bb = bboxes[i]
        images = load_img(image, False, False, args.image_size)
        feed_dict = {
            images_placeholder: images,
            phase_train_placeholder: False
        }
        feature_vector = sess.run(embeddings, feed_dict=feed_dict)
        result, accuracy = identify_person(feature_vector, feature_array, 8)
        result_name = result.split("/")[-2]  #.encode('utf-8')
        # append result
        result_list.append({
            'box': bb,
            'name': result_name,
            'acc': accuracy,
            'faceimg': image
        })

        del feature_vector
    return result_list
示例#3
0
def forward_pass(img, session, images_placeholder, phase_train_placeholder,
                 embeddings, image_size):
    """Feeds an image to the FaceNet model and returns a 128-dimension embedding for facial recognition.

    Args:
        img: image file (numpy array).
        session: The active Tensorflow session.
        images_placeholder: placeholder of the 'input:0' tensor of the pre-trained FaceNet model graph.
        phase_train_placeholder: placeholder of the 'phase_train:0' tensor of the pre-trained FaceNet model graph.
        embeddings: placeholder of the 'embeddings:0' tensor from the pre-trained FaceNet model graph.
        image_size: (int) required square image size.

    Returns:
          embedding: (numpy array) of 128 values after the image is fed to the FaceNet model.
    """
    # If there is a human face
    if img is not None:
        # Normalize the pixel values of the image for noise reduction for better accuracy and resize to desired size
        image = load_img(img=img,
                         do_random_crop=False,
                         do_random_flip=False,
                         do_prewhiten=True,
                         image_size=image_size)
        # Run forward pass on FaceNet model to calculate embedding
        feed_dict = {images_placeholder: image, phase_train_placeholder: False}
        embedding = session.run(embeddings, feed_dict=feed_dict)
        return embedding

    else:
        return None
示例#4
0
def recognize_face(sess, pnet, rnet, onet, feature_array):
    # Get input and output tensors
    images_placeholder = sess.graph.get_tensor_by_name("input:0")
    images_placeholder = tf.image.resize_images(images_placeholder, (160, 160))
    embeddings = sess.graph.get_tensor_by_name("embeddings:0")
    phase_train_placeholder = sess.graph.get_tensor_by_name("phase_train:0")

    image_size = args.image_size
    embedding_size = embeddings.get_shape()[1]
    #print("embedding_size:" + embedding_size)

    cap = cv2.VideoCapture(0)
    font = ImageFont.truetype('/System/Library/Fonts/PingFang.ttc', 18)

    while (True):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, 0)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break
        if (gray.size > 0):
            #print(gray.size)
            print("shape: " + str(gray.shape[0]) + ", " + str(gray.shape[1]))
            downsampleShape = (int(gray.shape[1] / 2), int(gray.shape[0] / 2))
            gray = np.asarray(
                Image.fromarray(gray).resize(downsampleShape,
                                             resample=Image.BILINEAR))

            response, faces, bboxes = align_face(gray, pnet, rnet, onet)
            print(response)
            if (response == True):
                for i, image in enumerate(faces):
                    bb = bboxes[i]
                    images = load_img(image, False, False, image_size)
                    feed_dict = {
                        images_placeholder: images,
                        phase_train_placeholder: False
                    }
                    feature_vector = sess.run(embeddings, feed_dict=feed_dict)
                    result, accuracy = identify_person(feature_vector,
                                                       feature_array, 8)
                    result_name = result.split("/")[-2]  #.encode('utf-8')

                    print(result_name)
                    print(accuracy)

                    if accuracy < 0.9:
                        cv2.rectangle(gray, (bb[0], bb[1]), (bb[2], bb[3]),
                                      (255, 255, 255), 2)
                        W = int(bb[2] - bb[0]) // 2
                        H = int(bb[3] - bb[1]) // 2
                        #cv2.putText(gray,"Hello "+result_name,(bb[0]+W-(W//2),bb[1]-7), cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1,cv2.LINE_AA)
                        gray_img = Image.fromarray(gray)
                        draw = ImageDraw.Draw(gray_img)
                        draw.text((bb[0] + W - (W // 2), bb[1] - 28),
                                  result_name,
                                  font=font,
                                  fill='white')
                        gray = np.array(gray_img)
                    else:
                        cv2.rectangle(gray, (bb[0], bb[1]), (bb[2], bb[3]),
                                      (255, 255, 255), 2)
                        W = int(bb[2] - bb[0]) // 2
                        H = int(bb[3] - bb[1]) // 2
                        cv2.putText(gray, "WHO ARE YOU ?",
                                    (bb[0] + W - (W // 2), bb[1] - 7),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                    (255, 255, 255), 1, cv2.LINE_AA)
                    del feature_vector

            cv2.imshow('img', gray)
        else:
            continue