Exemplo n.º 1
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 = [] 
    count_per_image = []
    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, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
        count_per_image.append(len(bounding_boxes))
        for j in range(len(bounding_boxes)):	
                det = np.squeeze(bounding_boxes[j,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.append(prewhitened)		
    images = np.stack(img_list)
    return images, count_per_image, nrof_samples
Exemplo n.º 2
0
def load_and_align_data(image_paths, image_size, align, landmarkIndices):
    nrof_samples = len(image_paths)
    img_list = [None] * nrof_samples
    for i in xrange(nrof_samples):
        img = misc.imread(image_paths[i])
        aligned = align.align(image_size, img, landmarkIndices=landmarkIndices, skipMulti=True)
        prewhitened = facenet.prewhiten(aligned)
        img_list[i] = prewhitened
    images = np.stack(img_list)
    return images
Exemplo n.º 3
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]
Exemplo n.º 4
0
    def compare_with_templates(self,images):
        '''
        compare input images with in_build templates

        :param images: images list which want to compare with in_build templates
        :type: a list of opencv_image
        :return:
            all_label:a list of label corresponding to the order of input image lists
            all_belief_list:a list of belief_list,
            bounding_box_list:a list of bounding box show where the face is

            None, None, None if angthing wrong in process
        '''
        images_number = len(images)
        img_list = [None]*images_number
        bounding_box_list = [None]*images_number
        for i in xrange(images_number):
            print i
            aligned,bounding_box = self.__align.align(self.__image_size, images[i], landmarkIndices=self.__landmarkIndices, skipMulti=False)
            if aligned == None:
                return None,None,None
            prewhitened = facenet.prewhiten(aligned)
            img_list[i] = prewhitened
            bounding_box_list[i] = bounding_box
        img_array = np.asarray(img_list)
        feed_dict = {self.__images_placeholder: img_array, self.__phase_train_placeholder: False}
        emb = self.__sess.run(self.__embeddings, feed_dict=feed_dict)

        if not emb == None:
            all_belief_list = [None]*images_number
            all_label = [None]*images_number
            for i in xrange(images_number):
                dis_list = [None]*self.__templates_number
                for j in range(0, self.__templates_number):
                    dist = np.sqrt(np.mean(np.square(np.subtract(self.__templates_emb[j, :], emb[i, :]))))
                    dis_list[j] = dist
                dis_array = np.array(dis_list)
                indicate = np.argmin(dis_array)

                dis_belief = np.exp(-3*dis_array)
                label = self.__templates_label[indicate]
                all_belief_list[i] = dis_belief
                all_label[i] = label
            return all_label,all_belief_list,bounding_box_list
        else :
            return None,None,None
Exemplo 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
    face_lib = "/home/xuhao/project/face_recognition/face_lib"
    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(
            os.path.join(face_lib, image_paths[i])),
                          mode='RGB')
        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='lanczos')
        # plt.figure(i)
        #plt.imshow(aligned)
        #I.save(str(i)+'.png')
        prewhitened = facenet.prewhiten(aligned)
        img_list[i] = prewhitened
    images = np.stack(img_list)
    #plt.show()
    return images
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
Exemplo n.º 7
0
    def load_and_align_data(self, image_paths, image_size, margin):

        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))
            sess = tf.Session()
            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:
            print(image)
            img = misc.imread(os.path.expanduser(image), mode='RGB')
            # img = misc.imread(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:
                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)
            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位置对原图resize,并对新得的aligned进行白化预处理
            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
Exemplo n.º 8
0
def get_embeddings(image_path, name, margin=44, image_size=160):
    """image file -> embeddings"""

    pnet, rnet, onet = init_mtcnn()

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

    img_list = []
    emb = {}

    if isinstance(image_path, str):
        img = imageio.imread(
            os.path.expanduser(os.path.join(image_path)), pilmode="RGB"
        )
    img_size = np.asarray(img.shape)[0:2]
    bounding_boxes, points = align.detect_face.detect_face(
        img, minsize, pnet, rnet, onet, threshold, factor
    )
    if len(bounding_boxes) < 1:
        #         img_file_dict[folder].remove(image)
        print("can't detect face, end")
        return

    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 = np.array(
        Image.fromarray(cropped).resize((image_size, image_size), Image.BILINEAR)
    ).astype(np.double)
    prewhitened = facenet.prewhiten(aligned)
    img_list.append(prewhitened)

    if img_list:
        emb[name] = np.stack(img_list)
    return face_2_embeddings(emb, use_num_key=False)
 def load_and_align_data(self, img, image_size):
     minsize = 20
     threshold = [0.6, 0.7, 0.7]
     factor = 0.709
     # bounding_boxes shape:(1,5)  type:np.ndarray
     bounding_boxes, _ = align.detect_face.detect_face(
         img, minsize, pnet, rnet, onet, threshold, factor)
     # 如果未发现目标 直接返回
     if len(bounding_boxes) < 1:
         return 0, 0, 0
     det = bounding_boxes
     det = det.astype(int)
     crop = []
     for i in range(len(bounding_boxes)):
         temp_crop = img[det[i, 1]:det[i, 3], det[i, 0]:det[i, 2], :]
         aligned = misc.imresize(temp_crop, (image_size, image_size))
         prewhitened = facenet.prewhiten(aligned)
         crop.append(prewhitened)
     # np.stack 将crop由一维list变为二维
     crop_image = np.stack(crop)
     return 1, det, crop_image  # mark标记位置,回归边框,切割图片
Exemplo n.º 10
0
    def execute(self, columns):
        [img, bboxes] = columns
        [h, w] = img.shape[:2]

        out_size = 160
        bboxes = readers.bboxes(bboxes, self.protobufs)
        outputs = ''
        for bbox in bboxes:
            face_img = img[int(h * bbox.y1):int(h * bbox.y2),
                           int(w * bbox.x1):int(w * bbox.x2)]
            face_img = cv2.resize(face_img, (out_size, out_size))
            face_img = facenet.prewhiten(face_img)
            embs = self.sess.run(self.embeddings,
                                 feed_dict={
                                     self.images_placeholder: [face_img],
                                     self.phase_train_placeholder: False
                                 })

            outputs += embs[0].tobytes()

        return [outputs]
Exemplo n.º 11
0
def load_and_align_data(image_path,image_size,margin): #margin为要剪裁的余量

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

    img = misc.imread(os.path.expanduser(image_path))  # 读取图片
    img_size = np.asarray(img.shape)[0:2]  # img[0]为宽度,img[1]为高度
    bounding_boxes, _ = align.detect_face.detect_face(img, minisize, pnet, rnet, onet, threshold,
                                                      factor)  # 读取并对齐人脸,bounding_boxes为人脸框数组,形状为[n,5],n代表边框数,这里一般只有1张人脸,5对应x1,y1,x2,y2,score,分别是左上角横坐标,左上角纵坐标,右下角横坐标,右下角纵坐标,人脸置信度
    det=np.squeeze(bounding_boxes[0,0:4])          #假设图片里的人脸数为1,所以这里要去除边框数那一维
    bb=np.zeros(4,dtype=np.int32)            #bb为要剪裁的人脸区域
    bb[0]=np.maximum(det[0]-margin/2,0)      #左上角x1
    bb[1]=np.maximum(det[1]-margin/2,0)      #左上角y1
    bb[2]=np.minimum(det[2]+margin/2,img_size[1])      #右下角x2
    bb[3]=np.minimum(det[3]+margin/2,img_size[0])      #右下角y2
    cropped=img[bb[1]:bb[3],bb[0]:bb[2],:]    #根据bb来裁剪原图片中的人脸
    aligned=misc.imresize(cropped,(image_size,image_size),interp='bilinear')   #将图片缩放为卷积神经网络模型输入的大小
    prewhitened=facenet.prewhiten(aligned)     #对裁剪出的人脸进行图片标准化处理

    return prewhitened
Exemplo n.º 12
0
 def getFaces(self):
     minsize = 20 # minimum size of face
     threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
     factor = 0.709 # scale factor
     
     faces = []
     img = misc.imread(self.img_path)
     img_size = np.asarray(img.shape)[0:2]
     bounding_boxes, _ = align.detect_face.detect_face(img, minsize, self.pnet, self.rnet, self.onet, threshold, factor)
     for box in bounding_boxes:
         det = np.squeeze(box[0:4])
         bb = np.zeros(4, dtype=np.int32)
         bb[0] = np.maximum(det[0] - 4, 0)
         bb[1] = np.maximum(det[1] - 4, 0)
         bb[2] = np.minimum(det[2] + 4, img_size[1])
         bb[3] = np.minimum(det[3] + 4, img_size[0])
         cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
         aligned = misc.imresize(cropped, (160, 160), interp='bilinear')
         prewhitened = facenet.prewhiten(aligned)
         faces.append(prewhitened)
     return faces
Exemplo n.º 13
0
def findName(i):
    cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
    if len(cropped) > i:
        try:
            cropped[i] = facenet.flip(cropped[i], False)
            scaled.append(
                misc.imresize(cropped[i], (image_size, image_size),
                              interp='bilinear'))
            scaled[i] = cv2.resize(scaled[i],
                                   (input_image_size, input_image_size),
                                   interpolation=cv2.INTER_CUBIC)
            scaled[i] = facenet.prewhiten(scaled[i])
            scaled_reshape.append(scaled[i].reshape(-1, input_image_size,
                                                    input_image_size, 3))
            feed_dict = {
                images_placeholder: scaled_reshape[i],
                phase_train_placeholder: False
            }
            emb_array[0, :] = sess.run(embeddings, feed_dict=feed_dict)
            predictions = model.predict_proba(emb_array)
            print(predictions)
            best_class_indices = np.argmax(predictions, axis=1)
            print(best_class_indices)
            best_class_probabilities = predictions[
                np.arange(len(best_class_indices)), best_class_indices]
            print(best_class_probabilities)

            print('result: ', best_class_indices[0])
            print(best_class_indices)
            print(HumanNames)
            for H_i in HumanNames:
                print(H_i)
                if len(HumanNames) >= best_class_indices[0]:
                    if HumanNames[best_class_indices[0]] == H_i:
                        return HumanNames[best_class_indices[0]]
                else:
                    return "bilinmiyor"
        except:
            print("birşey oldu")
    pass
Exemplo n.º 14
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 = []
    count_per_image = []
    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, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        count_per_image.append(len(bounding_boxes))
        for j in range(len(bounding_boxes)):
            det = np.squeeze(bounding_boxes[j, 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')

            # aligned = misc.imread("adversarial0.85-0.jpg")
            prewhitened = facenet.prewhiten(aligned)
            img_list.append(prewhitened)
    images = np.stack(img_list)
    return images, count_per_image, nrof_samples
Exemplo n.º 15
0
def collect_data(img):  
    detector = mtcnn.MTCNN()
    image_size = detector.image_size
    input_image_size = detector.input_image_size
                                                    
    bounding_boxes, img = detector.run_mtcnn(img)
    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)
            det = det[index, :]
                
        det = np.squeeze(det)
        bb_temp = np.zeros(4, dtype = np.int32)

        bb_temp[0] = det[0]
        bb_temp[1] = det[1]
        bb_temp[2] = det[2]
        bb_temp[3] = det[3]

        cropped_temp = img[bb_temp[1] : bb_temp[3],
                               bb_temp[0] : bb_temp[2], :]
        cropped_temp = facenet.flip(cropped_temp, False)
        scaled_temp = misc.imresize(cropped_temp, (image_size, image_size), interp = 'bilinear')
        scaled_temp = cv2.resize(scaled_temp, (input_image_size, input_image_size),
                       interpolation = cv2.INTER_CUBIC)
        scaled_temp = facenet.prewhiten(scaled_temp)
        scaled_reshape = scaled_temp.reshape(-1, input_image_size, input_image_size, 3)

    return scaled_reshape
Exemplo n.º 16
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)
    #image_paths图像路径,nrof_samples图片数量
    nrof_samples = len(image_paths)
    #img_list对齐后图片
    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]
        #获取人脸包围盒,MTCNN检测、对齐
        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
Exemplo n.º 17
0
def load_and_align_data(image_path):

    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_list = []
    print(image_path)
    img = misc.imread(image_path, mode='RGB')

    img_size = np.asarray(img.shape)[0:2]
    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)
    else:
        for bounding_box in bounding_boxes:
            det = np.squeeze(bounding_box)
            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.append(prewhitened)
    return img_list, bounding_boxes
Exemplo n.º 18
0
    def computeFeatureBytes(imageB64):
        """
        :param imageB64: 图片base64编码
        :return: 返回128纬特征向量 list
        """
        image_size = 299  # don't need equal to real image size, but this value should not small than this

        embedding_size = embeddings.get_shape()[1]
        scaled_reshape = []
        # 解码base64图片
        nparr = np.fromstring(base64.b64decode(imageB64), np.uint8)
        img = cv2.imdecode(nparr, cv2.COLOR_BGR2RGB)

        image = cv2.resize(img, (image_size, image_size),
                           interpolation=cv2.INTER_CUBIC)
        image = facenet.prewhiten(image)
        scaled_reshape.append(image.reshape(-1, image_size, image_size, 3))
        emb_array = np.zeros((1, embedding_size))
        emb_array[0, :] = \
        sess.run(embeddings, feed_dict={images_placeholder: scaled_reshape[0], phase_train_placeholder: False})[0]

        return emb_array[0].tolist()
Exemplo n.º 19
0
def align_data(image_paths, 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
    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
Exemplo n.º 20
0
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction):
    nrof_samples = len(image_paths)
    img_list = []
    count_per_image = []
    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, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
        count_per_image.append(len(bounding_boxes))
        for j in range(len(bounding_boxes)):
            det = np.squeeze(bounding_boxes[j,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.append(prewhitened)
    images = np.stack(img_list)
    return images, count_per_image, nrof_samples
Exemplo n.º 21
0
def load_img(files):
    minsize = 20
    threshold = [0.6,0.7,0.7]
    factor = 0.709
    result = {}
    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)
    for image in files:
        if image == '.DS_Store':
            continue
        aligned = mtcnn(image,minsize,pnet,rnet,onet,threshold,factor)
        if aligned is None:
            files.remove(image)
            continue
        prewhitened = facenet.prewhiten(aligned)  # 图片进行白化
        result[image.split('.')[0]] = prewhitened
    return result
Exemplo n.º 22
0
def align_data(image_list, image_size, margin, area, 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)):
        if image_list[x][1].ndim == 2:
            image_list[x][1] = facenet.to_rgb(image_list[x][1])
        image_list[x][1] = image_list[x][1][:, :, 0:3]
        aligned = misc.imresize(image_list[x][1], (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        prewhitened.reshape(-1, image_size, image_size, 3)
        img_list.append(prewhitened)

    if len(img_list) > 0:
        images = np.stack(img_list)
        return images
    else:
        return None
Exemplo n.º 23
0
def face_detection(image, pnet, rnet, onet, margin=44, image_size=160):
    """Detect face"""

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

    img_list = []

    # img = imageio.imread(os.path.expanduser(image), pilmode="RGB")
    img = image
    img_size = np.asarray(img.shape)[0:2]
    bounding_boxes, points = align.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")
        return None, None, None
    print(f"bound_boxes: {bounding_boxes}")
    print(f"points: {points}")

    for box in bounding_boxes:
        det = np.squeeze(box[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')
        aligned = np.array(
            Image.fromarray(cropped).resize((image_size, image_size), Image.BILINEAR)
        ).astype(np.double)
        prewhitened = facenet.prewhiten(aligned)
        img_list.append(prewhitened)

    images = np.stack(img_list)
    return bounding_boxes, points, images
Exemplo n.º 24
0
def get_face_image(img, 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_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:
        return None

    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)
    return prewhitened, bb
Exemplo n.º 25
0
def EmbeddingFace(ImgPath):
    with tf.Graph().as_default():
        with tf.Session() as sess:
            # 加载模型
            model = './20181021/'
            facenet.load_model(model)
            # 获取输入输出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")

            img = misc.imread(ImgPath, mode='RGB')
            image = facenet.prewhiten(img)
            feed_dict = {
                images_placeholder: image,
                phase_train_placeholder: False
            }
            img_emb = sess.run(embeddings, feed_dict=feed_dict)
    return img_emb
Exemplo n.º 26
0
def extract_feature(image_file, face_size=160, margin=44, gpu_memory_fraction=0.6):
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    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)
    image = misc.imread(image_file, mode="RGB")
    image_size = image.shape[0:2]
    bounding_boxes, _ = align.detect_face.detect_face(image, minsize, pnet, rnet, onet, threshold, factor)
    if len(bounding_boxes) < 1:
        print("\ncan't detect face, ignore ", image_file)
        return None
    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, image_size[1])
    bb[3] = np.minimum(det[3]+margin/2, image_size[0])
    cropped = image[bb[1]:bb[3], bb[0]:bb[2], :]
    aligned = misc.imresize(cropped, (face_size, face_size), interp='bilinear')
    prewhitened = facenet.prewhiten(aligned)
    face_tensor = prewhitened[np.newaxis, :, :, :]
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) as sess:
            # Load the model
            facenet.load_model(facenet_model)
            # 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")
            # Run forward pass to calculate embeddings
            feed_dict = {images_placeholder: face_tensor, phase_train_placeholder: False}
            print("Begins to compute embeddings...")
            face_embedding = sess.run(embeddings, feed_dict=feed_dict)
    return face_embedding[0]
Exemplo n.º 27
0
def vector(name_list):
    image_vector = []  #照片向量矩阵

    for i in range(len(name_list)):
        scaled_reshape = []
        image1 = scipy.misc.imread(name_list[i], mode='RGB')
        image1 = cv2.resize(image1, (image_size, image_size),
                            interpolation=cv2.INTER_CUBIC)
        image1 = facenet.prewhiten(image1)
        scaled_reshape.append(image1.reshape(-1, image_size, image_size, 3))
        emb_array1 = np.zeros((1, embedding_size))
        emb_array1[0, :] = sess.run(embeddings,
                                    feed_dict={
                                        images_placeholder: scaled_reshape[0],
                                        phase_train_placeholder: False
                                    })[0]
        #print(emb_array1)
        image_vector.append(emb_array1)
        if i % 50 == 0:
            print('已读取', i)
    # sess.run(embeddings, feed_dict={images_placeholder: scaled_reshape[0], phase_train_placeholder: False})[0]
    return image_vector
Exemplo n.º 28
0
def load_and_align_data(img, 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_size = np.asarray(img.shape)[0:2]

    # bounding_boxes shape:(1,5)  type:np.ndarray
    bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet,
                                                      onet, threshold, factor)
    if len(bounding_boxes) < 1:
        return 0, 0, 0

    # det = np.squeeze(bounding_boxes[:,0:4])
    det = bounding_boxes

    # print('det shape type')
    # print(det.shape)
    # print(type(det))

    det[:, 0] = np.maximum(det[:, 0] - margin / 2, 0)
    det[:, 1] = np.maximum(det[:, 1] - margin / 2, 0)
    det[:, 2] = np.minimum(det[:, 2] + margin / 2, img_size[1] - 1)
    det[:, 3] = np.minimum(det[:, 3] + margin / 2, img_size[0] - 1)

    det = det.astype(int)
    crop = []
    for i in range(len(bounding_boxes)):
        temp_crop = img[det[i, 1]:det[i, 3], det[i, 0]:det[i, 2], :]
        aligned = misc.imresize(temp_crop, (image_size, image_size),
                                interp='bilinear')
        prewhitened = facenet.prewhiten(aligned)
        crop.append(prewhitened)

    crop_image = np.stack(crop)

    return 1, det, crop_image
Exemplo n.º 29
0
def load_and_align_data(images,
                        image_size=160,
                        margin=44,
                        gpu_memory_fraction=1.0):

    print("start load!")
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    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_list = []
    for img in images:
        img = img[:, :, ::-1]
        img_size = np.asarray(img.shape)[0:2]
        bounding_boxes, _ = align.detect_face.detect_face(
            img, minsize, pnet, rnet, onet, threshold, factor)
        for j in range(len(bounding_boxes)):
            det = np.squeeze(bounding_boxes[j, 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.append(prewhitened)
    images = np.stack(img_list)
    return images
Exemplo n.º 30
0
def load_and_align_data(img, face_size):
    """
    Detect and crop faces in frame based on MTCNN.
    :param img: Image to be detected
    :param face_size: Minimum detected face size
    :return: Whether the frame contains faces, face boxes, cropped face image
    """
    img_size = np.asarray(img.shape)[0:2]

    bounding_boxes, _ = align.detect_face.detect_face(
        img=img,
        minsize=20 * face_size,
        pnet=pnet,
        rnet=rnet,
        onet=onet,
        threshold=[0.6, 0.7, 0.7],
        factor=0.709)

    if len(bounding_boxes) < 1:
        return False, np.zeros((0, 0)), []

    det = bounding_boxes
    det[:, 0] = np.maximum(det[:, 0] - MARGIN / 2, 0)
    det[:, 1] = np.maximum(det[:, 1] - MARGIN / 2, 0)
    det[:, 2] = np.minimum(det[:, 2] + MARGIN / 2, img_size[1] - 1)
    det[:, 3] = np.minimum(det[:, 3] + MARGIN / 2, img_size[0] - 1)
    det = det.astype(int)

    # Crop standard-size face images and pre-whiten them
    crop = []
    for i in range(len(bounding_boxes)):
        temp_crop = img[det[i, 1]:det[i, 3], det[i, 0]:det[i, 2], :]
        aligned = cv2.resize(temp_crop, (INPUT_SIZE, INPUT_SIZE))
        prewhitened = facenet.prewhiten(aligned)
        crop.append(prewhitened)
    crop_image = np.stack(crop)

    return True, det, crop_image
Exemplo n.º 31
0
def load_and_align_data(image, image_size, margin, gpu_memory_fraction):

    # 读取图片
    img = image
    # 获取图片的shape
    img_size = np.asarray(img.shape)[0:2]
    # 返回边界框数组 (参数分别是输入图片 脸部最小尺寸 三个网络 阈值 factor不清楚)
    bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet,
                                                      onet, threshold, factor)
    # bounding_boxes 返回的是人脸的坐标
    # 如果检测出图片中不存在人脸 则直接返回,return 0(表示不存在人脸,跳过此图)
    if len(bounding_boxes) < 1:
        return 0, 0, 0
    else:
        crop = []
        det = bounding_boxes

        det[:, 0] = np.maximum(det[:, 0], 0)
        det[:, 1] = np.maximum(det[:, 1], 0)
        det[:, 2] = np.minimum(det[:, 2], img_size[1])
        det[:, 3] = np.minimum(det[:, 3], img_size[0])

        # det[:,0]=np.maximum(det[:,0]-margin/2, 0)
        # det[:,1]=np.maximum(det[:,1]-margin/2, 0)
        # det[:,2]=np.minimum(det[:,2]+margin/2, img_size[1])
        # det[:,3]=np.minimum(det[:,3]+margin/2, img_size[0])

        det = det.astype(int)

        for i in range(len(bounding_boxes)):
            temp_crop = img[det[i, 1]:det[i, 3], det[i, 0]:det[i, 2], :]
            aligned = misc.imresize(temp_crop, (image_size, image_size),
                                    interp='bilinear')
            prewhitened = facenet.prewhiten(aligned)
            crop.append(prewhitened)
        crop_image = np.stack(crop)

        return det, crop_image, 1
Exemplo n.º 32
0
def get_emb_txt(csv_paths, model_path):
    with tf.Graph().as_default():
        with tf.Session() as sess:
            facenet.load_model(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')
            for path in csv_paths:
                #获取到anchor下面的csv文件下对应的dict
                labeled_dict = utils.csv_solver(path)
                emb = []
                #这里所获得的是一个list,按照key从小到大排列,[(id,[]),(id,[]),....]
                labeled_dict = sorted(labeled_dict.items(),
                                      key=lambda d: d[0],
                                      reverse=False)
                #读取图片,然后产生图片的向量
                for _class in labeled_dict:
                    images = []
                    for item in _class[-1]:
                        image = misc.imread(item)
                        aligned = misc.imresize(image, [160, 160],
                                                interp='bilinear')
                        prewhitened = facenet.prewhiten(aligned)
                        images.append(prewhitened)
                    images = np.stack(images).reshape([-1, 160, 160, 3])
                    feed_dict = {
                        images_placeholder: images,
                        phase_train_placeholder: False
                    }
                    emb_res = sess.run(embeddings, feed_dict=feed_dict)
                    emb.append(emb_res)
                print('------> got embeddings of ', path.split('.')[0])
                output_path = output_path_pre + path.split('/')[-1].split(
                    '.')[0] + '.txt'
                emb_txt(output_path, emb)
Exemplo n.º 33
0
def load_and_align_image_json(image_json, image_size, margin):
    minsize = 20 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    data = json.loads(image_json)
    image_list = data['image']
    image = np.array(image_list, dtype='uint8')
    img_size = np.asarray(image.shape)[0:2]
    bounding_boxes, _ = align.detect_face.detect_face(image, minsize, pnet, rnet, onet, threshold, factor)
    if len(bounding_boxes) < 1:
        print("can't detect face, skipping input")
        return None
    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
Exemplo n.º 34
0
def aln_image(m_detector, paths, margin, image_size):
	results = []
	for path in paths:
		# print(path)
		img = misc.imread(os.path.expanduser(path), mode='RGB')
		# print(m_detector.detect_faces(img))
		img_size = np.asarray(img.shape)[0:2]
		faced = m_detector.detect_faces(img)[0]
		x, y, width, height = np.array(faced["box"])
		width += x
		height += y

		bb = np.zeros(4, dtype=np.int32)
		bb[0] = np.maximum(x-margin/2, 0)
		bb[1] = np.maximum(y-margin/2, 0)
		bb[2] = np.minimum(width+margin/2, img_size[1])
		bb[3] = np.minimum(height+margin/2, img_size[0])
		cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
		print(cropped.shape)
		aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
		prewhitened = facenet.prewhiten(aligned)
		results.append(prewhitened)
	return results
Exemplo n.º 35
0
    def set_inside_templates(self,templates_file_name):
        '''
        build the inside template

        :param templates_file_name:path point to the templates_describe scv file
         the format wouldlike this:
            absolutely_path_to_image_1;label_1
            absolutely_path_to_image_2;label_2
            absolutely_path_to_image_3;label_3
            ...
        :type basestring
        :return: True if it success loaded the templates, or False
        '''
        templates_file = open(templates_file_name)
        templates_name = templates_file.readlines()
        self.clear_templates()
        self.__templates_number = len(templates_name)
        self.__templates_label = [None]*self.__templates_number
        img_list = [None] * self.__templates_number
        for i in xrange(self.__templates_number):
            # img = misc.imread(image_paths[i])
            line = templates_name[i].strip('\n')
            name,label = line.split(';')
            self.__templates_label[i]=int(label)
            img = cv2.imread(name)
            print name,"is reading ..."
            aligned,_ = self.__align.align(self.__image_size, img, landmarkIndices=self.__landmarkIndices, skipMulti=False)
            if aligned == None:
                return False
            prewhitened = facenet.prewhiten(aligned)
            img_list[i] = prewhitened
        # self.__templates = img_list
        img_array = np.asarray(img_list)
        feed_dict = {self.__images_placeholder: img_array, self.__phase_train_placeholder: False}
        emb = self.__sess.run(self.__embeddings, feed_dict=feed_dict)
        self.__templates_emb = emb
        return True
                        # cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                        # cropped[0] = facenet.flip(cropped[0], False)
                        # scaled.append(misc.imresize(cropped[0], (image_size, image_size), interp='bilinear'))
                        # scaled[0] = cv2.resize(scaled[0], (input_image_size,input_image_size),
                        #                        interpolation=cv2.INTER_CUBIC)
                        # scaled[0] = facenet.prewhiten(scaled[0])
                        # scaled_reshape.append(scaled[0].reshape(-1,input_image_size,input_image_size,3))
                        # feed_dict = {images_placeholder: scaled_reshape[0], phase_train_placeholder: False}

                        cropped = (frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                        print("{0} {1} {2} {3}".format(bb[i][0], bb[i][1], bb[i][2], bb[i][3]))
                        cropped = facenet.flip(cropped, False)
                        scaled = (misc.imresize(cropped, (image_size, image_size), interp='bilinear'))
                        scaled = cv2.resize(scaled, (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)
                        best_class_indices = np.argmax(predictions, axis=1)
                        best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                        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

                        # for H_i in HumanNames:
                        #     if HumanNames[best_class_indices[0]] == H_i:
                        result_names = class_names[best_class_indices[0]]
Exemplo n.º 37
0
                        bb[i][0] = det[i][0]
                        bb[i][1] = det[i][1]
                        bb[i][2] = det[i][2]
                        bb[i][3] = det[i][3]

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

                        cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                        cropped[0] = facenet.flip(cropped[0], False)
                        scaled.append(misc.imresize(cropped[0], (image_size, image_size), interp='bilinear'))
                        scaled[0] = cv2.resize(scaled[0], (input_image_size,input_image_size),
                                               interpolation=cv2.INTER_CUBIC)
                        scaled[0] = facenet.prewhiten(scaled[0])
                        scaled_reshape.append(scaled[0].reshape(-1,input_image_size,input_image_size,3))
                        feed_dict = {images_placeholder: scaled_reshape[0], phase_train_placeholder: False}
                        emb_array[0, :] = 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]
                        cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2)    #boxing face

                        #plot result idx under box
                        text_x = bb[i][0]
                        text_y = bb[i][3] + 20
                        # print('result: ', best_class_indices[0])
                        for H_i in HumanNames:
                            if HumanNames[best_class_indices[0]] == H_i:
                                result_names = HumanNames[best_class_indices[0]]
def _main():

    args = get_args()
    

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        with sess.as_default():
            # pnet, rnet, onet = detect_face.create_mtcnn(sess, './models/')

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

            print('Loading feature extraction model')
            modeldir = './models/facenet/20190310-055158'
            facenet.load_model(modeldir)

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

            classifier_filename = './myclassifier/my_classifier.pkl'
            classifier_filename_exp = os.path.expanduser(classifier_filename)
            with open(classifier_filename_exp, 'rb') as infile:
                (model, class_names) = pickle.load(infile)
                print('load classifier file-> %s' % classifier_filename_exp)

            video_capture = cv2.VideoCapture(0)
            c = 0

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

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

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

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

                    if frame.ndim == 2:
                        frame = facenet.to_rgb(frame)
                    frame = frame[:, :, 0:3]
                    #print(frame.shape[0])
                    #print(frame.shape[1])
                    
                    image = Image.fromarray(frame)
                    img, bounding_boxes = myYolo.detect_image(image)

                    # Remove the bounding boxes with low confidence
                    nrof_faces = len(bounding_boxes)
                    ## Use MTCNN to get the bounding boxes
                    # bounding_boxes, _ = detect_face.detect_face(frame, minsize, pnet, rnet, onet, threshold, factor)
                    # nrof_faces = bounding_boxes.shape[0]
                    #print('Detected_FaceNum: %d' % nrof_faces)

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

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

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

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

                            if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][2] >= len(frame[0]) or bb[i][3] >= len(frame):
                                print('face is inner of range!')
                                continue

                            # cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                            # cropped[0] = facenet.flip(cropped[0], False)
                            # scaled.append(misc.imresize(cropped[0], (image_size, image_size), interp='bilinear'))
                            # scaled[0] = cv2.resize(scaled[0], (input_image_size,input_image_size),
                            #                        interpolation=cv2.INTER_CUBIC)
                            # scaled[0] = facenet.prewhiten(scaled[0])
                            # scaled_reshape.append(scaled[0].reshape(-1,input_image_size,input_image_size,3))
                            # feed_dict = {images_placeholder: scaled_reshape[0], phase_train_placeholder: False}

                            cropped = (frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :])
                            print("{0} {1} {2} {3}".format(bb[i][0], bb[i][1], bb[i][2], bb[i][3]))
                            cropped = facenet.flip(cropped, False)
                            scaled = (misc.imresize(cropped, (image_size, image_size), interp='bilinear'))
                            scaled = cv2.resize(scaled, (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)
                            best_class_indices = np.argmax(predictions, axis=1)
                            best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                            print(best_class_probabilities)
                            cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2)
                            text_x = bb[i][0]
                            text_y = bb[i][3] + 20

                            # for H_i in HumanNames:
                            #     if HumanNames[best_class_indices[0]] == H_i:
                            result_names = class_names[best_class_indices[0]] if best_class_probabilities[0] > 0.45 else "Unknown"
                            #print(result_names)
                            cv2.putText(frame, result_names, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (0, 0, 255), thickness=1, lineType=2)
                    else:
                        print('Unable to align')

                sec = curTime - prevTime
                prevTime = curTime
                fps = 1 / (sec)
                str = 'FPS: %2.3f' % fps
                text_fps_x = len(frame[0]) - 150
                text_fps_y = 20
                cv2.putText(frame, str, (text_fps_x, text_fps_y),
                            cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), thickness=1, lineType=2)
                # c+=1
                cv2.imshow('Video', frame)

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

            video_capture.release()
            # #video writer
            # out.release()
            cv2.destroyAllWindows()