Mtcnn进行人脸剪裁和对齐B

Mtcnn进行人脸剪裁和对齐

 1 from scipy import misc
 2 import tensorflow as tf
 3 import detect_face
 4 import cv2
 5 # import matplotlib.pyplot as plt
 6 from PIL import Image
 7 import os
 8 # import scipy.misc
 9 # %pylab inline
10 fin = 'D:datamale'
11 fout = 'D:data\rainmale'
12 minsize = 20  # minimum size of face
13 threshold = [0.6, 0.7, 0.7]  # three steps's threshold
14 factor = 0.709  # scale factor
15 margin = 44
16 frame_interval = 3
17 batch_size = 1000
18 image_size = 182
19 input_image_size = 160
20 
21 print('Creating networks and loading parameters')
22 
23 with tf.Graph().as_default():
24     gpu_options = tf.GPUOptions(allow_growth=True)
25     sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
26     with sess.as_default():
27         pnet, rnet, onet = detect_face.create_mtcnn(sess, 'D:\code\real-time-deep-face-recognition-master\20170512-110547')
28 
29 
30 i= 0
31 
32 for file in os.listdir(fin):
33     try:
34             
35         file_fullname = fin + '/' + file
36         img = misc.imread(file_fullname)
37         # i+= 1
38         # img = misc.imread(image_path)
39         bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
40         nrof_faces = bounding_boxes.shape[0]  # 人脸数目
41         print(nrof_faces)
42         #print('找到人脸数目为:{}'.format(nrof_faces))
43     
44         # print(bounding_boxes)
45     
46         crop_faces = []
47         if nrof_faces != 0 :
48             for face_position in bounding_boxes:
49                 face_position = face_position.astype(int)
50                 print(face_position[0:4])
51                 cv2.rectangle(img, (face_position[0], face_position[1]), (face_position[2], face_position[3]), (0, 255, 0), 2)
52                 crop = img[face_position[1]:face_position[3],
53                        face_position[0]:face_position[2], ]
54                 # print(crop)
55                 # crop = cv2.resize(crop, (96, 96), interpolation=cv2.INTER_CUBIC)
56                 crop_faces.append(crop)
57                 img2 = Image.open(file_fullname)
58                 a = face_position[0:4]
59                 # print('crop_faces:',crop_faces)
60                 # a = [face_position[0:4]]
61                 box = (a)
62                 roi = img2.crop(box)
63                 i = roi.resize((224, 224))
64     
65                 out_path = fout + '/' + file
66     
67                 i.save(out_path)
68                 print('success')
69         else:
70             pass
71     except:
72         pass
原文地址:https://www.cnblogs.com/ansang/p/8313302.html