-
Notifications
You must be signed in to change notification settings - Fork 2
/
face-attributes-atlanta.py
187 lines (168 loc) · 7.36 KB
/
face-attributes-atlanta.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
import urllib.request
import urllib.error
import time
import json
import socket
from datetime import datetime
import glob
import csv
# api_key = 'g06BZoz_VFEJxvzaLPTlQI-ZsbhCbRbU'
# api_secret = 'EHoHJiGyJn-IoeHNBMGNlUFBKhoLmyci'
http_url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
api_key = 'AFWkr-35C8LACVx1raEXqnJMwb494tkA'
api_secret = 'Rj1-hdcAQxmQKWRQgiZtcQO8VxKT30E0'
def getFaceAttributes():
print(datetime.now())
columns = ['image_path', 'gender', 'age', 'smile_value', 'smile_threshold', 'emotion_anger', 'emotion_disgust', 'emotion_fear', 'emotion_happiness', 'emotion_neutral', 'emotion_sadness', 'emotion_surprise', 'ethnicity', 'beauty_male_score', 'beauty_female_score', 'mouthstatus_surgical_mask_or_respirator',
'mouthstatus_other_occlusion', 'mouthstatus_close', 'mouthstatus_open', 'skinstatus_health', 'skinstatus_stain', 'skinstatus_dark_circle', 'skinstatus_acne', 'face_rectangle_top', 'face_rectangle_left', 'face_rectangle_width', 'face_rectangle_height', 'status', 'time_stamp']
with open("face_attributes_atlanta_part4.csv", 'w') as csvfile:
# csv.DictWriter(csvfile, fieldnames=columns)
csvwriter = csv.writer(csvfile, lineterminator='\n')
csvwriter.writerow(columns)
i = 0
for filepath in glob.iglob(r'../atlanta/*.jpg'):
i = i+1
print(i)
if i < 24822 : continue
rows, message = getFace(filepath)
if rows is not None and message is 'success':
csvwriter.writerows(rows)
else:
time_stamp = datetime.now()
vals = [filepath, "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL",
"NULL", "NULL", "NULL", "NULL", message, time_stamp]
csvwriter.writerow(vals)
print(datetime.now())
print('finished')
def getFace(imagepath):
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key')
data.append(api_key)
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret')
data.append(api_secret)
data.append('--%s' % boundary)
fr = open(imagepath, 'rb')
data.append(
'Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file')
data.append('Content-Type: %s\r\n' % 'application/octet-stream')
data.append(fr.read())
fr.close()
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' %
'return_landmark')
data.append('1')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' %
'return_attributes')
data.append(
"gender,smiling,age,emotion,ethnicity,beauty,mouthstatus,skinstatus")
data.append('--%s--\r\n' % boundary)
for i, d in enumerate(data):
if isinstance(d, str):
data[i] = d.encode('utf-8')
http_body = b'\r\n'.join(data)
# build http request
req = urllib.request.Request(url=http_url, data=http_body)
# header
req.add_header(
'Content-Type', 'multipart/form-data; boundary=%s' % boundary)
data.clear()
try:
# post data to server
resp = urllib.request.urlopen(req, timeout=20)
# get response
qrcont = resp.read()
# if you want to load as json, you should decode first,
# for example: json.loads(qrount.decode('utf-8'))
faces = json.loads(qrcont.decode('utf-8'))['faces']
if len(faces) == 0:
return None, 'No Faces'
else:
i = 0
all_data = []
for face in faces:
i += 1
# print(face)
if face is not None:
result = faceSQLformat(face, imagepath)
if result is not None:
all_data.append(result)
# face_csv = face_csv + result + '\n'
# print(result)
# return result
return all_data, 'success'
except urllib.error.HTTPError as e:
# print("--------------")
# print(e.read().decode('utf-8'))
# print("-----------------")
return None, e
except socket.timeout as e:
# print('Face detection Error in: ', type(e)) # catched
return None, e
def faceSQLformat(faceAttri, image_path):
if 'attributes' not in faceAttri:
return None
face = faceAttri['attributes']
gender = face['gender']['value'] if bool(face['gender']['value']) else None
age = face['age']['value'] if bool(face['age']['value']) else None
smile_value = face['smile']['value'] if bool(
face['smile']['value']) else None
smile_threshold = face['smile']['threshold'] if bool(
face['smile']['threshold']) else None
emotion_anger = face['emotion']['anger'] if bool(
face['emotion']['anger']) else None
emotion_disgust = face['emotion']['disgust'] if bool(
face['emotion']['disgust']) else None
emotion_fear = face['emotion']['fear'] if bool(
face['emotion']['fear']) else None
emotion_happiness = face['emotion']['happiness'] if bool(
face['emotion']['happiness']) else None
emotion_neutral = face['emotion']['neutral'] if bool(
face['emotion']['neutral']) else None
emotion_sadness = face['emotion']['sadness'] if bool(
face['emotion']['sadness']) else None
emotion_surprise = face['emotion']['surprise'] if bool(
face['emotion']['surprise']) else None
ethnicity = face['ethnicity']['value'] if bool(face['ethnicity']['value']) else None
beauty_male_score = face['beauty']['male_score'] if bool(
face['beauty']['male_score']) else None
beauty_female_score = face['beauty']['female_score'] if bool(
face['beauty']['female_score']) else None
mouthstatus_surgical_mask_or_respirator = face['mouthstatus']['surgical_mask_or_respirator'] if bool(
face['mouthstatus']['surgical_mask_or_respirator']) else None
mouthstatus_other_occlusion = face['mouthstatus']['other_occlusion'] if bool(
face['mouthstatus']['other_occlusion']) else None
mouthstatus_close = face['mouthstatus']['close'] if bool(
face['mouthstatus']['close']) else None
mouthstatus_open = face['mouthstatus']['open'] if bool(
face['mouthstatus']['open']) else None
skinstatus_health = face['skinstatus']['health'] if bool(
face['skinstatus']['health']) else None
skinstatus_stain = face['skinstatus']['stain'] if bool(
face['skinstatus']['stain']) else None
skinstatus_dark_circle = face['skinstatus']['dark_circle'] if bool(
face['skinstatus']['dark_circle']) else None
skinstatus_acne = face['skinstatus']['acne'] if bool(
face['skinstatus']['acne']) else None
face_rectangle_top = faceAttri['face_rectangle']['top'] if bool(
faceAttri['face_rectangle']['top']) else None
face_rectangle_left = faceAttri['face_rectangle']['left'] if bool(
faceAttri['face_rectangle']['left']) else None
face_rectangle_width = faceAttri['face_rectangle']['width'] if bool(
faceAttri['face_rectangle']['width']) else None
face_rectangle_height = faceAttri['face_rectangle']['height'] if bool(
faceAttri['face_rectangle']['height']) else None
status = 'processed'
time_stamp = datetime.now()
values = [image_path, gender, age, smile_value, smile_threshold, emotion_anger, emotion_disgust, emotion_fear,
emotion_happiness, emotion_neutral, emotion_sadness, emotion_surprise, ethnicity, beauty_male_score,
beauty_female_score, mouthstatus_surgical_mask_or_respirator, mouthstatus_other_occlusion,
mouthstatus_close, mouthstatus_open, skinstatus_health, skinstatus_stain, skinstatus_dark_circle,
skinstatus_acne, face_rectangle_top, face_rectangle_left, face_rectangle_width, face_rectangle_height, status, time_stamp]
none_null = ["NULL" if val == None else val for val in values]
return none_null
getFaceAttributes()