对图像进行人脸检测和对齐

该段代码获取的信息是来自2018-10-03的重庆大学计算机学院主页(http://www.cs.cqu.edu.cn/ )上的公开数据和信息,同时需要指出的是,所有的数据使用均以学习交流为目的,而非营利。

该段代码属于teach_computer_to_identify_teachers项目

环境配置

由于人脸检测和对齐在Colab上完成,所以需要先对环境进行配置。在以下步骤中,我将使用Google Drive SDK 来挂载我的Google Drive到分配的虚拟机上,并进行基础的配置。

In [2]:
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
E: Package 'python-software-properties' has no installation candidate
Selecting previously unselected package libfuse2:amd64.
(Reading database ... 26397 files and directories currently installed.)
Preparing to unpack .../libfuse2_2.9.7-1ubuntu1_amd64.deb ...
Unpacking libfuse2:amd64 (2.9.7-1ubuntu1) ...
Selecting previously unselected package fuse.
Preparing to unpack .../fuse_2.9.7-1ubuntu1_amd64.deb ...
Unpacking fuse (2.9.7-1ubuntu1) ...
Selecting previously unselected package google-drive-ocamlfuse.
Preparing to unpack .../google-drive-ocamlfuse_0.7.1-0ubuntu2~ubuntu18.04.1_amd64.deb ...
Unpacking google-drive-ocamlfuse (0.7.1-0ubuntu2~ubuntu18.04.1) ...
Setting up libfuse2:amd64 (2.9.7-1ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up fuse (2.9.7-1ubuntu1) ...
Setting up google-drive-ocamlfuse (0.7.1-0ubuntu2~ubuntu18.04.1) ...
Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force
··········
Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force
Please enter the verification code: Access token retrieved correctly.
In [0]:
!mkdir -p drive
!google-drive-ocamlfuse drive  -o nonempty

获取代码

代码来自https://github.com/davidsandberg/facenet(MIT License)这是牛津大学团队实现的在Tensorflow上的Facenet人脸识别算法。

In [32]:
!git clone https://github.com/davidsandberg/facenet.git
Cloning into 'facenet'...
remote: Enumerating objects: 3146, done.
remote: Total 3146 (delta 0), reused 0 (delta 0), pack-reused 3146
Receiving objects: 100% (3146/3146), 2.93 MiB | 32.60 MiB/s, done.
Resolving deltas: 100% (2229/2229), done.
In [34]:
!ls facenet
contributed  __init__.py  README.md	    src   tmp
data	     LICENSE.md   requirements.txt  test  util

获取数据集

数据集在上节的爬虫中已获得,已上载到了Google Drive,CP过来就好了

In [0]:
!cp -f -r drive/DS_final/picture pic_ori
In [37]:
! ls pic_ori
万里.jpg    刘骥.jpg	宋伟.jpg    朱庆生.jpg	王波.jpg    郑洪英.jpg
伍星.jpg    古平.jpg	尚家兴.jpg  朱征宇.jpg	王茜.jpg    郭平.jpg
何中市.jpg  叶春晓.jpg	尚赵伟.jpg  朱晓红.jpg	石亮.jpg    郭松涛.jpg
何静媛.jpg  叶莲.jpg	尹云飞.jpg  李佳.jpg	石锐.jpg    钟世刚.jpg
冯亮.jpg    向涛.jpg	廖晓峰.jpg  李双庆.jpg	罗军.jpg    钟将.jpg
冯永.jpg    吴全旺.jpg	张太平.jpg  李季.jpg	肖春华.jpg  陈四清.jpg
刘凯.jpg    吴开贵.jpg	张敏.jpg    李学明.jpg	肖迪.jpg    陈恒鑫.jpg
刘卫宁.jpg  吴长泽.jpg	张玉芳.jpg  杨吉云.jpg	葛亮.jpg    陈波.jpg
刘大江.jpg  周尚波.jpg	张程.jpg    杨广超.jpg	谭玉娟.jpg  陈自郁.jpg
刘慧君.jpg  周庆.jpg	房斌.jpg    杨瑞龙.jpg	邓绍江.jpg  陈超.jpg
刘斌.jpg    周明强.jpg	文静.jpg    汪成亮.jpg	邢永康.jpg  黄仁.jpg
刘然.jpg    夏云霓.jpg	曽一.jpg    涂凤华.jpg	邹东升.jpg  黄宏宇.jpg
刘韦辰.jpg  孙天昊.jpg	曾令秋.jpg  熊忠阳.jpg	郑林江.jpg

检查运行条件

在Github上看了下目录结构,我们找到了align/align_dataset_matcnn.py来进行人脸检测和对齐。看了代码发现它是MTCNN模型的入口代码,需要import detect_face.py和facenet。而detect_face.py所需的P-Net、R-Net、O-Net预训练模型在align文件夹下给出了。所以我们只需把facenet_master放进sys.path里就好了。

In [0]:
import sys
sys.path.append('facenet/src')
In [45]:
!python facenet/src/align/align_dataset_mtcnn.py ./pic_ori ./pic_aligned --image_size 160 --margin 32 --random_order
Traceback (most recent call last):
  File "facenet/src/align/align_dataset_mtcnn.py", line 34, in <module>
    import facenet
ModuleNotFoundError: No module named 'facenet'

意识到把facenet_master放进sys.path需要在代码片段内操作,所以干脆从Github上把align_dataset_mtcnn.py源码复制进来改,改下sys.path,再把args给改成传入列表, 然后facenet.get_dataset不知道为啥又找不到了,手动复制进来

In [61]:
"""Performs face alignment and stores face thumbnails in the output directory."""
# MIT License
# 
# Copyright (c) 2016 David Sandberg
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append('facenet/src')
from scipy import misc
import sys
import os
import argparse
import tensorflow as tf
import numpy as np
import facenet
import align.detect_face
import random
from time import sleep

def get_dataset(path, has_class_directories=True):
    dataset = []
    path_exp = os.path.expanduser(path)
    classes = [path for path in os.listdir(path_exp) \
                    if os.path.isdir(os.path.join(path_exp, path))]
    classes.sort()
    nrof_classes = len(classes)
    for i in range(nrof_classes):
        class_name = classes[i]
        facedir = os.path.join(path_exp, class_name)
        image_paths = get_image_paths(facedir)
        dataset.append(ImageClass(class_name, image_paths))
  
    return dataset
  
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)
    dataset = 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 = align.detect_face.create_mtcnn(sess, None)
    
    minsize = 20 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    
    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir, filename+'.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim<2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:,:,0:3]
    
                        bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces>0:
                            det = bounding_boxes[:,0:4]
                            det_arr = []
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces>1:
                                if args.detect_multiple_faces:
                                    for i in range(nrof_faces):
                                        det_arr.append(np.squeeze(det[i]))
                                else:
                                    bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                                    offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                                    index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                                    det_arr.append(det[index,:])
                            else:
                                det_arr.append(np.squeeze(det))

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

def parse_arguments(argv):
    parser = argparse.ArgumentParser()
    
    parser.add_argument('input_dir', type=str, help='Directory with unaligned images.')
    parser.add_argument('output_dir', type=str, help='Directory with aligned face thumbnails.')
    parser.add_argument('--image_size', type=int,
        help='Image size (height, width) in pixels.', default=182)
    parser.add_argument('--margin', type=int,
        help='Margin for the crop around the bounding box (height, width) in pixels.', default=44)
    parser.add_argument('--random_order', 
        help='Shuffles the order of images to enable alignment using multiple processes.', action='store_true')
    parser.add_argument('--gpu_memory_fraction', type=float,
        help='Upper bound on the amount of GPU memory that will be used by the process.', default=1.0)
    parser.add_argument('--detect_multiple_faces', type=bool,
                        help='Detect and align multiple faces per image.', default=False)
    return parser.parse_args(argv)

if __name__ == '__main__':
    main(parse_arguments(['./pic_ori', './pic_aligned', '--image_size', '160','--margin','32','--random_order']))
Creating networks and loading parameters
Total number of images: 0
Number of successfully aligned images: 0

这个地方卡了很久,读了几遍代码才发现是输入目录结构不对,于是仿照lfw又来整理目录。

In [64]:
import shutil
pic_list = os.listdir("pic_ori")
for ipic in pic_list:
  iname =ipic.replace(".jpg","")
  os.makedirs("pic_ori/{}".format(iname))
  shutil.copyfile("pic_ori/{}.jpg".format(iname),"pic_ori/{}/{}.jpg".format(iname,iname))
  os.remove("pic_ori/{}.jpg".format(iname))
os.listdir("pic_ori")
      
Out[64]:
['肖春华',
 '孙天昊',
 '吴长泽',
 '朱晓红',
 '冯永',
 '刘骥',
 '李学明',
 '李佳',
 '古平',
 '周庆',
 '尚赵伟',
 '陈四清',
 '万里',
 '杨吉云',
 '肖迪',
 '陈波',
 '伍星',
 '邹东升',
 '尚家兴',
 '朱征宇',
 '冯亮',
 '邢永康',
 '张敏',
 '熊忠阳',
 '夏云霓',
 '何中市',
 '钟世刚',
 '刘然',
 '罗军',
 '吴开贵',
 '刘卫宁',
 '宋伟',
 '文静',
 '周明强',
 '周尚波',
 '向涛',
 '张玉芳',
 '汪成亮',
 '尹云飞',
 '刘大江',
 '钟将',
 '叶莲',
 '廖晓峰',
 '杨广超',
 '陈自郁',
 '郑洪英',
 '陈恒鑫',
 '邓绍江',
 '杨瑞龙',
 '何静媛',
 '刘斌',
 '石亮',
 '朱庆生',
 '黄宏宇',
 '吴全旺',
 '陈超',
 '曾令秋',
 '涂凤华',
 '张程',
 '张太平',
 '郭松涛',
 '郭平',
 '郑林江',
 '谭玉娟',
 '刘韦辰',
 '刘慧君',
 '刘凯',
 '李季',
 '房斌',
 '王波',
 '李双庆',
 '黄仁',
 '王茜',
 '曽一',
 '石锐',
 '叶春晓',
 '葛亮']

为了防止表意不清晰,碎片化,于是又复制了一遍

In [67]:
"""Performs face alignment and stores face thumbnails in the output directory."""
# MIT License
# 
# Copyright (c) 2016 David Sandberg
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append('facenet/src')
from scipy import misc
import sys
import os
import argparse
import tensorflow as tf
import numpy as np
import facenet
import align.detect_face
import random
from time import sleep

class ImageClass():
    "Stores the paths to images for a given class"
    def __init__(self, name, image_paths):
        self.name = name
        self.image_paths = image_paths
  
    def __str__(self):
        return self.name + ', ' + str(len(self.image_paths)) + ' images'
  
    def __len__(self):
        return len(self.image_paths)
      
def get_dataset(path, has_class_directories=True):
    dataset = []
    path_exp = os.path.expanduser(path)
    classes = [path for path in os.listdir(path_exp) \
                    if os.path.isdir(os.path.join(path_exp, path))]
    classes.sort()
    nrof_classes = len(classes)
    for i in range(nrof_classes):
        class_name = classes[i]
        facedir = os.path.join(path_exp, class_name)
        image_paths = get_image_paths(facedir)
        dataset.append(ImageClass(class_name, image_paths))
  
    return dataset

def get_image_paths(facedir):
    image_paths = []
    if os.path.isdir(facedir):
        images = os.listdir(facedir)
        image_paths = [os.path.join(facedir,img) for img in images]
    return image_paths
  
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)
    dataset = 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 = align.detect_face.create_mtcnn(sess, None)
    
    minsize = 20 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    
    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir, filename+'.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim<2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:,:,0:3]
    
                        bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces>0:
                            det = bounding_boxes[:,0:4]
                            det_arr = []
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces>1:
                                if args.detect_multiple_faces:
                                    for i in range(nrof_faces):
                                        det_arr.append(np.squeeze(det[i]))
                                else:
                                    bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                                    offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                                    index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                                    det_arr.append(det[index,:])
                            else:
                                det_arr.append(np.squeeze(det))

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

def parse_arguments(argv):
    parser = argparse.ArgumentParser()
    
    parser.add_argument('input_dir', type=str, help='Directory with unaligned images.')
    parser.add_argument('output_dir', type=str, help='Directory with aligned face thumbnails.')
    parser.add_argument('--image_size', type=int,
        help='Image size (height, width) in pixels.', default=182)
    parser.add_argument('--margin', type=int,
        help='Margin for the crop around the bounding box (height, width) in pixels.', default=44)
    parser.add_argument('--random_order', 
        help='Shuffles the order of images to enable alignment using multiple processes.', action='store_true')
    parser.add_argument('--gpu_memory_fraction', type=float,
        help='Upper bound on the amount of GPU memory that will be used by the process.', default=1.0)
    parser.add_argument('--detect_multiple_faces', type=bool,
                        help='Detect and align multiple faces per image.', default=False)
    return parser.parse_args(argv)

if __name__ == '__main__':
    main(parse_arguments(['./pic_ori', './pic_aligned', '--image_size', '160','--margin','32','--random_order']))
Creating networks and loading parameters
./pic_ori/石锐/石锐.jpg
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:114: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:155: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:162: DeprecationWarning: `imsave` is deprecated!
`imsave` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imwrite`` instead.
./pic_ori/杨吉云/杨吉云.jpg
./pic_ori/吴开贵/吴开贵.jpg
./pic_ori/郑林江/郑林江.jpg
./pic_ori/钟世刚/钟世刚.jpg
Unable to align "./pic_ori/钟世刚/钟世刚.jpg"
./pic_ori/曽一/曽一.jpg
./pic_ori/李双庆/李双庆.jpg
./pic_ori/罗军/罗军.jpg
./pic_ori/郭松涛/郭松涛.jpg
./pic_ori/周庆/周庆.jpg
./pic_ori/陈超/陈超.jpg
./pic_ori/朱晓红/朱晓红.jpg
./pic_ori/周明强/周明强.jpg
./pic_ori/尚家兴/尚家兴.jpg
./pic_ori/廖晓峰/廖晓峰.jpg
./pic_ori/葛亮/葛亮.jpg
./pic_ori/陈四清/陈四清.jpg
./pic_ori/何中市/何中市.jpg
./pic_ori/钟将/钟将.jpg
./pic_ori/李佳/李佳.jpg
./pic_ori/吴全旺/吴全旺.jpg
./pic_ori/冯永/冯永.jpg
./pic_ori/孙天昊/孙天昊.jpg
./pic_ori/刘大江/刘大江.jpg
./pic_ori/郭平/郭平.jpg
./pic_ori/朱庆生/朱庆生.jpg
./pic_ori/曾令秋/曾令秋.jpg
./pic_ori/汪成亮/汪成亮.jpg
./pic_ori/朱征宇/朱征宇.jpg
./pic_ori/张太平/张太平.jpg
./pic_ori/刘韦辰/刘韦辰.jpg
./pic_ori/冯亮/冯亮.jpg
./pic_ori/石亮/石亮.jpg
./pic_ori/肖迪/肖迪.jpg
./pic_ori/刘凯/刘凯.jpg
./pic_ori/文静/文静.jpg
./pic_ori/张玉芳/张玉芳.jpg
./pic_ori/张程/张程.jpg
./pic_ori/肖春华/肖春华.jpg
./pic_ori/尹云飞/尹云飞.jpg
./pic_ori/夏云霓/夏云霓.jpg
./pic_ori/邓绍江/邓绍江.jpg
./pic_ori/邹东升/邹东升.jpg
./pic_ori/叶莲/叶莲.jpg
./pic_ori/黄仁/黄仁.jpg
./pic_ori/刘卫宁/刘卫宁.jpg
./pic_ori/周尚波/周尚波.jpg
./pic_ori/杨广超/杨广超.jpg
./pic_ori/尚赵伟/尚赵伟.jpg
./pic_ori/陈波/陈波.jpg
Unable to align "./pic_ori/陈波/陈波.jpg"
./pic_ori/陈恒鑫/陈恒鑫.jpg
./pic_ori/陈自郁/陈自郁.jpg
./pic_ori/宋伟/宋伟.jpg
Unable to align "./pic_ori/宋伟/宋伟.jpg"
./pic_ori/涂凤华/涂凤华.jpg
Unable to align "./pic_ori/涂凤华/涂凤华.jpg"
./pic_ori/熊忠阳/熊忠阳.jpg
./pic_ori/李季/李季.jpg
./pic_ori/黄宏宇/黄宏宇.jpg
./pic_ori/叶春晓/叶春晓.jpg
./pic_ori/叶春晓/叶春晓.jpg: cannot identify image file './pic_ori/叶春晓/叶春晓.jpg'
./pic_ori/向涛/向涛.jpg
./pic_ori/伍星/伍星.jpg
./pic_ori/李学明/李学明.jpg
./pic_ori/杨瑞龙/杨瑞龙.jpg
./pic_ori/谭玉娟/谭玉娟.jpg
./pic_ori/何静媛/何静媛.jpg
./pic_ori/房斌/房斌.jpg
./pic_ori/王波/王波.jpg
./pic_ori/刘然/刘然.jpg
./pic_ori/刘慧君/刘慧君.jpg
./pic_ori/张敏/张敏.jpg
./pic_ori/刘骥/刘骥.jpg
./pic_ori/邢永康/邢永康.jpg
./pic_ori/吴长泽/吴长泽.jpg
./pic_ori/郑洪英/郑洪英.jpg
./pic_ori/万里/万里.jpg
./pic_ori/刘斌/刘斌.jpg
./pic_ori/古平/古平.jpg
./pic_ori/王茜/王茜.jpg
Total number of images: 77
Number of successfully aligned images: 72

完成以后,再多做一个原来的目录结构的数据集备用,最后把所有文件传回我的Drive

In [78]:
pic_list = os.listdir("pic_aligned")

for iname in pic_list:
  try:
    shutil.copyfile("pic_aligned/{}/{}.png".format(iname,iname),"pic_aligned_flat/{}.png".format(iname))
  except Exception as e:
    print(e)
    pass
os.listdir("pic_aligned_flat")
[Errno 20] Not a directory: 'pic_aligned/bounding_boxes_02930.txt/bounding_boxes_02930.txt.png'
[Errno 2] No such file or directory: 'pic_aligned/陈波/陈波.png'
[Errno 2] No such file or directory: 'pic_aligned/钟世刚/钟世刚.png'
[Errno 2] No such file or directory: 'pic_aligned/宋伟/宋伟.png'
[Errno 20] Not a directory: 'pic_aligned/bounding_boxes_95708.txt/bounding_boxes_95708.txt.png'
[Errno 2] No such file or directory: 'pic_aligned/涂凤华/涂凤华.png'
[Errno 2] No such file or directory: 'pic_aligned/叶春晓/叶春晓.png'
Out[78]:
['钟将.png',
 '周尚波.png',
 '郭平.png',
 '黄仁.png',
 '夏云霓.png',
 '尚家兴.png',
 '刘卫宁.png',
 '李双庆.png',
 '周明强.png',
 '刘然.png',
 '王波.png',
 '曾令秋.png',
 '万里.png',
 '罗军.png',
 '文静.png',
 '邢永康.png',
 '刘凯.png',
 '刘大江.png',
 '邹东升.png',
 '葛亮.png',
 '熊忠阳.png',
 '陈自郁.png',
 '张敏.png',
 '尚赵伟.png',
 '古平.png',
 '石锐.png',
 '刘慧君.png',
 '房斌.png',
 '刘骥.png',
 '何中市.png',
 '吴长泽.png',
 '杨吉云.png',
 '李佳.png',
 '李学明.png',
 '郑洪英.png',
 '杨广超.png',
 '朱征宇.png',
 '叶莲.png',
 '李季.png',
 '王茜.png',
 '郭松涛.png',
 '张太平.png',
 '吴开贵.png',
 '邓绍江.png',
 '向涛.png',
 '汪成亮.png',
 '吴全旺.png',
 '刘斌.png',
 '陈四清.png',
 '谭玉娟.png',
 '刘韦辰.png',
 '周庆.png',
 '何静媛.png',
 '冯永.png',
 '张程.png',
 '曽一.png',
 '杨瑞龙.png',
 '朱庆生.png',
 '冯亮.png',
 '朱晓红.png',
 '郑林江.png',
 '廖晓峰.png',
 '伍星.png',
 '孙天昊.png',
 '石亮.png',
 '张玉芳.png',
 '肖春华.png',
 '陈恒鑫.png',
 '陈超.png',
 '黄宏宇.png',
 '肖迪.png',
 '尹云飞.png']
In [0]:
!cp -r pic_aligned drive/DS_final/picture_aligned
!cp -r pic_aligned_flat drive/DS_final/picture_aligned_flat