Beispiel #1
0
 def detect(self,image_path):
     aligned = []
     try:
         img = misc.imread(image_path)
     except (IOError, ValueError, IndexError) as e:
         errorMessage = '{}: {}'.format(image_path, e)
         logging.info(errorMessage)
     else:
         if img.ndim < 2:
             logging.info('Unable to align "%s"' % image_path)
             return []
         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_all = bounding_boxes[:, 0:4]
             img_size = np.asarray(img.shape)[0:2]
             for boxindex in range(nrof_faces):
                 det = np.squeeze(det_all[boxindex, :])
                 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])
                 left, top, right, bottom = bb[0], bb[1], bb[2], bb[3]
                 aligned.append({'x': left,'y':top,'w':right-left,'h':bottom-top})
         return aligned
Beispiel #2
0
def detect_human_face(img_file):
    global g_facenet
    minsize = 20  # minimum size of face
    threshold = [0.85, 0.85, 0.9]  # three steps's threshold
    factor = 0.709  # scale factor

    img = np.asarray(Image.open(img_file).convert('RGB'))
    p, r, o = g_facenet
    bounding_boxes, _ = detect_face.detect_face(img, minsize, p, r, o,
                                                threshold, factor)

    return len(bounding_boxes) > 0
Beispiel #3
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 = detect_face.create_mtcnn(sess, None)

    tmp_image_paths = copy.copy(image_paths)
    img_list = []
    face_area = []
    file_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, _ = 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
        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')
            prewhitened = facenet.prewhiten(aligned)
            img_list.append(prewhitened)
            face_area.append(
                Image.open(image).crop((bb[0], bb[1], bb[2], bb[3])))
            file_list.append(image)
    images = np.stack(img_list)
    return images, face_area, file_list
Beispiel #4
0
def _detectSingleFace(path, minsize=50, threshold = [0.6, 0.7, 0.7], factor=0.709,pad=5,image_size=160,preprocsingFunc=None):

    I=imread(path,mode='RGB')
    if preprocsingFunc:
        I=preprocsingFunc(I)
    box, point=detect_face(I,minsize,pnet_fun,rnet_fun,onet_fun,threshold,factor)

    if len(box)==0:
        return None

    h,w=I.shape[0:2]
    x1, y1, x2, y2, acc = box[0]
    x1, y1, x2, y2 = max(x1-pad, 0), max(y1-pad, 0), min(x2+pad, w), min(y2+pad, h)
    x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)

    cropped=I[y1:y2,x1:x2]
    aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
    prewhitened=facenet.prewhiten(aligned)

    return prewhitened
Beispiel #5
0
def main(data_dir, min_size):
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, npy)

            minsize = min_size  # minimum size of face
            threshold = [0.6, 0.7, 0.7]  # three steps's threshold
            factor = 0.709  # scale factor
        dataset = face_net.get_dataset(data_dir)

        number_sample = 0
        number_face_detected = 0
        for cls in dataset:
            for image_path in cls.image_paths:
                number_sample = number_sample + 1
                img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
                if img.ndim == 2:
                    img = face_net.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):
                    print('{0} face detected : {1}'.format(
                        nrof_faces, image_path))
                elif (nrof_faces == 2):
                    print('{0} face detected : {1}'.format(
                        nrof_faces, image_path))
                    number_face_detected = number_face_detected + 1
                else:
                    number_face_detected = number_face_detected + 1

    print("Finish!!!!")
    print('Number face detected {0}'.format(number_face_detected))
Beispiel #6
0
            if ret == False:
                break
            # frame = cv2.resize(frame, (650, 650), fx=0.5, fy=0.5)  # resize frame (optional)

            number_frame += 1

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

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

                if frame.ndim == 2:
                    frame = face_net.to_rgb(frame)
                frame = frame[:, :, 0:3]
                bounding_boxes, _ = detect_face.detect_face(
                    frame, minsize, pnet, rnet, onet, threshold, factor)
                nrof_faces = bounding_boxes.shape[0]
                # if nrof_faces > 0:
                # print('Detected_FaceNum: %d' % nrof_faces)
                output_filename = os.path.expanduser(
                    output_dir + '/{0}_{1}_{2}.jpg'.format(
                        nrof_faces, number_frame,
                        datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')))
                cv2.imwrite(output_filename, frame)

                if nrof_faces > 0:
                    det = bounding_boxes[:, 0:4]
                    det_arr = []
                    img_size = np.asarray(frame.shape)[0:2]
                    if nrof_faces > 1:
                        if constants.ALIGN_DETECT_MULTIPLE_FACES:
def main(data_dir, output_dir):
    sleep(random.random())
    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('argument default'))
    dataset = face_net.get_dataset(data_dir)

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=constants.GPU_MEMORY_FRACTION_DEFAULT)
        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 = constants.FACE_REG_MINSIZE  # minimum size of face
    threshold = constants.ALIGN_THRESHOLD  # three steps's threshold
    factor = constants.ALIGN_FACTOR  # scale factor

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)

    face_detected_list = []

    nrof_images_total = 0
    nrof_successfully_aligned = 0
    if constants.ALIGN_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 constants.ALIGN_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')
            if not os.path.exists(output_filename):
                try:
                    img = misc.imread(image_path)
                except (IOError, ValueError, IndexError) as e:
                    error_message = '{}: {}'.format(image_path, e)
                    print(error_message)
                else:
                    if img.ndim < 2:
                        print('Unable to align "%s"' % image_path)
                        face_detected_list.append('Unable to align {0}'.format(image_path))
                        # text_file.write('%s\n' % (output_filename))
                        continue
                    if img.ndim == 2:
                        img = face_net.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 constants.ALIGN_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] - constants.COMPARE_MARGIN_DEFAULT / 2, 0)
                            bb[1] = np.maximum(det[1] - constants.COMPARE_MARGIN_DEFAULT / 2, 0)
                            bb[2] = np.minimum(det[2] + constants.COMPARE_MARGIN_DEFAULT / 2, img_size[1])
                            bb[3] = np.minimum(det[3] + constants.COMPARE_MARGIN_DEFAULT / 2, img_size[0])
                            cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                            scaled = misc.imresize(cropped, (constants.ALIGN_IMAGE_SIZE, constants.ALIGN_IMAGE_SIZE), interp='bilinear')
                            nrof_successfully_aligned += 1
                            filename_base, file_extension = os.path.splitext(output_filename)
                            if constants.ALIGN_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)
                            face_detected_list.append('%s --- BOX [%d , %d , %d , %d]\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3]))
                    else:
                        face_detected_list.append('Unable to align {0}'.format(image_path))

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
    return nrof_images_total, nrof_successfully_aligned, face_detected_list
Beispiel #8
0
def main(image_path, data_dir, model_dir, classifier_file):
    npy = ''
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=constants.
                                    GPU_MEMORY_FRACTION_DEFAULT)
        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 = constants.FACE_REG_MINSIZE  # minimum size of face
            threshold = constants.ALIGN_THRESHOLD  # three steps's threshold
            factor = constants.ALIGN_FACTOR  # scale factor
            frame_interval = 3
            image_size = 160
            input_image_size = 160

            human_names = os.listdir(data_dir)
            human_names.sort()

            print('Loading feature extraction model')
            face_net.load_model(model_dir)

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

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

            c = 0
            print('Start Recognition!')
            frame = cv2.imread(image_path, 0)
            time_f = frame_interval

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

                if nrof_faces > 0:
                    det = bounding_boxes[:, 0:4]
                    cropped = []
                    scaled = []
                    scaled_reshape = []
                    bb = np.zeros((nrof_faces, 4), dtype=np.int32)

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

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

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

                        cropped.append(frame[bb[i][1]:bb[i][3],
                                             bb[i][0]:bb[i][2], :])
                        cropped[i] = face_net.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] = face_net.prewhiten(scaled[i])
                        scaled_reshape.append(scaled[i].reshape(
                            -1, input_image_size, input_image_size, 3))
                        feed_dict = {
                            images_placeholder: scaled_reshape[i],
                            phase_train_placeholder: False
                        }
                        emb_array[0, :] = sess.run(embeddings,
                                                   feed_dict=feed_dict)
                        predictions = model.predict_proba(emb_array)
                        print(predictions)
                        best_class_indices = np.argmax(predictions, axis=1)
                        # print(best_class_indices)
                        best_class_probabilities = predictions[
                            np.arange(len(best_class_indices)),
                            best_class_indices]
                        print(best_class_probabilities)
                        cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                      (bb[i][2], bb[i][3]), (0, 255, 0),
                                      2)  # boxing face

                        # plot result idx under box
                        text_x = bb[i][0]
                        text_y = bb[i][3] - 10
                        print(
                            i, 'Result Indices: ', best_class_indices[0],
                            ' : ', 'Face detected of : {0}'.format(
                                human_names[best_class_indices[0]]))
                        print(human_names)
                        for H_i in human_names:
                            # print(H_i)
                            if human_names[best_class_indices[
                                    0]] == H_i and best_class_probabilities >= constants.FACE_REG_POSSIBILITY:
                                result_names = human_names[
                                    best_class_indices[0]]
                                cv2.putText(frame,
                                            str(i) + ': ' + result_names,
                                            (text_x, text_y),
                                            cv2.FONT_HERSHEY_PLAIN,
                                            1, (0, 0, 255),
                                            thickness=1,
                                            lineType=1)
                        print('------------------')
                else:
                    print('Unable to align')
            frame = cv2.resize(frame, (0, 0), fx=0.5,
                               fy=0.5)  # resize frame (optional)
            cv2.imshow('Image', frame)
            cv2.imwrite('output/' + image_path.split('/')[-1], frame)
            if cv2.waitKey(1000000) & 0xFF == ord('q'):
                sys.exit("Thanks")
Beispiel #9
0
def align(image_path):
    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)
        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)
    filename = os.path.splitext(os.path.split(image_path)[1])[0]

    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)
            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]
        print('nrof_faces: %s' % nrof_faces)
        n = 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,:]
                for one in det:
                    one = np.squeeze(one)
                    bb = np.zeros(4, dtype=np.int32)
                    bb[0] = np.maximum(one[0] - 5.0 / 2, 0)
                    bb[1] = np.maximum(one[1] - 5.0 / 2, 0)
                    bb[2] = np.minimum(one[2] + 5.0 / 2, img_size[1])
                    bb[3] = np.minimum(one[3] + 5.0 / 2, img_size[0])
                    cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                    scaled = misc.imresize(cropped, (128, 128),
                                           interp='bilinear')
                    misc.imsave('/dl/' + str(n) + '.png', scaled)
                    n += 1
        else:
            print('Unable to align "%s"' % image_path)
Beispiel #10
0
def face_verification(img_pairs_list):
    model = 'C:\\Users\\User\\.conda\\envs\\facenet_test\\Lib\\site-packages\\facenet\\align'
    model_facenet = r'C:\\Users\\User\\Desktop\\facenett\\20180402-114759'  # 模型在你电脑中的路径
    # mtcnn相关参数
    minsize = 40
    threshold = [0.4, 0.5, 0.6]  # pnet、rnet、onet三个网络输出人脸的阈值,大于阈值则保留,小于阈值则丢弃
    factor = 0.709  # scale factor

    # 创建mtcnn网络
    with tf.Graph().as_default():
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, model)

    margin = 44
    image_size = 160

    with tf.Graph().as_default():

        with tf.Session() as sess:

            # 根据模型文件载入模型
            facenet.load_model(model_facenet)
            # 得到输入、输出等张量
            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")

            # 设置可视化进度条相关参数
            jd = '\r   %2d%%\t [%s%s]'
            bar_num_total = 50
            total_num = len(img_pairs_list)
            result, dist = [], []

            for i in range(len(img_pairs_list)):

                # 画进度条
                if i % round(
                        total_num / bar_num_total) == 0 or i == total_num - 1:
                    bar_num_alright = round(bar_num_total * i / total_num)
                    alright = '#' * bar_num_alright
                    not_alright = '□' * (bar_num_total - bar_num_alright)
                    percent = (bar_num_alright / bar_num_total) * 100
                    print(jd % (percent, alright, not_alright), end='')

                # 读取一对人脸图像
                img_pairs = img_pairs_list[i]
                img_list = []
                img1 = cv2.imread(img_pairs[0])
                img2 = cv2.imread(img_pairs[1])

                img_size1 = np.asarray(img1.shape)[0:2]
                img_size2 = np.asarray(img2.shape)[0:2]

                # 检测该对图像中的人脸
                bounding_box1, _1 = detect_face.detect_face(
                    img1, minsize, pnet, rnet, onet, threshold, factor)
                bounding_box2, _2 = detect_face.detect_face(
                    img2, minsize, pnet, rnet, onet, threshold, factor)

                # 未检测到人脸,则将结果标为-1,后续计算准确率时排除
                if len(bounding_box1) < 1 or len(bounding_box2) < 1:
                    result.append(-1)
                    dist.append(-1)
                    continue

                # 将图片1加入img_list
                det = np.squeeze(bounding_box1[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_size1[1])
                bb[3] = np.minimum(det[3] + margin / 2, img_size1[0])
                cropped = img1[bb[1]:bb[3], bb[0]:bb[2], :]
                aligned = cv2.resize(cropped, (image_size, image_size))
                prewhitened = facenet.prewhiten(aligned)
                img_list.append(prewhitened)

                # 将图片2加入img_list
                det = np.squeeze(bounding_box2[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_size2[1])
                bb[3] = np.minimum(det[3] + margin / 2, img_size2[0])
                cropped = img2[bb[1]:bb[3], bb[0]:bb[2], :]
                aligned = cv2.resize(cropped, (image_size, image_size))
                prewhitened = facenet.prewhiten(aligned)
                img_list.append(prewhitened)

                images = np.stack(img_list)

                # 将两个人脸转化为512维的向量
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb = sess.run(embeddings, feed_dict=feed_dict)

                # 计算两个人脸向量的距离
                ed = np.sqrt(np.sum(np.square(np.subtract(emb[0], emb[1]))))
                dist.append(ed)
                # 根据得出的人脸间的距离,判断是否属于同一个人
                if ed <= 1.1:
                    result.append(1)
                else:
                    result.append(0)
    return result, dist
Beispiel #11
0
# 创建 tensorflow 网络并加载参数
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)

path = "C:\\Users\\xuefe\\Desktop\\facef\\a\\image\\"
img_dirs = os.listdir(path)
image_path = "C:\\Users\\xuefe\\Desktop\\facex\\b\\"

for im in img_dirs:
    img = path + '\\' + im
    img = misc.imread(img)
    bounding_boxes, face_points = detect_face.detect_face(
        img, minsize, pnet, rnet, onet, threshold, factor)
    if bounding_boxes.shape[0] != 1:
        continue
    # 通过人脸框置信度以及鼻子和眼睛的x坐标进行筛选
    if (bounding_boxes[0][4] <= 0.999) or (  # bounding_box[0][4]为人脸框的置信度
        (face_points[2][0] <= face_points[0][0]
         and face_points[2][0] <= face_points[1][0]) or (  # face_points表示
             face_points[2][0] >= face_points[0][0]
             and face_points[2][0] >= face_points[1][0])):  # 人脸的五个关键点
        continue  # 如果置信度过低或者人脸很歪则丢掉图片
    else:
        print(im, ':', bounding_boxes[0][4])
        misc.imsave(image_path + im, img)
def RecognizeFace(frames, model=None, class_names=None):

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

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

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

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

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

            # video_capture = cv2.VideoCapture("akshay_mov.mp4")
            c = 0

            HumanNames = class_names
            print(HumanNames)

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

            #frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)    #resize frame (optional)
            total_faces_detected = {}
            for frame in frames:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                curTime = time.time() + 1  # calc fps
                timeF = frame_interval

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

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

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

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

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

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

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

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

                                predictions = model.predict_proba(emb_array)
                                print(predictions)
                                best_class_indices = np.argmax(predictions,
                                                               axis=1)
                                # print(best_class_indices)
                                best_class_probabilities = predictions[
                                    np.arange(len(best_class_indices)),
                                    best_class_indices]

                                #plot result idx under box
                                text_x = bb[i][0]
                                text_y = bb[i][3] + 20
                                print('Result Indices: ',
                                      best_class_indices[0])
                                print(HumanNames)
                                for H_i in HumanNames:
                                    # print(H_i)
                                    if HumanNames[best_class_indices[
                                            0]] == H_i and best_class_probabilities >= 0.4:
                                        result_names = HumanNames[
                                            best_class_indices[0]]
                                        if result_names in total_faces_detected:
                                            if predictions[0][best_class_indices[
                                                    0]] > total_faces_detected[
                                                        result_names]:
                                                total_faces_detected[
                                                    result_names] = predictions[
                                                        0][best_class_indices[
                                                            0]]
                                        else:
                                            total_faces_detected[
                                                result_names] = predictions[0][
                                                    best_class_indices[0]]

                    else:
                        print("BHAKKK")
            if len(total_faces_detected) == 0:
                return None
            else:
                x = sorted(total_faces_detected.items(),
                           key=operator.itemgetter(1))
                return [x[len(x) - 1][0]]
Beispiel #13
0
def image_array_align_data(image_arr,
                           image_path,
                           pnet,
                           rnet,
                           onet,
                           image_size=160,
                           margin=32,
                           detect_multiple_faces=True):
    """
    截取人脸的类
    :param image_arr: 人脸像素点数组
    :param image_path: 拍摄人脸存储路径
    :param pnet: caffe模型
    :param rnet: caffe模型
    :param onet: caffe模型
    :param image_size: 图像大小
    :param margin: 边缘截取
    :param gpu_memory_fraction: 允许的gpu内存大小
    :param detect_multiple_faces: 是否可以识别多张脸,默认为False
    :return: 若成功,返回截取的人脸数组集合如果没有检测到人脸,直接返回一个1*3的0矩阵
    """
    minsize = MINSIZE  # minimum size of face
    threshold = THRESHOLD  # three steps's threshold
    factor = FACTOR  # scale factor

    img = image_arr
    bounding_boxes, _ = 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((len(det_arr), 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 = misc.imresize(cropped, (image_size, image_size),
                                   interp='bilinear')
            nrof_successfully_aligned += 1

            # 保存检测的头像
            filename_base = BASE_DIR + os.sep + 'media' + os.sep + 'face_160' + os.sep + datetime.datetime.now(
            ).strftime('%Y-%m-%d')

            if not os.path.exists(filename_base):
                os.mkdir(filename_base)

            filename = os.path.basename(image_path)
            filename_name, file_extension = os.path.splitext(filename)
            # 多个人脸时,在picname后加_0 _1 _2 依次累加。
            output_filename_n = "{}/{}_{}{}".format(filename_base,
                                                    filename_name, i,
                                                    file_extension)
            misc.imsave(output_filename_n, scaled)

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

            images[i] = scaled
    if nrof_faces > 0:
        return images
    else:
        return np.zeros((1, 3))
Beispiel #14
0
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, _ = 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] - 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
                            misc.imsave(output_filename, scaled)
                            text_file.write(
                                '%s %d %d %d %d\n' %
                                (output_filename, 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)
def main(test_dir, data_dir, model_dir, classifier_file):
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=constants.
                                    GPU_MEMORY_FRACTION_DEFAULT)
        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 = constants.FACE_REG_MINSIZE  # minimum size of face
            threshold = constants.ALIGN_THRESHOLD  # three steps's threshold
            factor = constants.ALIGN_FACTOR  # scale factor
            image_size = 160
            input_image_size = 160

            human_names = os.listdir(data_dir)
            human_names.sort()

            print('Loading feature extraction model')
            face_net.load_model(model_dir)

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

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

            c = 0
            print('Start Recognition!')
            dataset = face_net.get_dataset(test_dir)
            number_of_face_recognition = 0
            for cls in dataset:
                for image_path in cls.image_paths:
                    frame = cv2.imread(image_path, 0)
                    if frame.ndim == 2:
                        frame = face_net.to_rgb(frame)
                    frame = frame[:, :, 0:3]
                    bounding_boxes, _ = detect_face.detect_face(
                        frame, minsize, pnet, rnet, onet, threshold, factor)
                    nrof_faces = bounding_boxes.shape[0]
                    if nrof_faces > 0:
                        det = bounding_boxes[:, 0:4]
                        cropped = []
                        scaled = []
                        scaled_reshape = []
                        bb = np.zeros((nrof_faces, 4), dtype=np.int32)

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

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

                            # inner exception
                            if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][
                                    2] >= len(
                                        frame[0]) or bb[i][3] >= len(frame):
                                print(
                                    'Face is too close {0}'.format(image_path))
                                break

                            cropped.append(frame[bb[i][1]:bb[i][3],
                                                 bb[i][0]:bb[i][2], :])
                            cropped[i] = face_net.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] = face_net.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)
                            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]

                            # plot result idx under box
                            for H_i in human_names:
                                # print(H_i)
                                if human_names[best_class_indices[0]] == H_i \
                                        and H_i in image_path:
                                    print('{0} : {1}'.format(
                                        best_class_probabilities, image_path))
                                    number_of_face_recognition = number_of_face_recognition + 1
                    else:
                        print('Unable to recognition {0}'.format(image_path))

    print("Finish!!!!")
    print('Number face detected {0}'.format(number_of_face_recognition))