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 # %pylab inline
 7 
 8 minsize = 20  # minimum size of face
 9 threshold = [0.6, 0.7, 0.7]  # three steps's threshold
10 factor = 0.709  # scale factor
11 margin = 44
12 frame_interval = 3
13 batch_size = 1000
14 image_size = 182
15 input_image_size = 160
16 
17 print('Creating networks and loading parameters')
18 
19 with tf.Graph().as_default():
20     gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
21     sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
22     with sess.as_default():
23         pnet, rnet, onet = detect_face.create_mtcnn(sess, 'D:\\pycode\\real-time-deep-face-recognition-master\\20170512-110547')
24 
25 image_path = 'D:\\Users\\a\\Pictures\\test_pho\\5.jpg'
26 
27 img = misc.imread(image_path)
28 bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
29 nrof_faces = bounding_boxes.shape[0]  # 人脸数目
30 print('找到人脸数目为:{}'.format(nrof_faces))
31 
32 print(bounding_boxes)
33 
34 crop_faces = []
35 for face_position in bounding_boxes:
36     face_position = face_position.astype(int)
37     print(face_position[0:4])
38     cv2.rectangle(img, (face_position[0], face_position[1]), (face_position[2], face_position[3]), (0, 255, 0), 2)
39     crop = img[face_position[1]:face_position[3],
40            face_position[0]:face_position[2], ]
41 
42     crop = cv2.resize(crop, (96, 96), interpolation=cv2.INTER_CUBIC)
43     print(crop.shape)
44     crop_faces.append(crop)
45     print(crop)
46     plt.imshow(crop)
47     plt.show()
48 
49 plt.imshow(img)
50 plt.show()

 

 

posted @ 2018-01-12 11:03  ayew  阅读(3034)  评论(1编辑  收藏  举报