Ejemplo n.º 1
0
def load_and_align_image(image, image_size, margin, gpu_memory_fraction):

    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    print('Creating networks and loading parameters')
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        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, None)

    img_size = np.asarray(image.shape)[0:2]
    bounding_boxes, _ = detect_face.detect_face(image, minsize, pnet, rnet,
                                                onet, threshold, factor)
    if len(bounding_boxes) < 1:
        print("can't detect face, remove ", image)
    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])
    cropped = image[bb[1]:bb[3], bb[0]:bb[2], :]
    aligned = misc.imresize(cropped, (image_size, image_size),
                            interp='bilinear')
    prewhitened = facenet.prewhiten(aligned)

    return prewhitened
Ejemplo n.º 2
0
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction):
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    nrof_samples = len(image_paths)
    img_list = [None] * nrof_samples
    for i in xrange(nrof_samples):
        img = misc.imread(os.path.expanduser(image_paths[i]))
        img_size = np.asarray(img.shape)[0:2]
        bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet,
                                                    onet, threshold, factor)
        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])
        cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
        aligned = misc.imresize(cropped, (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        img_list[i] = prewhitened
    images = np.stack(img_list)
    return images
Ejemplo n.º 3
0
def align_image2(image, image_size, margin, pnet, rnet, onet):
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    img_list = []
    boxes = []

    img = misc.imread(image, mode='RGB')
    img_size = np.asarray(img.shape)[0:2]
    bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet,
                                                threshold, factor)
    #print('number of box',len(bounding_boxes))
    if len(bounding_boxes) < 1:
        print('can\'t detect face')
        return img_list, boxes
    for det in bounding_boxes:
        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])
        cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
        aligned = misc.imresize(cropped, (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        boxes.append(bb.tolist())
        img_list.append(prewhitened)
    images = np.stack(img_list)
    return images, boxes
Ejemplo n.º 4
0
def align_data(image_list, image_size, margin, pnet, rnet, onet):
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    img_list = []

    for x in range(len(image_list)):
        img_size = np.asarray(image_list[x].shape)[0:2]
        bounding_boxes, _ = align.detect_face.detect_face(
            image_list[x], minsize, pnet, rnet, onet, threshold, factor)
        nrof_samples = len(bounding_boxes)
        if nrof_samples > 0:
            for i in range(nrof_samples):
                if bounding_boxes[i][4] > 0.95:
                    det = np.squeeze(bounding_boxes[i, 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])
                    cropped = image_list[x][bb[1]:bb[3], bb[0]:bb[2], :]
                    aligned = misc.imresize(cropped, (image_size, image_size),
                                            interp='bilinear')
                    prewhitened = facenet.prewhiten(aligned)
                    img_list.append(prewhitened)

    if len(img_list) > 0:
        images = np.stack(img_list)
        return images
    else:
        return None
Ejemplo n.º 5
0
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction):

    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    print('Creating networks and loading parameters')
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)

    nrof_samples = len(image_paths)
    img_list = [None] * nrof_samples
    for i in range(nrof_samples):
        img = misc.imread(os.path.expanduser(image_paths[i]))
        img_size = np.asarray(img.shape)[0:2]
        bounding_boxes, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        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])
        cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
        aligned = misc.imresize(cropped, (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        img_list[i] = prewhitened
    images = np.stack(img_list)
    return images
Ejemplo n.º 6
0
    def generate_embedding(self, face):
        # Get input and output tensors
        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")

        prewhiten_face = facenet.prewhiten(face.image)

        # Run forward pass to calculate embeddings
        feed_dict = {images_placeholder: [prewhiten_face], phase_train_placeholder: False}
        return self.sess.run(embeddings, feed_dict=feed_dict)[0]
Ejemplo n.º 7
0
def load_png_files(image_paths, image_size):
    """Due to we have cropped the images for faces, there is no need to redo MTCNN net"""
    img_list = []
    print(image_paths)
    for image in image_paths:
        img = misc.imread(os.path.expanduser(image), mode='RGB')
        img = misc.imresize(img, (image_size, image_size), interp='bilinear')
        prewhitened = facenet.prewhiten(img)
        img_list.append(prewhitened)
    images = np.stack(img_list)
    return images
Ejemplo n.º 8
0
 def face_embeddings(self, image_path):
     image = self.load_image(
         image_path, self.input_image_size, self.input_image_size, 'RGB')
     # prewhiten()
     # -> 平均値を差し引き、入力画像のピクセル値の範囲を正規化. トレーニングが簡単になる.
     prewhitened = facenet.prewhiten(image)
     prewhitened = prewhitened.reshape(
         -1, prewhitened.shape[0], prewhitened.shape[1], prewhitened.shape[2])
     feed_dict = {self.images_placeholder: prewhitened,
                  self.phase_train_placeholder: False}
     embeddings = self.sess.run(self.embeddings, feed_dict=feed_dict)
     return embeddings
def load_images_from_folder(paths, image_size):
    image_list = []
    for filename in paths:
        img = misc.imread(filename)
        img = misc.imresize(img, (image_size, image_size), interp='bilinear')
        # print('========================')
        # print(img)
        # print('img shape {}'.format(np.shape(img)))
        prewhitened = facenet.prewhiten(img)
        image_list.append(prewhitened)
        print('shape of image_list {}'.format(np.shape(image_list)))

        images = np.stack(image_list)
    return images
Ejemplo n.º 10
0
def get_batch_data_test():
    image_path_list = [
        './data/compare/Abel_Pacheco_0003.png',
        './data/compare/Abel_Pacheco_0004.png'
    ]
    batch_data, batch_fname = [], []
    for image_path in image_path_list:
        img = Image.open(image_path)
        input_data = np.array(img.resize((160, 160))).astype(np.float32)
        input_data = facenet.prewhiten(input_data)
        Image.fromarray(input_data.astype(np.uint8)).show()
        batch_data.append(input_data)
        batch_fname.append(image_path)
    return batch_data, batch_fname
Ejemplo n.º 11
0
    def run(self):
        cap = cv2.VideoCapture(0)
        while True:
            # get a frame
            self.frame_counter += 1

            _, np_image = cap.read()  # GBR格式
            #np_image = np.roll(np_image, 1, axis=-1) # 重新排列通道,转变为 RGB格式
            b, g, r = np_image[:, :, 0], np_image[:, :, 1], np_image[:, :, 2]
            np_image = np.stack((r, g, b), axis=2)
            t_start = time.time()
            self.captured_image.set_image(input_image=np_image)
            image = Image.fromarray(np_image, mode='RGB')
            draw = ImageDraw.Draw(image)
            tmp = self.face_detector.detect_faces([np_image])
            if np.shape(tmp)[0] != 0:
                face_bb_list = tmp[0]

                for face_bb in face_bb_list:
                    draw.rectangle(
                        (face_bb.top_left_w, face_bb.top_left_h, face_bb.bottom_right_w, face_bb.bottom_right_h),
                        outline='red')

                    cropped = np_image[face_bb.top_left_h:face_bb.bottom_right_h,
                              face_bb.top_left_w:face_bb.bottom_right_w, :]
                    aligned = misc.imresize(cropped, (self.face_resize, self.face_resize), interp='bicubic')
                    prewhitened = facenet.prewhiten(aligned)
                    face_image = np.reshape(prewhitened, (1,) + np.shape(prewhitened))
                    features = self.face_feature_extractor.extract_features_from_images(face_image)
                    most_similar_face = search_most_similar_face_from_db(features)
                    if most_similar_face is None:
                        continue
                    if most_similar_face.similarity > 0.9:
                        draw.text(
                            (face_bb.top_left_w, face_bb.top_left_h),
                            text=most_similar_face.name + ' similarity : ' + str(most_similar_face.similarity)
                        )
                    else:
                        draw.text(
                            (face_bb.top_left_w, face_bb.top_left_h),
                            text='No Match, smallest distance : ' + str(most_similar_face.similarity)
                        )

            tk_image = ImageTk.PhotoImage(image=image)
            self.image_widget['image'] = tk_image
            t_finish = time.time()
            print('frame %d: time cost %.3fs' % (self.frame_counter, t_finish - t_start))
        cap.release()
        cv2.desotryAllWindows()
Ejemplo n.º 12
0
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction):

    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    print('Creating networks and loading parameters')
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
            # pnet, rnet, net = detect_face.create_mtcnn(sess, None)

    tmp_image_paths = image_paths.copy()
    img_list = []
    for image in tmp_image_paths:
        img = misc.imread(os.path.expanduser(image), mode='RGB')
        img_size = np.asarray(img.shape)[0:2]
        bounding_boxes, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        # bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)

        # if len(bounding_boxes) < 1:
        #   image_paths.remove(image)
        #   print("can't detect face, remove ", image)
        #   continue
        # det = np.squeeze(bounding_boxes[0,0:4])  #去掉了最后一个元素?
        # bb = np.zeros(4, dtype=np.int32)
        # # np.maximum:(X, Y, out=None) ,X 与 Y 逐位比较取其大者;相当于矩阵个元素比较
        # bb[0] = np.maximum(det[0]-margin/2, 0)#margin:人脸的宽和高?默认为44
        # 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])
        # cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]

        cropped = img
        aligned = misc.imresize(cropped, (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        img_list.append(prewhitened)
    images = np.stack(img_list)
    return images
Ejemplo n.º 13
0
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction):
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    print('Creating networks and loading parameters')
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)

    tmp_image_paths = copy.copy(image_paths)
    img_list = []
    for image in tmp_image_paths:
        img = misc.imread(os.path.expanduser(image), mode='RGB')
        img_size = np.asarray(img.shape)[0:2]
        bounding_boxes, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        if len(bounding_boxes) < 1:
            bounding_boxes = np.array([[
                24.11117871, 20.92340004, 126.24065695, 139.88384303,
                0.99999821
            ]])
            # image_paths.remove(image)
            print("can't detect face, set random Bounding Box ", image)
            # cropped = img
            # continue
        det = np.squeeze(bounding_boxes[0, 0:4])
        # print('bounding_boxes is {}'.format(bounding_boxes))
        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])
        cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
        aligned = misc.imresize(img, (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        img_list.append(prewhitened)
    images = np.stack(img_list)
    return images
Ejemplo n.º 14
0
 def record_face(self):
     """
     从最近捕获到的一帧中(self.captured_image) 找脸、提取特征、连同self.face_name_entry录入的信息,写入数据库
     """
     self.captured_image.lock_acquire()
     np_image = np.copy(self.captured_image.get_image())
     self.captured_image.lock_release()
     tmp = self.face_detector.detect_faces([np_image])
     if np.shape(tmp)[0] != 0:
         raw_image_id = insert_raw_image_into_db(image=Image.fromarray(np_image), name=self.face_name_entry.get())
         face_bb = tmp[0][0]
         cropped = np_image[face_bb.top_left_h:face_bb.bottom_right_h, face_bb.top_left_w:face_bb.bottom_right_w, :]
         aligned = misc.imresize(cropped, (self.face_resize, self.face_resize), interp='bicubic')
         prewhitened = facenet.prewhiten(aligned)
         face_image = np.reshape(prewhitened, (1,) + np.shape(prewhitened))
         features = self.face_feature_extractor.extract_features_from_images(face_image)
         face_info = FaceInfo(name=self.face_name_entry.get(), face_image=Image.fromarray(cropped),
                              face_features=features[0, :].tolist(), raw_image_id=raw_image_id)
         face_info.insert_into_db()
Ejemplo n.º 15
0
def get_batch_data():
    batch_data, batch_fname = [], []
    for image_name in tqdm(test_image_names):
        image_path = os.path.join(image_dir, image_name)
        i = 0
        for image_fname in os.listdir(image_path):
            if i > 3:
                break
            i += 1
            if not image_fname.endswith('.png'):
                continue
            img = Image.open(os.path.join(image_path, image_fname))
            input_data = np.array(img.resize((160, 160))).astype(np.float32)
            input_data = facenet.prewhiten(input_data)

            batch_data.append(input_data)
            batch_fname.append(image_fname)

            if len(batch_data) == batch_size:
                yield batch_data, batch_fname
                batch_data, batch_fname = [], []

    if len(batch_data) > 0:
        yield batch_data, batch_fname
Ejemplo n.º 16
0
    def load_data(self,
                  imgs,
                  do_random_crop,
                  do_random_flip,
                  image_size,
                  do_prewhiten=True):

        nrof_samples = len(imgs)
        images = np.zeros((nrof_samples, 160, 160, 3))

        for i in range(nrof_samples):

            img = imgs[i]
            if img.ndim == 2:
                img = to_rgb(img)
            if do_prewhiten:
                img = facenet.prewhiten(img)
            img = facenet.crop(img, do_random_crop, image_size)
            img = facenet.flip(img, do_random_flip)
            if (image_size, image_size) != img.shape[:2]:
                img = misc.imresize(img, (image_size, image_size))
            images[i, :, :, :] = img

        return images
Ejemplo n.º 17
0
def main():
    #cap = VideoStream(src=0).start()
    cap = cv2.VideoCapture('http://192.168.1.102:4747/video')

    while (True):
        cropped_array = []
        bb_array = []
        name_array = []
        ret, frame = cap.read()
        (h, w) = frame.shape[:2]
        frame = imutils.resize(frame, width=600)
        frame = cv2.flip(frame, 1)

        # ?===================Face Recognition=====================================
        with recog_graph.as_default():
            with recog_sess.as_default():
                # recog_sess.run(recog_init)
                bounding_boxes, _ = detect_face.detect_face(
                    frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

                faces_found = bounding_boxes.shape[0]
                if faces_found > 0:
                    det = bounding_boxes[:, 0:4]
                    bb = np.zeros((faces_found, 4), dtype=np.int32)
                    for i in range(faces_found):
                        bb[i][0] = det[i][0]
                        bb[i][1] = det[i][1]
                        bb[i][2] = det[i][2]
                        bb[i][3] = det[i][3]
                        # print(bb[i][3]-bb[i][1])
                        # print(frame.shape[0])
                        # print((bb[i][3]-bb[i][1])/frame.shape[0])
                        if (bb[i][3] - bb[i][1]) / frame.shape[0] > 0.25:
                            cropped = frame[bb[i][1]:bb[i][3],
                                            bb[i][0]:bb[i][2], :]
                            scaled = cv2.resize(
                                cropped, (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),
                                interpolation=cv2.INTER_CUBIC)
                            scaled = facenet.prewhiten(scaled)
                            scaled_reshape = scaled.reshape(
                                -1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)
                            feed_dict = {
                                images_placeholder: scaled_reshape,
                                phase_train_placeholder: False
                            }
                            emb_array = recog_sess.run(embeddings,
                                                       feed_dict=feed_dict)

                            predictions = recog_model.predict_proba(emb_array)
                            best_class_indices = np.argmax(predictions, axis=1)
                            best_class_probabilities = predictions[
                                np.arange(len(best_class_indices)),
                                best_class_indices]
                            best_name = class_names[best_class_indices[0]]
                            # print("Name: {}, Probability: {}".format(best_name, best_class_probabilities))

                            if best_class_probabilities > 0.8:
                                cropped_array.append(cropped)
                                bb_array.append(bb)
                                name_array.append(
                                    class_names[best_class_indices[0]])

                                person_detected[best_name] += 1
                            else:
                                name = "Unknown"

        # ?===================Liveness=====================================
        with liveness_graph.as_default():
            with liveness_sess.as_default():
                # liveness_sess.run(liveness_init)
                for cropped, bb, name in zip(cropped_array, bb_array,
                                             name_array):
                    #TODO: Liveness
                    face = cv2.resize(cropped, (32, 32))
                    face = face.astype("float") / 255.0
                    face = img_to_array(face)
                    face = np.expand_dims(face, axis=0)

                    # Dua vao model de nhan dien fake/real
                    preds = liveness_model.predict(face)[0]

                    j = np.argmax(preds)
                    label = le.classes_[j]

                    # TODO: Draw Identicated Face
                    if (j == 0):
                        cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                      (bb[i][2], bb[i][3]), (0, 0, 255), 2)
                        text_x = bb[i][0]
                        text_y = bb[i][3] + 20

                        #name = class_names[best_class_indices[0]]
                        cv2.putText(frame,
                                    name, (text_x, text_y),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)
                        cv2.putText(frame,
                                    str(round(best_class_probabilities[0], 3)),
                                    (text_x, text_y + 17),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)
                    else:
                        cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                      (bb[i][2], bb[i][3]), (0, 255, 0), 2)
                        text_x = bb[i][0]
                        text_y = bb[i][3] + 20

                        #name = class_names[best_class_indices[0]]
                        cv2.putText(frame,
                                    name, (text_x, text_y),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)
                        cv2.putText(frame,
                                    str(round(best_class_probabilities[0], 3)),
                                    (text_x, text_y + 17),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)

        cv2.imshow('Face Recognition', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
Ejemplo n.º 18
0
def AttendenceCheck(url):
    # parser = argparse.ArgumentParser()
    # parser.add_argument('--path', help='Url of the image you want to dowload and test on.', default=0)
    # args = parser.parse_args()
    
    # Cai dat cac tham so can thiet
    MINSIZE = 20
    THRESHOLD = [0.6, 0.7, 0.7]
    FACTOR = 0.709
    IMAGE_SIZE = 182
    INPUT_IMAGE_SIZE = 160
    CLASSIFIER_PATH = 'Models/facemodel.pkl'
    # IMAGE_URL = args.path
    if url != "None":
        IMAGE_URL = url
        image_path = dwl.download_image(IMAGE_URL)
        print("Download done")
    else:
        IMAGE_URL = "test_image/d1.jpg"
        image_path = IMAGE_URL
    FACENET_MODEL_PATH = 'Models/20180402-114759.pb'
    best_name = ""
    best_class_probabilities = 0
    
    with open(CLASSIFIER_PATH, 'rb') as file:
        model, class_names = pickle.load(file)
    print("Custom Classifier, Successfully loaded")

    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():
            print('Loading feature extraction model')
            facenet.load_model(FACENET_MODEL_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")
            embedding_size = embeddings.get_shape()[1]

            pnet, rnet, onet = detect_face.create_mtcnn(sess, "src/align")

            people_detected = set()
            person_detected = collections.Counter()

            frame = cv2.imread(image_path)

            # frame = cv2.imread(IMAGE_URL)

            bounding_boxes, _ = detect_face.detect_face(frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

            faces_found = bounding_boxes.shape[0]
            try:
                if faces_found > 0:
                    det = bounding_boxes[:, 0:4]
                    bb = np.zeros((faces_found, 4), dtype=np.int32)
                    for i in range(faces_found):
                        bb[i][0] = det[i][0]
                        bb[i][1] = det[i][1]
                        bb[i][2] = det[i][2]
                        bb[i][3] = det[i][3]

                        cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :]
                        scaled = cv2.resize(cropped, (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),
                                            interpolation=cv2.INTER_CUBIC)
                        scaled = facenet.prewhiten(scaled)
                        scaled_reshape = scaled.reshape(-1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)
                        feed_dict = {images_placeholder: scaled_reshape, phase_train_placeholder: False}
                        emb_array = sess.run(embeddings, feed_dict=feed_dict)
                        predictions = model.predict_proba(emb_array)
                        best_class_indices = np.argmax(predictions, axis=1)
                        best_class_probabilities = predictions[
                            np.arange(len(best_class_indices)), best_class_indices]
                        best_name = class_names[best_class_indices[0]]
                        
                        # print("Name: {}, Probability: {}".format(best_name, best_class_probabilities))
                        cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2)
                        text_x = bb[i][0]
                        text_y = bb[i][3] + 20
                        
                        if best_class_probabilities > 0.8:
                            name = class_names[best_class_indices[0]]
                        else:
                            name = "Unknown"
                        cv2.putText(frame, name, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (255, 255, 255), thickness=1, lineType=2)
                        cv2.putText(frame, str(round(best_class_probabilities[0], 3)), (text_x, text_y + 17),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255), thickness=1, lineType=2)
                        person_detected[best_name] += 1
            except:
                    pass
    print(best_name, str(best_class_probabilities))
    return best_name, str(best_class_probabilities)
Ejemplo n.º 19
0
def reco_face(args):
    save_path = "{}{}{}{}{}".format(args.save_path, args.model_dir[2:4], '_',
                                    args.dist, '/')
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    with tf.device('/gpu:0'):
        with tf.Graph().as_default():
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            with tf.Session(config=config) as sess:
                file_paths = args.img_path
                imgs_list = os.listdir(file_paths)
                all_img = len(os.listdir(file_paths))
                people_sum = 0
                img_list = []
                emb_list = []

                facenet.load_model(args.model_dir)
                #                 facenet.load_model('20190218-164145/20190218-164145.pb')
                image_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]

                start_time = time.time()
                for img in imgs_list:
                    x = len(os.listdir(save_path))
                    file = "{}{}".format(file_paths, img)
                    image = cv2.imread(file)
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    image = facenet.prewhiten(image)
                    image_reshaped = image.reshape(-1, 160, 160, 3)
                    emb_temp = np.zeros((1, embedding_size))
                    emb_temp[0, :] = sess.run(embeddings,
                                              feed_dict={
                                                  image_placeholder:
                                                  image_reshaped,
                                                  phase_train_placeholder:
                                                  False
                                              })[0]

                    if x == 0:
                        people_sum += 1
                        output_peoplename = "{}{}".format(save_path, img)
                        misc.imsave(output_peoplename, image)
                        print("save new face")
                        img_list.append(image_reshaped)
                        emb_list.append(emb_temp[0, :])
                    else:
                        is_exist = False
                        for k in range(x):
                            dist = np.dot(emb_temp[0, :], emb_list[k]) / (
                                np.linalg.norm(emb_temp[0, :]) *
                                np.linalg.norm(emb_list[k]))
                            print(' %1.4f  ' % dist, end='')
                            if (dist > args.dist):
                                print("\n already existed as", k + 1)
                                is_exist = True
                                break

                        if not is_exist:
                            people_sum += 1
                            output_peoplename = "{}{}".format(save_path, img)
                            misc.imsave(output_peoplename, image)
                            print("save new face")
                            emb_list.append(emb_temp[0, :])
                            img_list.append(image_reshaped)

                duration = time.time() - start_time
                print("detect time:", duration)

    return people_sum
Ejemplo n.º 20
0
def detect_frame(dist_thre, capture_count, nrof_successfully_aligned, img,
                 img_list, emb_list, file_paths, minsize, threshold, factor,
                 save_path):
    output_dir_img = './datasets/mtcnn_160/img/'
    if not os.path.exists(output_dir_img):
        os.makedirs(output_dir_img)
    with tf.device('/gpu:0'):
        with tf.Graph().as_default():
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            with tf.Session(config=config) as sess:
                if len(file_paths) == 3:
                    image_pnet = tf.placeholder(tf.float32,
                                                [None, None, None, 3])
                    pnet = PNet({'data': image_pnet}, mode='test')
                    out_tensor_pnet = pnet.get_all_output()

                    image_rnet = tf.placeholder(tf.float32, [None, 24, 24, 3])
                    rnet = RNet({'data': image_rnet}, mode='test')
                    out_tensor_rnet = rnet.get_all_output()

                    image_onet = tf.placeholder(tf.float32, [None, 48, 48, 3])
                    onet = ONet({'data': image_onet}, mode='test')
                    out_tensor_onet = onet.get_all_output()

                    saver_pnet = tf.train.Saver([
                        v for v in tf.global_variables()
                        if v.name[0:5] == "pnet/"
                    ])
                    saver_rnet = tf.train.Saver([
                        v for v in tf.global_variables()
                        if v.name[0:5] == "rnet/"
                    ])
                    saver_onet = tf.train.Saver([
                        v for v in tf.global_variables()
                        if v.name[0:5] == "onet/"
                    ])

                    saver_pnet.restore(sess, file_paths[0])

                    def pnet_fun(img):
                        return sess.run(out_tensor_pnet,
                                        feed_dict={image_pnet: img})

                    saver_rnet.restore(sess, file_paths[1])

                    def rnet_fun(img):
                        return sess.run(out_tensor_rnet,
                                        feed_dict={image_rnet: img})

                    saver_onet.restore(sess, file_paths[2])

                    def onet_fun(img):
                        return sess.run(out_tensor_onet,
                                        feed_dict={image_onet: img})

                else:
                    saver = tf.train.import_meta_graph(file_paths[0])
                    saver.restore(sess, file_paths[1])

                    def pnet_fun(img):
                        return sess.run(
                            ('softmax/Reshape_1:0', 'pnet/conv4-2/BiasAdd:0'),
                            feed_dict={'Placeholder:0': img})

                    def rnet_fun(img):
                        return sess.run(('softmax_1/softmax:0',
                                         'rnet/conv5-2/rnet/conv5-2:0'),
                                        feed_dict={'Placeholder_1:0': img})

                    def onet_fun(img):
                        return sess.run(('softmax_2/softmax:0',
                                         'onet/conv6-2/onet/conv6-2:0',
                                         'onet/conv6-3/onet/conv6-3:0'),
                                        feed_dict={'Placeholder_2:0': img})

                # Add a random key to the filename to allow alignment using multiple processes
                random_key = np.random.randint(0, high=99999)
                output_dir_bbox = './datasets/mtcnn_160/bbox/'
                if not os.path.exists(output_dir_bbox):
                    os.makedirs(output_dir_bbox)
                bounding_boxes_filename = os.path.join(
                    output_dir_bbox, 'bounding_boxes_%05d.txt' % random_key)

                with open(bounding_boxes_filename, "w") as text_file:
                    start_time = time.time()
                    rectangles, points = detect_face(img, minsize, pnet_fun,
                                                     rnet_fun, onet_fun,
                                                     threshold, factor)
                    duration = time.time() - start_time

                    print("detect time:", duration)
                    print(type(rectangles))

                    nrof_faces = rectangles.shape[0]
                    if nrof_faces > 0:
                        facenet.load_model(
                            '20180408-102900/20180408-102900.pb')
                        #                         facenet.load_model('20190218-164145/20190218-164145.pb')
                        image_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]

                        det = rectangles[:, 0:4]
                        det_arr = []
                        img_size = np.asarray(img.shape)[0:2]
                        if nrof_faces > 1:
                            for i in range(nrof_faces):
                                det_arr.append(np.squeeze(det[i]))
                        else:
                            det_arr.append(np.squeeze(det))

                        for i, det in enumerate(det_arr):
                            output_filename = "{}{}{}".format(
                                output_dir_img, capture_count, '.png')
                            det = np.squeeze(det)
                            bb = np.zeros(4, dtype=np.int32)
                            bb[0] = np.maximum(det[0] - 32 / 2, 0)
                            bb[1] = np.maximum(det[1] - 32 / 2, 0)
                            bb[2] = np.minimum(det[2] + 32 / 2, img_size[1])
                            bb[3] = np.minimum(det[3] + 32 / 2, img_size[0])
                            cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                            scaled = misc.imresize(cropped, (160, 160),
                                                   interp='bilinear')
                            scaled = cv2.cvtColor(scaled, cv2.COLOR_BGR2RGB)
                            image = facenet.prewhiten(scaled)
                            image_reshaped = image.reshape(-1, 160, 160, 3)
                            emb_temp = np.zeros((1, embedding_size))
                            emb_temp[0, :] = sess.run(
                                embeddings,
                                feed_dict={
                                    image_placeholder: image_reshaped,
                                    phase_train_placeholder: False
                                })[0]

                            if len(os.listdir(output_dir_img)) == 0:
                                nrof_successfully_aligned += 1
                                output_peoplename = "{}{}{}".format(
                                    output_dir_img, nrof_successfully_aligned,
                                    '.png')
                                misc.imsave(output_peoplename, scaled)
                                print("\n save new.")
                                img_list.append(image_reshaped)
                                emb_list.append(emb_temp[0, :])
                            else:
                                x = len(os.listdir(output_dir_img))
                                is_exist = False
                                print(i + 1, 'face in capture', capture_count,
                                      ':')
                                for k in range(x):
                                    dist = np.sqrt(
                                        np.sum(
                                            np.square(
                                                np.subtract(
                                                    emb_temp[0, :],
                                                    emb_list[k]))))
                                    print(' %1.4f  ' % dist, end='')
                                    if (dist < dist_thre and dist > 0):
                                        print("\n already existed.")
                                        is_exist = True
                                        break

                                if not is_exist:
                                    nrof_successfully_aligned += 1
                                    output_peoplename = "{}{}{}".format(
                                        output_dir_img,
                                        nrof_successfully_aligned, '.png')
                                    misc.imsave(output_peoplename, scaled)
                                    print("\n save new.")
                                    emb_list.append(emb_temp[0, :])
                                    img_list.append(image_reshaped)

                            text_file.write(
                                '%s %d %d %d %d\n' %
                                (output_filename, bb[0], bb[1], bb[2], bb[3]))
                    else:
                        print('NO FACE in capture %d' % (capture_count))
                        text_file.write('%s\n' % (output_dir_img))

            points = np.transpose(points)
            for rectangle in rectangles:
                cv2.putText(img, str(rectangle[4]),
                            (int(rectangle[0]), int(rectangle[1])),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
                cv2.rectangle(img, (int(rectangle[0]), int(rectangle[1])),
                              (int(rectangle[2]), int(rectangle[3])),
                              (255, 0, 0), 2)
            for point in points:
                for i in range(0, 10, 2):
                    cv2.circle(img, (int(point[i]), int(point[i + 1])),
                               4, (255, 0, 255),
                               thickness=2)
            cv2.imwrite(save_path + str(capture_count) + '.jpg', img)


#             if cv2.waitKey(0) & 0xFF == ord('q'):
#                 cv2.destroyAllWindows()
    return rectangles, nrof_successfully_aligned, img_list, emb_list
Ejemplo n.º 21
0
def load_data(image_paths, image_size):
    nrof_samples = len(image_paths)
    images = np.zeros((nrof_samples, image_size, image_size, 3))
    for i in range(nrof_samples):
        images[i, :, :, :] = facenet.prewhiten(misc.imread(image_paths[i]))
    return images
Ejemplo n.º 22
0
                    scaled = None
                    scaled_reshape = None
                    bb = [0, 0, 0, 0]

                    emb_array = np.zeros((1, embedding_size))

                    bb[0] = int(np.maximum(bounding_boxes[i][0], 0))
                    bb[1] = int(np.maximum(bounding_boxes[i][1], 0))
                    bb[2] = int(np.minimum(bounding_boxes[i][2], img_size[1]))
                    bb[3] = int(np.minimum(bounding_boxes[i][3], img_size[0]))

                    cropped = frame[bb[1]:bb[3], bb[0]:bb[2], :]
                    scaled = cv2.resize(cropped,
                                        (input_image_size, input_image_size),
                                        interpolation=cv2.INTER_CUBIC)
                    scaled = facenet.prewhiten(scaled)
                    scaled_reshape = scaled.reshape(-1, input_image_size,
                                                    input_image_size, 3)
                    feed_dict = {
                        images_placeholder: scaled_reshape,
                        phase_train_placeholder: False
                    }
                    emb_array[0, :] = sess.run(embeddings, feed_dict=feed_dict)
                    predictions = model.predict_proba(emb_array)
                    frame = show_prediction_labels_on_image(
                        frame, bb, predictions, HumanNames)
            cv2.imshow('Video', frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
Ejemplo n.º 23
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--path',
                        help='Path of the video you want to test on.',
                        default=0)
    args = parser.parse_args()

    MINSIZE = 20
    THRESHOLD = [0.6, 0.7, 0.7]
    FACTOR = 0.709
    IMAGE_SIZE = 182
    INPUT_IMAGE_SIZE = 160
    CLASSIFIER_PATH = './Models/custom/custom.pkl'
    VIDEO_PATH = args.path
    FACENET_MODEL_PATH = './Models/facenet/20180402-114759.pb'

    # Load The Custom Classifier
    with open(CLASSIFIER_PATH, 'rb') as file:
        model, class_names = pickle.load(file)

    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():

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(FACENET_MODEL_PATH)

            update_bool(True)

            # Get input and output tensors
            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]

            pnet, rnet, onet = detect_face.create_mtcnn(sess, "src/align")

            people_detected = set()
            person_detected = collections.Counter()

            cap = cv2.VideoCapture(VIDEO_PATH)

            while (cap.isOpened()):
                ret, frame = cap.read()

                bounding_boxes, _ = detect_face.detect_face(
                    frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

                faces_found = bounding_boxes.shape[0]
                try:
                    if faces_found > 0:
                        det = bounding_boxes[:, 0:4]
                        bb = np.zeros((faces_found, 4), dtype=np.int32)
                        for i in range(faces_found):
                            bb[i][0] = det[i][0]
                            bb[i][1] = det[i][1]
                            bb[i][2] = det[i][2]
                            bb[i][3] = det[i][3]

                            cropped = frame[bb[i][1]:bb[i][3],
                                            bb[i][0]:bb[i][2], :]
                            scaled = cv2.resize(
                                cropped, (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),
                                interpolation=cv2.INTER_CUBIC)
                            scaled = facenet.prewhiten(scaled)
                            scaled_reshape = scaled.reshape(
                                -1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)
                            feed_dict = {
                                images_placeholder: scaled_reshape,
                                phase_train_placeholder: False
                            }
                            emb_array = sess.run(embeddings,
                                                 feed_dict=feed_dict)
                            predictions = model.predict_proba(emb_array)
                            best_class_indices = np.argmax(predictions, axis=1)
                            best_class_probabilities = predictions[
                                np.arange(len(best_class_indices)),
                                best_class_indices]
                            best_name = class_names[best_class_indices[0]]
                            print("Name: {}, Probability: {}".format(
                                best_name, best_class_probabilities))

                            cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                          (bb[i][2], bb[i][3]), (0, 255, 0), 2)
                            text_x = bb[i][0]
                            text_y = bb[i][3] + 20

                            if best_class_probabilities > 0.3:
                                name = class_names[best_class_indices[0]]
                            else:
                                name = "Unknown"

                            cv2.putText(frame,
                                        name, (text_x, text_y),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (255, 255, 255),
                                        thickness=1,
                                        lineType=2)
                            cv2.putText(frame,
                                        str(
                                            round(best_class_probabilities[0],
                                                  3)), (text_x, text_y + 17),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (255, 255, 255),
                                        thickness=1,
                                        lineType=2)
                            person_detected[best_name] += 1

                            is_open_to_speak = read_bool()
                            # print(is_open_to_speak)
                            if is_open_to_speak == '1':
                                try:
                                    update_bool(False)
                                    subprocess.Popen([
                                        'python', 'text_to_speech.py',
                                        best_name
                                    ])
                                except Exception as e:
                                    print(e)
                                    print("Error: unable to start thread")

                except Exception as e:
                    print(e)

                cv2.imshow('Face Recognition', frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            cap.release()
            cv2.destroyAllWindows()
Ejemplo n.º 24
0
 def no_align(image_path):
     img = misc.imread(image_path, mode='RGB')
     img = misc.imresize(img, (image_size, image_size), interp='bilinear')
     img = facenet.prewhiten(img)
     return img