-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcroping.py
199 lines (163 loc) · 6.9 KB
/
croping.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
import sys
import io
import base64
from PIL import Image
from PIL import ImageDraw
from genericpath import isfile
import os
from oauth2client.service_account import ServiceAccountCredentials
NUM_THREADS = 10
MAX_RESULTS = 1
IMAGE_SIZE = 96, 96
class FaceDetector():
def __init__(self):
# initialize library
# credentials = GoogleCredentials.get_application_default()
scopes = ['https://www.googleapis.com/auth/cloud-platform']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'C:/Users/joon2/Desktop/Jipgyo1-a1c841c90c8c.json', scopes=scopes)
self.service = discovery.build('vision', 'v1', credentials=credentials)
# print ("Getting vision API client : %s" ,self.service)
# def extract_face(selfself,image_file,output_file):
def detect_face(self, image_file):
try:
with io.open(image_file, 'rb') as fd:
image = fd.read()
batch_request = [{
'image': {
'content': base64.b64encode(image).decode('utf-8')
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': MAX_RESULTS,
}]
}]
fd.close()
request = self.service.images().annotate(body={
'requests': batch_request, })
response = request.execute()
if 'faceAnnotations' not in response['responses'][0]:
print('[Error] %s: Cannot find face ' % image_file)
return None
face = response['responses'][0]['faceAnnotations']
box = face[0]['fdBoundingPoly']['vertices']
left = box[0]['x']
top = box[1]['y']
right = box[2]['x']
bottom = box[2]['y']
rect = [left, top, right, bottom]
print("[Info] %s: Find face from in position %s" % (image_file, rect))
return rect
except Exception as e:
print('[Error] %s: cannot process file : %s' % (image_file, str(e)))
def rect_face(self, image_file, rect, outputfile):
try:
fd = io.open(image_file, 'rb')
image = Image.open(fd)
draw = ImageDraw.Draw(image)
draw.rectangle(rect, fill=None, outline="green")
image.save(outputfile)
fd.close()
print('[Info] %s: Mark face with Rect %s and write it to file : %s' % (image_file, rect, outputfile))
except Exception as e:
print('[Error] %s: Rect image writing error : %s' % (image_file, str(e)))
def crop_face(self, image_file, rect, outputfile):
try:
fd = io.open(image_file, 'rb')
image = Image.open(fd)
crop = image.crop(rect)
im = crop.resize(IMAGE_SIZE, Image.ANTIALIAS)
im.save(outputfile, "JPEG")
fd.close()
print('[Info] %s: Crop face %s and write it to file : %s' % (image_file, rect, outputfile))
except Exception as e:
print('[Error] %s: Crop image writing error : %s' % (image_file, str(e)))
def getfiles(self, src_dir):
files = []
for f in os.listdir(src_dir):
if isfile(os.path.join(src_dir, f)):
if not f.startswith('.'):
files.append(os.path.join(src_dir, f))
return files
def rect_faces_dir(self, src_dir, des_dir):
if not os.path.exists(des_dir):
os.makedirs(des_dir)
files = self.getfiles(src_dir)
for f in files:
des_file = os.path.join(des_dir, os.path.basename(f))
rect = self.detect_face(f)
if rect != None:
self.rect_face(f, rect, des_file)
def crop_faces_dir(self, src_dir, des_dir):
# training data will be written in $des_dir/training
# validation data will be written in $des_dir/validate
des_dir_training = os.path.join(des_dir, 'training')
des_dir_validate = os.path.join(des_dir, 'validate')
if not os.path.exists(des_dir):
os.makedirs(des_dir)
if not os.path.exists(des_dir_training):
os.makedirs(des_dir_training)
if not os.path.exists(des_dir_validate):
os.makedirs(des_dir_validate)
path, folder_name = os.path.split(src_dir)
label = folder_name
# create label file. it will contains file location
# and label for each file
training_file = open('training_file.txt', 'a')
validate_file = open('validate_file.txt', 'a')
files = self.getfiles(src_dir)
cnt = 0
for f in files:
rect = self.detect_face(f)
# replace ',' in file name to '.'
# because ',' is used for deliminator of image file name and its label
des_file_name = os.path.basename(f)
des_file_name = des_file_name.replace(',', '_')
if rect != None:
# 70% of file will be stored in training data directory
if (cnt < 8):
des_file = os.path.join(des_dir_training, des_file_name)
self.crop_face(f, rect, des_file)
training_file.write("%s,%s\n" % (des_file, label))
# 30% of files will be stored in validation data directory
else: # for validation data
des_file = os.path.join(des_dir_validate, des_file_name)
self.crop_face(f, rect, des_file)
validate_file.write("%s,%s\n" % (des_file, label))
if (cnt > 9):
cnt = 0
cnt = cnt + 1
training_file.close()
validate_file.close()
def getdirs(self, dir):
dirs = []
for f in os.listdir(dir):
f = os.path.join(dir, f)
if os.path.isdir(f):
if not f.startswith('.'):
dirs.append(f)
return dirs
def crop_faces_rootdir(self, src_dir, des_dir):
# crop file from sub-directoris in src_dir
dirs = self.getdirs(src_dir)
# list sub directory
for d in dirs:
print('[INFO] : ### Starting cropping in directory %s ###' % d)
self.crop_faces_dir(d, des_dir)
# loop and run face crop
def main(argv):
# print(os.getcwd())
# srcdir = argv[1]
# desdir = argv[2]
srcdir = "/Users/joon2/PycharmProjects/jipjung/이은창"
desdir = "/Users/joon2/PycharmProjects/jipjung/이은창A"
detector = FaceDetector()
detector.crop_faces_rootdir(srcdir, desdir)
# detector.crop_faces_dir(inputfile,outputfile)
# rect = detector.detect_face(inputfile)
# detector.rect_image(inputfile, rect, outputfile)
# detector.crop_face(inputfile, rect, outputfile)
if __name__ == "__main__":
main(sys.argv)