Mtcnn进行人脸剪裁和对齐B

简介: Mtcnn进行人脸剪裁和对齐 1 from scipy import misc 2 import tensorflow as tf 3 import detect_face 4 import cv2 5 # import matplotlib.

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:\data\male'
11 fout = 'D:\data\\rain\male'
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

 

相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
目录
相关文章
|
5月前
|
算法 Python
扭曲图像 鼻子拉伸
【6月更文挑战第28天】
29 0
|
6月前
|
算法 计算机视觉
缩放图像
【5月更文挑战第12天】缩放图像。
45 5
|
6月前
|
编解码 物联网 计算机视觉
【OpenCV】—图像金子塔与图片尺寸缩放
【OpenCV】—图像金子塔与图片尺寸缩放
|
6月前
|
计算机视觉 索引
【OpenCV】—ROI区域图像叠加&图像混合
【OpenCV】—ROI区域图像叠加&图像混合
|
C++ 计算机视觉
C++-图像目标区裁剪ImageCropping
C++-图像目标区裁剪ImageCropping
100 0
OpenCV-图像阴影调整
OpenCV-图像阴影调整
204 0
OpenCV-图像高光调整
OpenCV-图像高光调整
256 0
|
编解码
图片的变形与模糊,是两回事
图片的变形与模糊,是两回事
132 0
|
算法 计算机视觉
【4Opencv】如何识别出轮廓准确的长和宽
问题来源:实际项目中,需要给出识别轮廓的长度和宽度。 初步分析: 轮廓分析的例程为: int main( int argc, char** argv ){    //read the image    Mat img = imread("e:/sandbox/leaf.
1643 1