-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgeGenderAPI.py
69 lines (59 loc) · 2.46 KB
/
AgeGenderAPI.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
import cv2
import dlib
import numpy as np
import urllib.request as ur
import tensorflow as tf
from openvino.runtime import Core
import os
def model_init(model):
ie_core = Core()
model = ie_core.read_model(model=model)
compiled_model = ie_core.compile_model(model=model, device_name="CPU")
input_keys = compiled_model.input(0)
output_keys = compiled_model.output(0)
return input_keys, output_keys, compiled_model
def AgeGender_detection(image_encoded, file_path):
ir_model_xml = "saved_model.xml"
ir_model_bin = "saved_model.bin"
inputs, outputs, compiled_model = model_init(ir_model_xml)
margin = 0.4
img_size = 64
detector = dlib.get_frontal_face_detector()
decoded = ur.urlopen(image_encoded)
img = decoded.file.read()
np_data = np.frombuffer(img, np.uint8)
img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)
img_h, img_w, _ = np.shape(img)
detected = detector(img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
if len(detected) > 0:
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + \
1, d.bottom() + 1, d.width(), d.height()
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
faces[i, :, :, :] = cv2.resize(
img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
# make a prediction for Age and Gender
results = compiled_model([faces])
a = dict(zip(['gender', 'age'], results.values()))
age = a['age']
gender = a['gender']
ages = np.arange(0, 101).reshape(101, 1)
predicted_ages = age.dot(ages).flatten()
Gender="Female" if gender[0][0] > 0.4 else "Male"
filename, extension = os.path.splitext(file_path)
with open("results.txt", 'a') as outfile:
outfile.write(filename+" is "+ Gender+ '\n')
outfile.write(filename+"'s age is %d\n" % (int(predicted_ages)))
outfile.write("..................................."+'\n')
def read_text_file(file_path):
with open(file_path, 'r') as f:
selfie= f.read()
AgeGender_detection(selfie,file_path)
for file in os.listdir():
if file.endswith(".txt"):
file_path = f"{file}"
read_text_file(file_path)