def face_detect(input_img, pnet, rnet, onet,minsize=60, threshold=[0.6, 0.7, 0.7], factor=0.707):
    frame_to_face = input_img
    img_size = frame_to_face.shape
    frame_to_face = frame_to_face[..., ::-1]
    bounding_boxes, _ = FaceRecognition.detect_face.detect_face(frame_to_face, minsize, pnet, rnet, onet, threshold, factor)
    if len(bounding_boxes) > 0:
        faces = []
        faces_boxes = []
        faces_confidence = []
        for i in range(len(bounding_boxes)):
            det = np.squeeze(bounding_boxes[i, 0:4])
            bb = np.zeros(4, dtype=np.int32)
            margin_x = (det[2] - det[0]) / 4
            margin_y = (det[3] - det[1]) / 4
            assert margin_x > 0 and margin_y > 0
            bb[0] = np.maximum(det[0] - margin_x, 0)
            bb[1] = np.maximum(det[1] - margin_y, 0)
            bb[2] = np.minimum(det[2] + margin_x, img_size[1])
            bb[3] = np.minimum(det[3] + margin_y / 2, img_size[0])
            # output_image = cv2.rectangle(output_image, (bb[0], bb[1]), (bb[2], bb[3]), (0, 255, 0), 3)
            # bounding_boxes = find_face_from_openpose(keypoints)

            # for bb in bounding_boxes:
            face_img = frame_to_face[bb[1]:bb[3], bb[0]:bb[2], :]
            face_img = cv2.resize(face_img, (160, 160))
            face_img = facenet.prewhiten(face_img)
            faces.append(face_img)
            faces_boxes.append(bb)
    else:
        faces = []
        faces_boxes = []
    return faces, faces_boxes
def face_detect_and_recognize(input_img, sess, pnet, rnet, onet, images_placeholder, phase_train_placeholder,
                              embeddings,
                              minsize, threshold, factor):
    frame_to_face = input_img
    img_size = frame_to_face.shape
    frame_to_face = frame_to_face[..., ::-1]
    bounding_boxes, _ = FaceRecognition.detect_face.detect_face(frame_to_face, minsize, pnet, rnet, onet,
                                                                threshold,
                                                                factor)
    if len(bounding_boxes) > 0:
        faces = []
        faces_boxes = []
        faces_confidence = []
        for i in range(len(bounding_boxes)):
            det = np.squeeze(bounding_boxes[i, 0:4])
            bb = np.zeros(4, dtype=np.int32)
            margin_x = (det[2] - det[0]) / 4
            margin_y = (det[3] - det[1]) / 4
            assert margin_x > 0 and margin_y > 0
            bb[0] = np.maximum(det[0] - margin_x, 0)
            bb[1] = np.maximum(det[1] - margin_y, 0)
            bb[2] = np.minimum(det[2] + margin_x, img_size[1])
            bb[3] = np.minimum(det[3] + margin_y, img_size[0])
            # output_image = cv2.rectangle(output_image, (bb[0], bb[1]), (bb[2], bb[3]), (0, 255, 0), 3)
            # bounding_boxes = find_face_from_openpose(keypoints)

            # for bb in bounding_boxes:
            face_img = frame_to_face[bb[1]:bb[3], bb[0]:bb[2], :]
            face_img = cv2.resize(face_img, (160, 160))
            face_img = facenet.prewhiten(face_img)
            faces.append(face_img)
            faces_boxes.append(bb)

        feed_dict = {images_placeholder: faces,
                     phase_train_placeholder: False}
        emb = sess.run(embeddings, feed_dict=feed_dict)
    else:
        faces_boxes = []
        emb = []
    return faces_boxes, emb
Esempio n. 3
0
    def resultado(self):

        #input_video="http://*****:*****@192.168.1.33:8081"
        #input_video="./FaceRecognition/videoGOT.mp4" #ruta

        modeldir = os.path.realpath(
            "./FaceRecognition/model/20170511-185253.pb"
        )  # 'C:/Users/Corgito/Desktop/untitled/FaceRecognition/model/20170511-185253.pb'
        classifier_filename = os.path.realpath(
            "./FaceRecognition/class/classifier.pkl"
        )  # 'C:/Users/Corgito/Desktop/untitled/FaceRecognition/class/classifier.pkl'
        npy = os.path.realpath(
            "./FaceRecognition/npy"
        )  # 'C:/Users/Corgito/Desktop/untitled/FaceRecognition/npy'
        train_img = os.path.realpath(
            "./FaceRecognition/train_img"
        )  # "C:/Users/Corgito/Desktop/untitled/FaceRecognition/train_img"
        with tf.Graph().as_default():
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
            sess = tf.Session(config=tf.ConfigProto(
                gpu_options=gpu_options, log_device_placement=False))
            with sess.as_default():
                pnet, rnet, onet = detect_face.create_mtcnn(sess, npy)

                minsize = 10  # minimum size of face
                threshold = [0.8, 0.8, 0.8]  # three steps's threshold
                factor = 0.709  # scale factor
                margin = 44
                frame_interval = 3
                batch_size = 1000
                image_size = 182
                input_image_size = 160

                HumanNames = os.listdir(train_img)
                HumanNames.sort()

                print('Loading Modal')
                facenet.load_model(modeldir)
                images_placeholder = tf.get_default_graph().get_tensor_by_name(
                    "input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name(
                    "embeddings:0")
                phase_train_placeholder = tf.get_default_graph(
                ).get_tensor_by_name("phase_train:0")
                embedding_size = embeddings.get_shape()[1]

                classifier_filename_exp = os.path.expanduser(
                    classifier_filename)
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                video_capture = cv2.VideoCapture(0)
                c = 0

                print('Start Recognition')
                prevTime = 0
                while True:
                    ret, frame = video_capture.read()

                    #frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)  # resize frame (optional)

                    curTime = time.time() + 1  # calc fps
                    timeF = frame_interval

                    if (c % timeF == 0):
                        find_results = []

                        if frame.ndim == 2:
                            frame = facenet.to_rgb(frame)
                        frame = frame[:, :, 0:3]
                        bounding_boxes, _ = detect_face.detect_face(
                            frame, minsize, pnet, rnet, onet, threshold,
                            factor)
                        nrof_faces = bounding_boxes.shape[0]
                        print('Detected_FaceNum: %d' % nrof_faces)

                        if nrof_faces > 0:
                            det = bounding_boxes[:, 0:4]
                            img_size = np.asarray(frame.shape)[0:2]

                            cropped = []
                            scaled = []
                            scaled_reshape = []
                            bb = np.zeros((nrof_faces, 4), dtype=np.int32)

                            for i in range(nrof_faces):
                                emb_array = np.zeros((1, embedding_size))

                                bb[i][0] = det[i][0]
                                bb[i][1] = det[i][1]
                                bb[i][2] = det[i][2]
                                bb[i][3] = det[i][3]

                                # inner exception
                                if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][
                                        2] >= len(frame[0]) or bb[i][3] >= len(
                                            frame):
                                    print('Face is very close!')
                                    continue

                                cropped.append(frame[bb[i][1]:bb[i][3],
                                                     bb[i][0]:bb[i][2], :])
                                cropped[i] = facenet.flip(cropped[i], False)
                                scaled.append(
                                    misc.imresize(cropped[i],
                                                  (image_size, image_size),
                                                  interp='bilinear'))
                                scaled[i] = cv2.resize(
                                    scaled[i],
                                    (input_image_size, input_image_size),
                                    interpolation=cv2.INTER_CUBIC)
                                scaled[i] = facenet.prewhiten(scaled[i])
                                scaled_reshape.append(scaled[i].reshape(
                                    -1, input_image_size, input_image_size, 3))
                                feed_dict = {
                                    images_placeholder: scaled_reshape[i],
                                    phase_train_placeholder: False
                                }
                                emb_array[0, :] = sess.run(embeddings,
                                                           feed_dict=feed_dict)
                                predictions = model.predict_proba(emb_array)
                                print(predictions)
                                best_class_indices = np.argmax(predictions,
                                                               axis=1)
                                best_class_probabilities = predictions[
                                    np.arange(len(best_class_indices)),
                                    best_class_indices]
                                # print("predictions")
                                print(best_class_indices, ' with accuracy ',
                                      best_class_probabilities)

                                # print(best_class_probabilities)
                                if best_class_probabilities > 0.1:
                                    cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                                  (bb[i][2], bb[i][3]),
                                                  (0, 255, 0),
                                                  2)  # boxing face

                                    # plot result idx under box
                                    text_x = bb[i][0]
                                    text_y = bb[i][3] + 20
                                    print('Result Indices: ',
                                          best_class_indices[0])
                                    print(HumanNames)
                                    for H_i in HumanNames:
                                        if HumanNames[
                                                best_class_indices[0]] == H_i:
                                            result_names = HumanNames[
                                                best_class_indices[0]]
                                            cv2.putText(
                                                frame,
                                                result_names, (text_x, text_y),
                                                cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                1, (0, 0, 255),
                                                thickness=1,
                                                lineType=2)

                        else:
                            print('Alignment Failure')
            # c+=1
            #cv2.imshow('Video', frame)

                    evento, cuadro = cv2.imencode('.jpg', frame)
                    #ventana='<video width="320" height="240" controls> <source src="',cuadro.tobytes(),'" type="video/mp4"> </video>'
                    ventana = cuadro.tobytes()

                    yield (b'--frame\r\n'
                           b'Content-Type: video/mp4\r\n\r\n' + ventana +
                           b'\r\n\r\n')

                    if cv2.waitKey(32) & 0xFF == ord('q'):
                        break

                video_capture.release()
                cv2.destroyAllWindows()
Esempio n. 4
0
    def resultado(self, ruta):
        """
        Método para aplicar reconocimiento facial a la imagen.

        :Método función: **resultado** (self,input_video).
        :valor self: None.
        :valor input_video: Ruta del archivo de imagen.

        :Lista de parámetros variables:

            :minsize: Tamaño mínimo para detectar el rostro.
            :umbral: Valor mínimo para marcar a la persona como desconocida.
    
        :return result_name: Devuelve el nombre de la persona reconocida.
        """
        result_names = "No se detecto ninguna cara"
        img_path = ruta
        modeldir = os.path.realpath(
            "./FaceRecognition/model/20170511-185253.pb")
        classifier_filename = os.path.realpath(
            "./FaceRecognition/class/classifier.pkl")
        npy = os.path.realpath("./FaceRecognition/npy")
        train_img = os.path.realpath("./FaceRecognition/train_img")

        with tf.Graph().as_default():
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
            sess = tf.Session(config=tf.ConfigProto(
                gpu_options=gpu_options, log_device_placement=False))
            with sess.as_default():
                pnet, rnet, onet = detect_face.create_mtcnn(sess, npy)
                image = cv2.imread(img_path)
                umbral = 0.35
                h, w, _ = image.shape
                minsize = 8  # minimum size of face
                threshold = [0.7, 0.7, 0.7]  # three steps's threshold
                factor = 0.709  # scale factor
                margin = 20
                frame_interval = 3
                batch_size = 1000
                image_size = 182
                input_image_size = 160

                HumanNames = os.listdir(train_img)
                HumanNames.sort()

                print('Loading feature extraction model')
                facenet.load_model(modeldir)

                images_placeholder = tf.get_default_graph().get_tensor_by_name(
                    "input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name(
                    "embeddings:0")
                phase_train_placeholder = tf.get_default_graph(
                ).get_tensor_by_name("phase_train:0")
                embedding_size = embeddings.get_shape()[1]

                classifier_filename_exp = os.path.expanduser(
                    classifier_filename)
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)

                c = 0

                print('Start Recognition!')
                prevTime = 0
                frame = cv2.imread(img_path, 0)

                curTime = time.time() + 1  # calc fps
                timeF = frame_interval

                if (c % timeF == 0):
                    find_results = []

                    if frame.ndim == 2:
                        frame = facenet.to_rgb(frame)
                    frame = frame[:, :, 0:3]
                    bounding_boxes, _ = detect_face.detect_face(
                        frame, minsize, pnet, rnet, onet, threshold, factor)
                    nrof_faces = bounding_boxes.shape[0]
                    print('Face Detected: %d' % nrof_faces)

                    if nrof_faces > 0:
                        det = bounding_boxes[:, 0:4]
                        img_size = np.asarray(frame.shape)[0:2]

                        cropped = []
                        scaled = []
                        scaled_reshape = []
                        bb = np.zeros((nrof_faces, 4), dtype=np.int32)

                        for i in range(nrof_faces):
                            emb_array = np.zeros((1, embedding_size))

                            bb[i][0] = det[i][0]
                            bb[i][1] = det[i][1]
                            bb[i][2] = det[i][2]
                            bb[i][3] = det[i][3]

                            # inner exception
                            if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][
                                    2] >= len(
                                        frame[0]) or bb[i][3] >= len(frame):
                                print('face is too close')
                                continue

                            cropped.append(frame[bb[i][1]:bb[i][3],
                                                 bb[i][0]:bb[i][2], :])
                            cropped[i] = facenet.flip(cropped[i], False)
                            scaled.append(
                                misc.imresize(cropped[i],
                                              (image_size, image_size),
                                              interp='bilinear'))
                            scaled[i] = cv2.resize(
                                scaled[i],
                                (input_image_size, input_image_size),
                                interpolation=cv2.INTER_CUBIC)
                            scaled[i] = facenet.prewhiten(scaled[i])
                            scaled_reshape.append(scaled[i].reshape(
                                -1, input_image_size, input_image_size, 3))
                            feed_dict = {
                                images_placeholder: scaled_reshape[i],
                                phase_train_placeholder: False
                            }
                            emb_array[0, :] = sess.run(embeddings,
                                                       feed_dict=feed_dict)
                            predictions = model.predict_proba(emb_array)
                            print(predictions)
                            best_class_indices = np.argmax(predictions, axis=1)
                            # print(best_class_indices)
                            best_class_probabilities = predictions[
                                np.arange(len(best_class_indices)),
                                best_class_indices]
                            print(best_class_probabilities)
                            cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                          (bb[i][2], bb[i][3]), (0, 255, 0),
                                          2)  # boxing face

                            # plot result idx under box
                            text_x = bb[i][0]
                            text_y = bb[i][3] + 20
                            print('Result Indices: ', best_class_indices[0])
                            print(HumanNames)
                            print("Mejor Indicie", best_class_indices[0])
                            if best_class_probabilities < umbral:
                                result_names = "Desconocido"

                                cv2.putText(frame,
                                            result_names + str(i),
                                            (text_x, text_y),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            1, (0, 0, 255),
                                            thickness=1,
                                            lineType=2)
                            else:
                                for H_i in HumanNames:

                                    if HumanNames[
                                            best_class_indices[0]] == H_i:
                                        result_names = HumanNames[
                                            best_class_indices[0]]  # + str(i)
                                        cv2.putText(
                                            frame,
                                            result_names, (text_x, text_y),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            1, (0, 0, 255),
                                            thickness=1,
                                            lineType=2)

                            print("La cara", i, "pertene a", result_names,
                                  "con un indice de probabilidad de",
                                  best_class_probabilities)
                    else:
                        print('Unable to align')
                #dim=int(frame.shape[1]-200),int(frame.shape[0]-200)#height,width
                #frame = cv2.resize(frame, dim, fx=0.5, fy=0.5)  #el resize es opcional, pero si se hace mejor despues de la deteccíon ya que si no perdemos información
                #cv2.imshow('Image', frame)
                cv2.imwrite(ruta, frame)
                return result_names

                if cv2.waitKey(1000000) & 0xFF == ord('q'):
                    cv2.destroyAllWindows()
                    #sys.exit("Thanks")
                cv2.destroyAllWindows()
Esempio n. 5
0
def process_latest_frame(openpose_path,
                         mtcnn_path,
                         facenet_path,
                         reid_path,
                         queue=None):
    frame_count = 1
    # Load OpenPose Model
    openpose = load_openpose_model(openpose_path)
    print('----------OpenPose Loaded----------')

    # Load MTCNN (face detection)
    minsize = 50  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709
    margin = 44
    with tf.Graph().as_default():
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess1 = tf.Session(config=config)
        with sess1.as_default():
            pnet, rnet, onet = FaceRecognition.detect_face.create_mtcnn(
                sess1, mtcnn_path)
    print('----------MTCNN Loaded----------')

    # Load Facenet
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    facenet.load_model(facenet_path)
    images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
    embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
    phase_train_placeholder = tf.get_default_graph().get_tensor_by_name(
        "phase_train:0")
    print('----------FaceNet Loaded----------')

    # Load Reid model
    reid_model = load_reid_model(reid_path)
    print('----------ReID Loaded----------')

    while True:
        if not queue.empty():
            frame_count += 1

            start = time.time()
            frame_to_process = queue.get()
            img_size = np.asarray(frame_to_process.shape)[0:2]
            keypoints, output_image = openpose.forward(frame_to_process, True)

            if frame_count % 200 == 0:
                print('--------------Do face recognition-------------')
                frame_to_face = frame_to_process.copy()
                frame_to_face = frame_to_face[..., ::-1]
                bounding_boxes, _ = FaceRecognition.detect_face.detect_face(
                    frame_to_face, minsize, pnet, rnet, onet, threshold,
                    factor)
                if len(bounding_boxes) > 0:
                    det = np.squeeze(bounding_boxes[0, 0:4])
                    bb = np.zeros(4, dtype=np.int32)
                    bb[0] = np.maximum(det[0] - margin / 2, 0)
                    bb[1] = np.maximum(det[1] - margin / 2, 0)
                    bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
                    bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
                    # output_image = cv2.rectangle(output_image, (bb[0], bb[1]), (bb[2], bb[3]), (0, 255, 0), 3)
                    # bounding_boxes = find_face_from_openpose(keypoints)

                    # for bb in bounding_boxes:
                    face_img = frame_to_face[bb[1]:bb[3], bb[0]:bb[2], :]
                    face_img = cv2.resize(face_img, (160, 160))
                    face_img = facenet.prewhiten(face_img)

                    feed_dict = {
                        images_placeholder: [face_img],
                        phase_train_placeholder: False
                    }
                    emb = sess.run(embeddings, feed_dict=feed_dict)
                    output_image = cv2.rectangle(output_image, (bb[0], bb[1]),
                                                 (bb[2], bb[3]), (0, 255, 0),
                                                 3)
            print('Frame: {:>7}, {:4}'.format(frame_count,
                                              time.time() - start))
            cv2.imshow('a', output_image)
            cv2.waitKey(1)