-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcut_face.py
55 lines (40 loc) · 1.38 KB
/
cut_face.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
# -*- coding: utf-8 -*-
import os
import sys
import cv2
import face_recognition
# 设置递归深度
sys.setrecursionlimit(1000000)
def get_face(filepath):
img = cv2.imread(filepath)
face_locations = face_recognition.face_locations(img)
if len(face_locations) == 1:
# 只处理拥有一个人脸的图片
(top, right, bottom, left) = face_locations[0]
# dst = cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 255), 1)
# 左上坐标:(1,2) 右下坐标:(3,4) face = img[2:4,1:3]
face = cv2.resize(img[top: bottom, left: right], (128, 128))
print(''.join(filepath.split('.')[:-1]) + '.png')
save_face(face, ''.join(filepath.split('.')[:-1]) + '.png')
def show_face(face):
cv2.imshow('', face)
cv2.waitKey(0)
cv2.destroyAllWindows()
def save_face(face, filename):
cv2.imwrite(filename=filename, img=face)
def list_dir(rootdir):
"""遍历目录"""
files = []
child_list = os.listdir(rootdir)
for i in range(len(child_list)):
path = os.path.join(rootdir, child_list[i])
if os.path.isdir(path):
files.extend(list_dir(rootdir))
if os.path.isfile(path):
files.append(path.replace('\\', '/'))
return files
if __name__ == '__main__':
# for file in list_dir('faces'):
# print(file)
# get_face(file)
get_face('faces/4.jpg')