-
Notifications
You must be signed in to change notification settings - Fork 35
/
crop_faces_save.py
65 lines (44 loc) · 1.7 KB
/
crop_faces_save.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Author: coneypo
# Blog: http://www.cnblogs.com/AdaminXie
# GitHub: https://github.com/coneypo/Dlib_face_cut
import dlib
import numpy as np
import cv2
import os
# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()
# Dlib 人脸 landmark 特征点检测器
predictor = dlib.shape_predictor('data/dlib/shape_predictor_68_face_landmarks.dat')
# 读取图像的路径
path_read = "data/images/faces_for_test/"
img = cv2.imread(path_read + "test_faces_10.jpg")
# 用来存储生成的单张人脸的路径
path_save = "data/images/faces_separated/"
def mkdir_for_save_images():
if not os.path.isdir(path_save):
os.mkdir(path_save)
def clear_images():
img_list = os.listdir(path_save)
for img in img_list:
os.remove(path_save + img)
def main():
# 新建文件夹, 清理存下来的图像文件
mkdir_for_save_images()
clear_images()
faces = detector(img, 1)
print("人脸数 / faces in all:", len(faces), '\n')
for num, face in enumerate(faces):
# 计算矩形框大小
height = face.bottom() - face.top()
width = face.right() - face.left()
# 根据人脸大小生成空的图像
img_blank = np.zeros((height, width, 3), np.uint8)
for i in range(height):
for j in range(width):
img_blank[i][j] = img[face.top() + i][face.left() + j]
# cv2.imshow("face_"+str(num+1), img_blank)
# 存在本地
print("Save into:", path_save + "img_face_" + str(num + 1) + ".jpg")
cv2.imwrite(path_save + "img_face_" + str(num + 1) + ".jpg", img_blank)
if __name__ == '__main__':
main()