Spaces:
Runtime error
Runtime error
Commit
•
d737fdb
1
Parent(s):
7ea273a
major fixes for image
Browse files- .gitignore +2 -1
- app.py +5 -4
- server.py +0 -154
.gitignore
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
/.venv/*
|
|
|
|
1 |
+
/.venv/*
|
2 |
+
/flagged/*
|
app.py
CHANGED
@@ -40,9 +40,9 @@ def main_pipeline(image):
|
|
40 |
|
41 |
#image = im_b64.file.read()
|
42 |
|
43 |
-
image = Image.open(
|
44 |
|
45 |
-
image = image.convert("RGB")
|
46 |
|
47 |
image = np.asarray(image)
|
48 |
|
@@ -62,7 +62,8 @@ def main_pipeline(image):
|
|
62 |
#cv2.imshow('image', np.asarray(image))
|
63 |
#cv2.waitKey()
|
64 |
|
65 |
-
image =
|
|
|
66 |
image = tf.keras.preprocessing.image.img_to_array(image)
|
67 |
image = image / 255.0
|
68 |
image = tf.expand_dims(image, axis=0)
|
@@ -93,7 +94,7 @@ def main_pipeline(image):
|
|
93 |
return predictions_json
|
94 |
|
95 |
# initializing the input component
|
96 |
-
image = gr.inputs.Image(shape = (
|
97 |
# initializing the output component
|
98 |
labels = gr.outputs.Label()
|
99 |
|
|
|
40 |
|
41 |
#image = im_b64.file.read()
|
42 |
|
43 |
+
#image = Image.open(image)
|
44 |
|
45 |
+
#image = image.convert("RGB")
|
46 |
|
47 |
image = np.asarray(image)
|
48 |
|
|
|
62 |
#cv2.imshow('image', np.asarray(image))
|
63 |
#cv2.waitKey()
|
64 |
|
65 |
+
image = image.resize((224,224))
|
66 |
+
#image = tf.image.resize(image, [224,224])
|
67 |
image = tf.keras.preprocessing.image.img_to_array(image)
|
68 |
image = image / 255.0
|
69 |
image = tf.expand_dims(image, axis=0)
|
|
|
94 |
return predictions_json
|
95 |
|
96 |
# initializing the input component
|
97 |
+
image = gr.inputs.Image(shape = (224, 224))
|
98 |
# initializing the output component
|
99 |
labels = gr.outputs.Label()
|
100 |
|
server.py
DELETED
@@ -1,154 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
import os
|
3 |
-
from fastapi import FastAPI, Request
|
4 |
-
from matplotlib import pyplot as plt
|
5 |
-
from PIL import Image
|
6 |
-
from fastapi.middleware.cors import CORSMiddleware
|
7 |
-
|
8 |
-
import tensorflow as tf
|
9 |
-
import numpy as np
|
10 |
-
import yaml
|
11 |
-
import io
|
12 |
-
import cv2
|
13 |
-
|
14 |
-
|
15 |
-
# read config file
|
16 |
-
def read_config():
|
17 |
-
config = {}
|
18 |
-
print(os.path.curdir)
|
19 |
-
with open(os.path.join('api','models_config.yaml'), 'r') as cf:
|
20 |
-
config = yaml.safe_load(cf)
|
21 |
-
|
22 |
-
for var in config:
|
23 |
-
config[var] = config[var].replace(';', os.sep)
|
24 |
-
|
25 |
-
return config
|
26 |
-
|
27 |
-
|
28 |
-
# create app and load model
|
29 |
-
config = read_config()
|
30 |
-
service = FastAPI()
|
31 |
-
|
32 |
-
# read path to test images
|
33 |
-
test_img = [os.path.join(config['TEST_IMG_PATH'], 'test1.jpg'), os.path.join(config['TEST_IMG_PATH'], 'test2.jpg'), os.path.join(config['TEST_IMG_PATH'], 'test3.jpg'),
|
34 |
-
os.path.join(config['TEST_IMG_PATH'], 'test4.jpg'), os.path.join(config['TEST_IMG_PATH'], 'test5.jpg'), os.path.join(config['TEST_IMG_PATH'], 'test6.jpg'),
|
35 |
-
os.path.join(config['TEST_IMG_PATH'], 'test7.jpg')]
|
36 |
-
|
37 |
-
# load pretrained models
|
38 |
-
age_model = tf.keras.models.load_model(config['A_M_PATH'])
|
39 |
-
gender_model = tf.keras.models.load_model(config['G_M_PATH'])
|
40 |
-
face_cascade = cv2.CascadeClassifier(config['FD_M_PATH'])
|
41 |
-
|
42 |
-
|
43 |
-
# add CORS middleware
|
44 |
-
service.add_middleware(
|
45 |
-
CORSMiddleware,
|
46 |
-
allow_origins=['*']
|
47 |
-
)
|
48 |
-
|
49 |
-
|
50 |
-
# status route
|
51 |
-
@service.get("/checkstatus")
|
52 |
-
async def read_root():
|
53 |
-
"""
|
54 |
-
Check status of routes
|
55 |
-
"""
|
56 |
-
|
57 |
-
url_list = [{"path": route.path, "name": route.name} for route in service.routes]
|
58 |
-
|
59 |
-
if len(url_list) == 7:
|
60 |
-
return "Healthy"
|
61 |
-
else:
|
62 |
-
return "Unhealthy"
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
async def make_predictions_pipeline(request, from_slider= False):
|
67 |
-
|
68 |
-
if from_slider:
|
69 |
-
file = await request.form()
|
70 |
-
img_ind = file['img']
|
71 |
-
image = Image.open(test_img[int(img_ind)])
|
72 |
-
else:
|
73 |
-
file = await request.form()
|
74 |
-
im_b64 = file['img']
|
75 |
-
|
76 |
-
image = im_b64.file.read()
|
77 |
-
image = Image.open(io.BytesIO(image))
|
78 |
-
image = image.convert("RGB")
|
79 |
-
|
80 |
-
image = np.asarray(image)
|
81 |
-
|
82 |
-
gray_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
83 |
-
faces = face_cascade.detectMultiScale(gray_img, 1.1, 4)
|
84 |
-
|
85 |
-
if len(faces) == 0:
|
86 |
-
return "NO FACE DETECTED"
|
87 |
-
|
88 |
-
x, y, w, h = faces[0]
|
89 |
-
#cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
90 |
-
|
91 |
-
|
92 |
-
image = Image.fromarray(image)
|
93 |
-
image = image.crop((x, y, x + w, y + h))
|
94 |
-
|
95 |
-
#cv2.imshow('image', np.asarray(image))
|
96 |
-
#cv2.waitKey()
|
97 |
-
|
98 |
-
image = tf.image.resize(image, [224,224])
|
99 |
-
image = tf.keras.preprocessing.image.img_to_array(image)
|
100 |
-
image = image / 255.0
|
101 |
-
image = tf.expand_dims(image, axis=0)
|
102 |
-
|
103 |
-
age_prds = age_model.predict(image)
|
104 |
-
gender_prds = gender_model.predict(image)
|
105 |
-
|
106 |
-
age_prds = np.around(age_prds)
|
107 |
-
gender_prds = np.around(gender_prds)
|
108 |
-
gender = ""
|
109 |
-
|
110 |
-
if gender_prds[0][0] == 0:
|
111 |
-
gender = 'male'
|
112 |
-
else:
|
113 |
-
gender = 'female'
|
114 |
-
|
115 |
-
data = {}
|
116 |
-
data['age'] = str(age_prds[0][0])
|
117 |
-
data['gender'] = gender
|
118 |
-
b_box_arr = [(x, y, w, h) for (x, y, w, h) in faces]
|
119 |
-
|
120 |
-
data['left'] = str(b_box_arr[0][0])
|
121 |
-
data['top'] = str(b_box_arr[0][1])
|
122 |
-
data['width'] = str(b_box_arr[0][2])
|
123 |
-
data['height'] = str(b_box_arr[0][3])
|
124 |
-
predictions_json = json.dumps(data)
|
125 |
-
|
126 |
-
return predictions_json
|
127 |
-
|
128 |
-
|
129 |
-
# slider predictions route
|
130 |
-
@service.post("/api/predictions_slider")
|
131 |
-
async def receive_image(request: Request):
|
132 |
-
"""
|
133 |
-
Function for predicts from slider
|
134 |
-
"""
|
135 |
-
results_json = await make_predictions_pipeline(request, from_slider= True)
|
136 |
-
return results_json
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
# main predictions route
|
141 |
-
@service.post("/api/predictions")
|
142 |
-
async def receive_image(request: Request):
|
143 |
-
"""
|
144 |
-
Function for predicts
|
145 |
-
|
146 |
-
Example request:
|
147 |
-
|
148 |
-
img = open( FILEPATH , 'rb')
|
149 |
-
files = {'img': img}
|
150 |
-
resp = requests.post("http://{host:port}/api/predictions", files= files)
|
151 |
-
"""
|
152 |
-
|
153 |
-
results_json = await make_predictions_pipeline(request, from_slider= False)
|
154 |
-
return results_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|