Example #1
0
def align_face(pic_path):
    if os.path.exists(pic_path):
        try:
            img = misc.imread(pic_path)
        except (IOError, ValueError, IndexError) as e:
            errorMessage = '{}: {}'.format(pic_path, e)
            print(errorMessage)
        if img.ndim < 2:
            print('Unable to align "%s"' % pic_path)
            return
        if img.ndim == 2:
            img = facenet.to_rgb(img)
        img = img[:, :, 0:3]
        bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet,
                                                    onet, threshold, factor)
        nrof_faces = bounding_boxes.shape[0]

        if nrof_faces > 0:
            det = bounding_boxes[:, 0:4]
            img_size = np.asarray(img.shape)[0:2]
            if nrof_faces > 1:
                bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                               det[:, 1])
                img_center = img_size / 2
                offsets = np.vstack([
                    (det[:, 0] + det[:, 2]) / 2 - img_center[1],
                    (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                ])
                offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                index = np.argmax(bounding_box_size - offset_dist_squared *
                                  2.0)  # some extra weight on the centering
                det = det[index, :]
            det = np.squeeze(det)
            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], :]
            scaled = misc.imresize(cropped, (image_size, image_size),
                                   interp='bilinear')
            misc.imsave(pic_path, scaled)
def align_face(pic_path):
    if os.path.exists(pic_path):
        try:
            img = misc.imread(pic_path)
        except (IOError, ValueError, IndexError) as e:
            errorMessage = '{}: {}'.format(pic_path, e)
            print(errorMessage)
        if img.ndim < 2:
            print('Unable to align "%s"' % pic_path)
            return
        if img.ndim == 2:
            img = facenet.to_rgb(img)
        img = img[:, :, 0:3]
        bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
        nrof_faces = bounding_boxes.shape[0]

        if nrof_faces > 0:
            det = bounding_boxes[:, 0:4]
            img_size = np.asarray(img.shape)[0:2]
            if nrof_faces > 1:
                bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1])
                img_center = img_size / 2
                offsets = np.vstack(
                        [(det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
                offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                index = np.argmax(bounding_box_size - offset_dist_squared * 2.0)  # some extra weight on the centering
                det = det[index, :]
            det = np.squeeze(det)
            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], :]
            scaled = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
            misc.imsave(pic_path, scaled)
def main(args):
    sleep(random.random())
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(args.input_dir)

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=args.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)

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

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(
        output_dir, 'bounding_boxes_%05d.txt' % random_key)

    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir,
                                               filename + '.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:, :, 0:3]

                        bounding_boxes, _ = align.detect_face.detect_face(
                            img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces > 0:
                            det = bounding_boxes[:, 0:4]
                            det_arr = []
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces > 1:
                                if args.detect_multiple_faces:
                                    for i in range(nrof_faces):
                                        det_arr.append(np.squeeze(det[i]))
                                else:
                                    bounding_box_size = (
                                        det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                                  det[:, 1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([
                                        (det[:, 0] + det[:, 2]) / 2 -
                                        img_center[1],
                                        (det[:, 1] + det[:, 3]) / 2 -
                                        img_center[0]
                                    ])
                                    offset_dist_squared = np.sum(
                                        np.power(offsets, 2.0), 0)
                                    index = np.argmax(
                                        bounding_box_size -
                                        offset_dist_squared * 2.0
                                    )  # some extra weight on the centering
                                    det_arr.append(det[index, :])
                            else:
                                det_arr.append(np.squeeze(det))

                            for i, det in enumerate(det_arr):
                                det = np.squeeze(det)
                                bb = np.zeros(4, dtype=np.int32)
                                bb[0] = np.maximum(det[0] - args.margin / 2, 0)
                                bb[1] = np.maximum(det[1] - args.margin / 2, 0)
                                bb[2] = np.minimum(det[2] + args.margin / 2,
                                                   img_size[1])
                                bb[3] = np.minimum(det[3] + args.margin / 2,
                                                   img_size[0])
                                cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                                scaled = misc.imresize(
                                    cropped,
                                    (args.image_size, args.image_size),
                                    interp='bilinear')
                                nrof_successfully_aligned += 1
                                filename_base, file_extension = os.path.splitext(
                                    output_filename)
                                if args.detect_multiple_faces:
                                    output_filename_n = "{}_{}{}".format(
                                        filename_base, i, file_extension)
                                else:
                                    output_filename_n = "{}{}".format(
                                        filename_base, file_extension)
                                misc.imsave(output_filename_n, scaled)
                                text_file.write('%s %d %d %d %d\n' %
                                                (output_filename_n, bb[0],
                                                 bb[1], bb[2], bb[3]))
                        else:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)
Example #4
0
    def process_data(self, class_name, image_paths, text_file, result_file):
        output_class_dir = os.path.join(self.out_image_dir, class_name)
        tf.io.gfile.mkdir(output_class_dir)
        if self.random_order:
            random.shuffle(image_paths)
        for image_path in image_paths:
            file = os.path.split(image_path)[1]
            filename = os.path.splitext(file)[0]
            output_filename = os.path.join(output_class_dir, filename + '.png')
            tmp_path = os.path.join(self.tmp_dir, file)
            tf.io.gfile.copy(image_path, tmp_path, True)

            try:
                # img = misc.imread(tmp_path)
                img = imread(tmp_path)
            except (IOError, ValueError, IndexError) as e:
                error_message = '{}: {}\n'.format(image_path, e)
                result_file.write(error_message)
            else:
                if img.ndim < 2:
                    result_file.write('Unable to align "%s"' % image_path)
                    text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = facenet.to_rgb(img)
                img = img[:, :, 0:3]

                bounding_boxes, _ = detect_face.detect_face(
                    img, self.minsize, self.pnet, self.rnet, self.onet,
                    self.threshold, self.factor)
                nrof_faces = bounding_boxes.shape[0]
                if nrof_faces > 0:
                    det = bounding_boxes[:, 0:4]
                    det_arr = []
                    img_size = np.asarray(img.shape)[0:2]
                    if nrof_faces > 1:
                        if self.detect_multiple_faces:
                            for i in range(nrof_faces):
                                det_arr.append(np.squeeze(det[i]))
                        else:
                            bounding_box_size = (det[:, 2] - det[:, 0]) * (
                                det[:, 3] - det[:, 1])
                            img_center = img_size / 2
                            offsets = np.vstack([
                                (det[:, 0] + det[:, 2]) / 2 - img_center[1],
                                (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                            ])
                            offset_dist_squared = np.sum(
                                np.power(offsets, 2.0), 0)
                            index = np.argmax(bounding_box_size -
                                              offset_dist_squared * 2.0)
                            det_arr.append(det[index, :])
                    else:
                        det_arr.append(np.squeeze(det))

                    for i, det in enumerate(det_arr):
                        det = np.squeeze(det)
                        bb = np.zeros(4, dtype=np.int32)
                        bb[0] = np.maximum(det[0] - self.margin / 2, 0)
                        bb[1] = np.maximum(det[1] - self.margin / 2, 0)
                        bb[2] = np.minimum(det[2] + self.margin / 2,
                                           img_size[1])
                        bb[3] = np.minimum(det[3] + self.margin / 2,
                                           img_size[0])
                        cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                        # scaled = misc.imresize(cropped, (self.image_size, self.image_size), interp='bilinear')
                        scaled = np.array(
                            Image.fromarray(cropped).resize(
                                (self.image_size, self.image_size),
                                resample=Image.BILINEAR))
                        filename_base, file_extension = os.path.splitext(
                            tmp_path)
                        if self.detect_multiple_faces:
                            tmp_filename_n = "{}_{}{}".format(
                                filename_base, i, file_extension)
                        else:
                            tmp_filename_n = "{}{}".format(
                                filename_base, file_extension)
                        # misc.imsave(tmp_filename_n, scaled)
                        imsave(tmp_filename_n, scaled)
                        output_filename_n = os.path.join(
                            output_class_dir,
                            os.path.split(tmp_filename_n)[1])
                        tf.io.gfile.copy(tmp_filename_n, output_filename_n,
                                         True)
                        text_file.write(
                            '%s %d %d %d %d\n' %
                            (output_filename_n, bb[0], bb[1], bb[2], bb[3]))
                else:
                    result_file.write('Unable to align "%s"' % image_path)
                    text_file.write('%s\n' % (output_filename))
Example #5
0
def gen(camera):
    sess = tf.Session()
    with sess.as_default():
        pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
        facenet.load_model(
            '/home/rohitner/models/facenet/20180402-114759/20180402-114759.pb')
        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")

        classifier_filename_exp = '/home/rohitner/models/lfw_classifier.pkl'
        with open(classifier_filename_exp, 'rb') as infile:
            (model, class_names) = pickle.load(infile)
        print('Loaded classifier model from file "%s"' %
              classifier_filename_exp)

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

        while True:
            success, img = camera.read()
            results = tfnet.return_predict(img)
            for result in results:
                cv2.rectangle(
                    img, (result["topleft"]["x"], result["topleft"]["y"]),
                    (result["bottomright"]["x"], result["bottomright"]["y"]),
                    (255, 0, 0), 4)
                text_x, text_y = result["topleft"]["x"] - 10, result[
                    "topleft"]["y"] - 10

                cv2.putText(img, result["label"], (text_x, text_y),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2,
                            cv2.LINE_AA)
            if img.ndim < 2:
                print('Unable to align')
                continue
            if img.ndim == 2:
                img = facenet.to_rgb(img)
            img = img[:, :, 0:3]
            bounding_boxes, _ = detect_face.detect_face(
                img, minsize, pnet, rnet, onet, threshold, factor)
            nrof_faces = bounding_boxes.shape[0]
            if nrof_faces > 0:
                det = bounding_boxes[:, 0:4]
                det_arr = []
                img_size = np.asarray(img.shape)[0:2]
                if nrof_faces > 1:
                    if True:  # args.detect_multiple_faces:
                        for i in range(nrof_faces):
                            det_arr.append(np.squeeze(det[i]))
                    else:
                        bounding_box_size = (det[:, 2] - det[:, 0]) * (
                            det[:, 3] - det[:, 1])
                        img_center = img_size / 2
                        offsets = np.vstack([
                            (det[:, 0] + det[:, 2]) / 2 - img_center[1],
                            (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                        ])
                        offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                        index = np.argmax(
                            bounding_box_size - offset_dist_squared *
                            2.0)  # some extra weight on the centering
                        det_arr.append(det[index, :])
                else:
                    det_arr.append(np.squeeze(det))
                for i, det in enumerate(det_arr):
                    det = np.squeeze(det)
                    bb = np.zeros(4, dtype=np.int32)
                    bb[0] = np.maximum(det[0] - 44 / 2, 0)
                    bb[1] = np.maximum(det[1] - 44 / 2, 0)
                    bb[2] = np.minimum(det[2] + 44 / 2, img_size[1])
                    bb[3] = np.minimum(det[3] + 44 / 2, img_size[0])
                    cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                    scaled = misc.imresize(cropped, (160, 160),
                                           interp='bilinear')
                    scaled = prewhiten_and_expand(scaled)
                    emb = sess.run(embeddings,
                                   feed_dict={
                                       images_placeholder: scaled,
                                       phase_train_placeholder: False
                                   })
                    predictions = model.predict_proba(emb)
                    best_class_indices = np.argmax(predictions)
                    best_class_probabilities = predictions[0,
                                                           best_class_indices]
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    cv2.rectangle(img, (bb[0], bb[1]), (bb[2], bb[3]),
                                  (0, 255, 0), 5)
                    cv2.putText(img, class_names[best_class_indices],
                                (bb[0], bb[1] - 10), font, 0.5, (255, 0, 0), 2,
                                cv2.LINE_AA)
            else:
                print('No face detected')

            ret, jpeg = cv2.imencode('.jpg', img)
            frame = jpeg.tobytes()

            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
Example #6
0
def align(input_dir='database',
          output_dir='database_aligned',
          image_size=182,
          margin=44,
          gpu_memory_fraction=1,
          random_order=False):
    sleep(random.random())
    output_dir = os.path.expanduser(output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Store some git revision info in a text file in the log directory
    # src_path, _ = os.path.split(os.path.realpath(__file__))
    cwd = os.getcwd()
    facenet.store_revision_info(cwd, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(input_dir)
    print(input_dir)
    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)  # noqa: E501
        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)

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

    # Add a random key to the filename to allow alignment using multiple
    # processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(
        output_dir, 'bounding_boxes_%05d.txt' % random_key)  # noqa: E501

    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir,
                                               filename + '.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = imageio.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:, :, 0:3]

                        bounding_boxes, _ = detect_face.detect_face(
                            img, minsize, pnet, rnet, onet, threshold,
                            factor)  # noqa: E501

                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces > 0:
                            det = bounding_boxes[:, 0:4]
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces > 1:
                                bounding_box_size = (det[:, 2] - det[:, 0]) * (
                                    det[:, 3] - det[:, 1])  # noqa: E501
                                img_center = img_size / 2
                                offsets = np.vstack([
                                    (det[:, 0] + det[:, 2]) / 2 -
                                    img_center[1],
                                    (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                                ])  # noqa: E501
                                offset_dist_squared = np.sum(
                                    np.power(offsets, 2.0), 0)  # noqa: E501
                                index = np.argmax(
                                    bounding_box_size -
                                    offset_dist_squared * 2.0
                                )  # noqa: E501  # some extra weight on the centering
                                det = det[index, :]
                            det = np.squeeze(det)
                            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], :]
                            scaled = resize(cropped, (image_size, image_size),
                                            mode='constant')
                            nrof_successfully_aligned += 1
                            # convert image to uint8 before saving
                            imageio.imwrite(output_filename,
                                            img_as_ubyte(scaled))
                            text_file.write('%s %d %d %d %d\n' %
                                            (output_filename, bb[0], bb[1],
                                             bb[2], bb[3]))  # noqa: E501
                        else:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)  # noqa: E501