Пример #1
0
    def find_faces(self, image):
        faces_raw = []
        faces = []
        
        if image.ndim < 2:
            print('Unable to align img, ndim = %d' % ndim)
            return faces, faces_raw
        if image.ndim == 2:
            image = facenet.to_rgb(image)
        image = image[:, :, 0:3]

        bounding_boxes, _ = align.detect_face.detect_face(image, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor)
        #nrof_faces = bounding_boxes.shape[0]
        #print("nrof_faces " + str(nrof_faces))
        img_size = np.asarray(image.shape)[0:2]
        for det in bounding_boxes:
            face = np.zeros((1, self.image_size_model, self.image_size_model, 3))
            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 = image[bb[1]:bb[3], bb[0]:bb[2], :]
            scaled = misc.imresize(cropped, (self.image_size, self.image_size), interp='bilinear')
            faces_raw.append(scaled)
            prewhiten = facenet.prewhiten(scaled)
            facecrop = facenet.crop(prewhiten, False, self.image_size_model)

            face[0,:,:,:] = facecrop
            faces.append(face)
        #self.save_img(faces_raw,"")
        return faces, faces_raw
Пример #2
0
def classify(image, sess):
    global model, class_names
    np.random.seed(seed=np.sum(image))

    # 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]

    # Run forward pass to calculate embeddings
    print('Calculating features for image')
    emb_array = np.zeros((1, embedding_size))

    images = np.zeros((1, 160, 160, 3))

    if image.ndim == 2:
        image = facenet.to_rgb(image)
    image = facenet.prewhiten(image)
    image = facenet.crop(image, False, 160)
    image = facenet.flip(image, False)
    images[0, :, :, :] = image

    feed_dict = {images_placeholder: images, phase_train_placeholder: False}
    emb_array[0:1, :] = sess.run(embeddings, feed_dict=feed_dict)

    ###
    predictions = model.predict_proba(emb_array)[0]
    best_class_indices = ind = np.argpartition(predictions, -3)[-3:]
    best_class_indices = best_class_indices[np.argsort(
        predictions[best_class_indices])][::-1]
    best_class_probabilities = predictions[best_class_indices]

    return np.array(class_names)[best_class_indices], best_class_probabilities
Пример #3
0
def align_data_with_bb(image_paths, image_size, margin, pnet, rnet, onet,image_size_for_pretrain):
    try:
        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 range(nrof_samples):
            # print (i)
            b_box = []
            img = misc.imread(os.path.expanduser(image_paths[i]))
            print ("246")
            print (img.shape)

            img_size = np.asarray(img.shape)[0:2]
            bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)

            print (bounding_boxes)
            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],:]

            cropped = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)

            img_to_norm = cv2.cvtColor(cropped, 37)
            img_to_norm[0] = cv2.equalizeHist(img_to_norm[0])
            prewhitened = cv2.cvtColor(img_to_norm, 39)
            res = cv2.fastNlMeansDenoisingColored(prewhitened, None, 10, 10, 7, 21)

            if len(b_box) == 0:
                print ("2")
                b_box.append(bb[0])
                b_box.append(bb[1])
                b_box.append(bb[2])
                b_box.append(bb[3])
                # cv2.rectangle(img,(bb[0],bb[1]),(bb[2],bb[3]),(255,0,0),2)
                crop_img = img[bb[1]:bb[3],bb[0]:bb[2],:]
                # return_img = img
                crop_img = facenet.crop(crop_img, None, image_size_for_pretrain)
                crop_img = facenet.flip(crop_img, None)
                # img = crop(img, do_random_crop, image_size)
                # img = flip(img, do_random_flip)

                resize=misc.imresize(crop_img, (image_size_for_pretrain, image_size_for_pretrain), interp='bilinear')
                return_img = resize
            aligned = misc.imresize(res, (image_size_for_pretrain, image_size_for_pretrain), interp='bilinear')
            prewhitened = facenet.prewhiten(aligned)
            img_list[i] = prewhitened
        images = np.stack(img_list)
        return images, return_img
    except:
        traceback.print_exc()
        pass
def load_image(img, image_size, do_prewhiten=True):
    image = np.zeros((1, image_size, image_size, 3))
    if img.ndim == 2:
        img = facenet.to_rgb(img)
    if do_prewhiten:
        img = facenet.prewhiten(img)
    img = facenet.crop(img, False, image_size)
    img = facenet.flip(img, False)
    image[0, :, :, :] = img
    return image
Пример #5
0
def resize_image(img, image_size, do_prewhiten=True):
    print(img.shape)
    if img.ndim == 2:
        img = to_rgb(img)
    if do_prewhiten:
        img = prewhiten(img)
    img = crop(img, False, image_size)
    img = flip(img, False)
    img = np.expand_dims(img, axis=0)
    #images[:,:,:,:] = img
    #return images
    return img
Пример #6
0
def load_images():
    dataset = facenet.get_dataset('dataset')
    paths, labels = facenet.get_image_paths_and_labels(dataset)

    img_list = []

    for image in paths:
        img = cv2.imread(image)  #reading
        img = facenet.crop(img, False, 160)  #crop
        prewhitened = facenet.prewhiten(img)  #some adjustments
        img_list.append(prewhitened)
    images = np.stack(img_list)
    return images
def load_data(img_paths, flip, img_size, do_prewhiten=True):
    m = len(img_paths)
    imgs = np.zeros((m, img_size, img_size, 3))
    for i in range(m):
        img = misc.imread(img_paths[i])
        if img.ndim == 2:
            img = facenet.to_rgb(img)
        if do_prewhiten:
            img = facenet.prewhiten(img)
        img = facenet.crop(img, False, image_size)
        if flip:
            img = np.fliplr(img)
        imgs[i, :, :, :] = img
    return imgs
Пример #8
0
 def prepare_images(self,
                    image,
                    box,
                    image_size,
                    do_random_crop=False,
                    do_random_flip=False,
                    do_prewhiten=True):
     img = np.array(image.crop(box=(box[0], box[1], box[2], box[3])))
     img = resize(img, (image_size, image_size))
     if img.ndim == 2:
         img = facenet.to_rgb(img)
     if do_prewhiten:
         img = prewhiten(img)
     img = facenet.crop(img, do_random_crop, image_size)
     img = facenet.flip(img, do_random_flip)
     return img
Пример #9
0
def get_face_in_frame(frame, aligned_list):
    images = np.zeros((len(aligned_list), img_size, img_size, 3))
    i = 0
    for face_pos in aligned_list:
        if face_pos[0] < 0 or face_pos[1] < 0:
            continue
        else:
            img = frame[face_pos[1]:face_pos[3], face_pos[0]:face_pos[2], ]
            if img.ndim == 2:
                img = facenet.to_rgb(img)
            img = misc.imresize(img, (img_size, img_size), interp='bilinear')
            img = facenet.prewhiten(img)
            img = facenet.crop(img, False, img_size)
            images[i, :, :, :] = img
            i += 1
    return images
Пример #10
0
def load_img(img,
             do_random_crop,
             do_random_flip,
             image_size,
             do_prewhiten=True):
    '''
    Process the captured images from the webcam, prewhitening, cropping and
    flipping as required. Returns processed image.
    '''
    images = np.zeros((1, image_size, image_size, 3))
    if img.ndim == 2:
        img = to_rgb(img)
    if do_prewhiten:
        img = prewhiten(img)
    img = crop(img, do_random_crop, image_size)
    img = flip(img, do_random_flip)
    images[:, :, :, :] = img
    return images
Пример #11
0
def recognize(model):
    g_recognition = tf.Graph()
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
    with g_recognition.as_default():
        with tf.Session(
                graph=g_recognition,
                config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
            # Load the model
            facenet.load_model(model)

            # Get input and output tensors
            images_placeholder = g_recognition.get_tensor_by_name("input:0")
            embeddings = g_recognition.get_tensor_by_name("embeddings:0")
            phase_train_placeholder = g_recognition.get_tensor_by_name(
                "phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            emb_array = np.zeros((1, embedding_size))
            images = np.zeros((1, 160, 160, 3))

            img = None

            while True:
                if img is None:
                    img = yield None

                img = crop(img, False, 160)
                img = flip(img, False)

                images[0, :, :, :] = img

                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                start_index = 0
                end_index = 1
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

                img = yield emb_array
Пример #12
0
    def image_array_align_data(self,
                               image_arr,
                               image_path,
                               image_size=160,
                               margin=32,
                               gpu_memory_fraction=1.0,
                               detect_multiple_faces=True):
        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)

        img = image_arr
        bounding_boxes, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        nrof_faces = bounding_boxes.shape[0]

        nrof_successfully_aligned = 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 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))

            images = np.zeros((nrof_faces, image_size, image_size, 3))
            for i, det in enumerate(det_arr):
                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], :]
                # 进行图片缩放 cv2.resize(img,(w,h))
                scaled = imresize(cropped, (image_size, image_size),
                                  interp='bilinear')
                nrof_successfully_aligned += 1

                # print(scaled)
                # scaled=self.prewhiten(scaled)
                # 保存检测的头像
                filename_base = './img/'
                filename = os.path.basename(image_path)
                filename_name, file_extension = os.path.splitext(filename)
                output_filename_n = "{}/{}_{}{}".format(
                    filename_base, filename_name, i, file_extension)
                imsave(output_filename_n, scaled)

                scaled = facenet.prewhiten(scaled)
                scaled = facenet.crop(scaled, False, 160)
                scaled = facenet.flip(scaled, False)

                images[i] = scaled
        if nrof_faces > 0:
            return images
        else:
            # 如果没有检测到人脸  直接返回一个1*3的0矩阵  多少维度都行  只要能和是不是一个图片辨别出来就行
            return np.zeros((1, 3))