Spaces:
Runtime error
Runtime error
VanShingel
commited on
Commit
•
5a721da
1
Parent(s):
c9d37f2
base model
Browse files- app.py +97 -5
- images/test1.jpg +0 -0
- images/test2.jpg +0 -0
- images/test3.jpg +0 -0
- images/test4.jpg +0 -0
- images/test5.jpg +0 -0
- images/test6.jpg +0 -0
- images/test7.jpg +0 -0
- models/age_model.h5 +3 -0
- models/gender_model.h5 +3 -0
- models/haarcascade_frontalface_default.xml +0 -0
- models_config.yaml +5 -0
- requeriments.txt +0 -0
app.py
CHANGED
@@ -1,11 +1,103 @@
|
|
1 |
'''
|
2 |
-
|
3 |
'''
|
4 |
|
|
|
5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
'''
|
2 |
+
Build Project
|
3 |
'''
|
4 |
|
5 |
+
# imported necessary libraries
|
6 |
import gradio as gr
|
7 |
+
import tensorflow as tf
|
8 |
+
import numpy as np
|
9 |
+
import os
|
10 |
+
import yaml
|
11 |
+
import cv2
|
12 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
+
import json
|
15 |
|
16 |
+
# read config file
|
17 |
+
def read_config():
|
18 |
+
config = {}
|
19 |
+
print(os.path.curdir)
|
20 |
+
with open('models_config.yaml', 'r') as cf:
|
21 |
+
config = yaml.safe_load(cf)
|
22 |
|
23 |
+
for var in config:
|
24 |
+
config[var] = config[var].replace(';', os.sep)
|
25 |
+
|
26 |
+
return config
|
27 |
+
|
28 |
+
config = read_config()
|
29 |
+
|
30 |
+
# loading the models
|
31 |
+
age_model = tf.keras.models.load_model(config['A_M_PATH'])
|
32 |
+
gender_model = tf.keras.models.load_model(config['G_M_PATH'])
|
33 |
+
face_cascade = cv2.CascadeClassifier(config['FD_M_PATH'])
|
34 |
+
|
35 |
+
|
36 |
+
def main_pipeline(image):
|
37 |
+
|
38 |
+
#file = request.form()
|
39 |
+
#im_b64 = file['img']
|
40 |
+
|
41 |
+
#image = im_b64.file.read()
|
42 |
+
|
43 |
+
image = Image.open(io.BytesIO(image))
|
44 |
+
|
45 |
+
image = image.convert("RGB")
|
46 |
+
|
47 |
+
image = np.asarray(image)
|
48 |
+
|
49 |
+
gray_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
50 |
+
faces = face_cascade.detectMultiScale(gray_img, 1.1, 4)
|
51 |
+
|
52 |
+
if len(faces) == 0:
|
53 |
+
return "NO FACE DETECTED"
|
54 |
+
|
55 |
+
x, y, w, h = faces[0]
|
56 |
+
#cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
57 |
+
|
58 |
+
|
59 |
+
image = Image.fromarray(image)
|
60 |
+
image = image.crop((x, y, x + w, y + h))
|
61 |
+
|
62 |
+
#cv2.imshow('image', np.asarray(image))
|
63 |
+
#cv2.waitKey()
|
64 |
+
|
65 |
+
image = tf.image.resize(image, [224,224])
|
66 |
+
image = tf.keras.preprocessing.image.img_to_array(image)
|
67 |
+
image = image / 255.0
|
68 |
+
image = tf.expand_dims(image, axis=0)
|
69 |
+
|
70 |
+
age_prds = age_model.predict(image)
|
71 |
+
gender_prds = gender_model.predict(image)
|
72 |
+
|
73 |
+
age_prds = np.around(age_prds)
|
74 |
+
gender_prds = np.around(gender_prds)
|
75 |
+
gender = ""
|
76 |
+
|
77 |
+
if gender_prds[0][0] == 0:
|
78 |
+
gender = 'male'
|
79 |
+
else:
|
80 |
+
gender = 'female'
|
81 |
+
|
82 |
+
data = {}
|
83 |
+
data['age'] = str(age_prds[0][0])
|
84 |
+
data['gender'] = gender
|
85 |
+
b_box_arr = [(x, y, w, h) for (x, y, w, h) in faces]
|
86 |
+
|
87 |
+
data['left'] = str(b_box_arr[0][0])
|
88 |
+
data['top'] = str(b_box_arr[0][1])
|
89 |
+
data['width'] = str(b_box_arr[0][2])
|
90 |
+
data['height'] = str(b_box_arr[0][3])
|
91 |
+
predictions_json = json.dumps(data)
|
92 |
+
|
93 |
+
return predictions_json
|
94 |
+
|
95 |
+
# initializing the input component
|
96 |
+
image = gr.inputs.Image(shape = (299, 299, 3))
|
97 |
+
# initializing the output component
|
98 |
+
labels = gr.outputs.Label()
|
99 |
+
|
100 |
+
|
101 |
+
# launching the interface
|
102 |
+
gr.Interface(fn = main_pipeline, inputs = image,
|
103 |
+
outputs = labels, capture_session = True).launch()
|
images/test1.jpg
ADDED
images/test2.jpg
ADDED
images/test3.jpg
ADDED
images/test4.jpg
ADDED
images/test5.jpg
ADDED
images/test6.jpg
ADDED
images/test7.jpg
ADDED
models/age_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4a79e82415e7f0da4e95873f6db3bbcc9ea141a7b3fdf8ac7463e50a4abf21ff
|
3 |
+
size 83884320
|
models/gender_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c5e6e1d0c0dda25a1d89dae4930f0de0939b4aabd4355454e14bc2aba27cf12e
|
3 |
+
size 83884328
|
models/haarcascade_frontalface_default.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
models_config.yaml
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
A_M_PATH: api;models;age_model.h5
|
2 |
+
G_M_PATH: api;models;gender_model.h5
|
3 |
+
FD_M_PATH: api;models;haarcascade_frontalface_default.xml
|
4 |
+
|
5 |
+
TEST_IMG_PATH: api;images
|
requeriments.txt
ADDED
Binary file (3.45 kB). View file
|
|