-
Notifications
You must be signed in to change notification settings - Fork 3
/
streamlit_app.py
359 lines (277 loc) · 15.1 KB
/
streamlit_app.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from io import BytesIO
import streamlit as st
import numpy as np
from PIL import Image, ImageColor
from keras.models import load_model
from keras_preprocessing.image import img_to_array
from streamlit_webrtc import webrtc_streamer, RTCConfiguration
import av
import cv2
model = load_model('GR.h5')
# Create gender classes
classes = {
0: 'female',
1: 'male'
}
RTC_CONFIGURATION = RTCConfiguration(
{"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
)
# Set page configs. Get emoji names from WebFx
st.set_page_config(page_title="Real-time Gender Detection", page_icon="./assets/faceman_cropped.png", layout="centered")
# -------------Header Section------------------------------------------------
title = '<p style="text-align: center;font-size: 40px;font-weight: 550; "> Real-time Gender Detection</p>'
st.markdown(title, unsafe_allow_html=True)
st.markdown(
"Gender Recognition using *Haar-Cascade Algorithm*, *OpenCV*, *and* *Tensorflow*.")
supported_modes = "<html> " \
"<body><div> <b>Supported Face Detection Modes (Change modes from sidebar menu)</b>" \
"<ul><li>Image Upload</li><li>Webcam Image Capture</li><li>Webcam Video Realtime</li></ul>" \
"</div></body></html>"
st.markdown(supported_modes, unsafe_allow_html=True)
st.warning("NOTE : Click the arrow icon at Top-Left to open Sidebar menu. ")
# -------------Sidebar Section------------------------------------------------
detection_mode = None
# Haar-Cascade Parameters
minimum_neighbors = 4
# Minimum possible object size
min_object_size = (50, 50)
# bounding box thickness
bbox_thickness = 3
# bounding box color
bbox_color = (0, 255, 0)
with st.sidebar:
st.image("./assets/faceman_cropped.png", width=260)
title = '<p style="font-size: 25px;font-weight: 550;">Face Detection Settings</p>'
st.markdown(title, unsafe_allow_html=True)
# choose the mode for detection
mode = st.radio("Choose Face Detection Mode", ('Image Upload',
'Webcam Image Capture',
'Webcam Real-time'), index=0)
if mode == 'Image Upload':
detection_mode = mode
elif mode == "Webcam Image Capture":
detection_mode = mode
elif mode == 'Webcam Real-time':
detection_mode = mode
# slider for choosing parameter values
minimum_neighbors = st.slider("Mininum Neighbors", min_value=0, max_value=10,
help="Parameter specifying how many neighbors each candidate "
"rectangle should have to retain it. This parameter will affect "
"the quality of the detected faces. Higher value results in less "
"detections but with higher quality.",
value=minimum_neighbors)
# slider for choosing parameter values
min_size = st.slider(f"Mininum Object Size, Eg-{min_object_size} pixels ", min_value=3, max_value=500,
help="Minimum possible object size. Objects smaller than that are ignored.",
value=70)
min_object_size = (min_size, min_size)
# Get bbox color and convert from hex to rgb
bbox_color = ImageColor.getcolor(str(st.color_picker(label="Bounding Box Color", value="#00FF00")), "RGB")
# ste bbox thickness
bbox_thickness = st.slider("Bounding Box Thickness", min_value=1, max_value=30,
help="Sets the thickness of bounding boxes",
value=bbox_thickness)
st.info("NOTE : The quality of detection will depend on above paramters."
" Try adjusting them as needed to get the most optimal output")
# line break
st.markdown(" ")
# About the programmer
st.markdown("## Made by *Penda Silas, Ogunjimi Ayobami, Ebenezer Acquah, Olabisi Oluwale Anthony, Raphael Okai, and Oluwatimilehin Folarin* \U0001F609")
st.markdown("[*Github Repo*](https://github.com/SilasPenda/Real-time-Gender-Detection)")
# -------------Image Upload Section------------------------------------------------
if detection_mode == "Image Upload":
# Example Images
col1, col2 = st.columns(2)
with col1:
st.image(image="./assets/example_2.png")
with col2:
st.image(image="./assets/example_3.png")
uploaded_file = st.file_uploader("Upload Image (Only PNG & JPG images allowed)", type=['png', 'jpg'])
if uploaded_file is not None:
with st.spinner("Detecting faces..."):
img = Image.open(uploaded_file)
# To convert PIL Image to numpy array:
img = np.array(img)
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Convert into grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray_img, 1.1, minNeighbors=minimum_neighbors, minSize=min_object_size)
if len(faces) == 0:
st.warning(
"No Face Detected in Image. Make sure your face is visible in the camera with proper lighting."
" Also try adjusting detection parameters")
else:
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=bbox_color, thickness=bbox_thickness)
# Do preprocessing based on model
face_crop = img[y:y + h, x:x + w]
face_crop = cv2.resize(face_crop, (224, 224))
face_crop = img_to_array(face_crop)
face_crop = face_crop / 255
face_crop = np.expand_dims(face_crop, axis = 0)
# Predict gender
prediction = model.predict(face_crop)[0]
# Get the max accuracy
idx = prediction.argmax(axis=-1)
# Get the label using the max accuracy
label_class = classes[idx]
# Create the format for label and confidence (%) to be displayed
label = '{}: {:2f}%'.format(label_class, prediction[idx] * 100)
# # Write label and confidence above the face rectangle
cv2.putText(img, label, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Display the output
st.image(img)
if len(faces) > 1:
st.success("Total of " + str(
len(faces)) + " faces detected inside the image. Try adjusting minimum object size if we missed anything")
# convert to pillow image
img = Image.fromarray(img)
buffered = BytesIO()
img.save(buffered, format="JPEG")
# Creating columns to center button
col1, col2, col3 = st.columns(3)
with col1:
pass
with col3:
pass
with col2:
st.download_button(
label="Download image",
data=buffered.getvalue(),
file_name="output.png",
mime="image/png")
else:
st.success(
"Only 1 face detected inside the image. Try adjusting minimum object size if we missed anything.")
# convert to pillow image
img = Image.fromarray(img)
buffered = BytesIO()
img.save(buffered, format="JPEG")
# Creating columns to center button
col1, col2, col3 = st.columns(3)
with col1:
pass
with col3:
pass
with col2:
st.download_button(
label="Download image",
data=buffered.getvalue(),
file_name="output.png",
mime="image/png")
# -------------Webcam Image Capture Section------------------------------------------------
if detection_mode == "Webcam Image Capture":
st.info("NOTE : In order to use this mode, you need to give webcam access.")
img_file_buffer = st.camera_input("Capture an Image from Webcam", disabled=False, key=1,
help="Make sure you have given webcam permission to the site")
if img_file_buffer is not None:
with st.spinner("Detecting faces ..."):
# To read image file buffer as a PIL Image:
img = Image.open(img_file_buffer)
# To convert PIL Image to numpy array:
img = np.array(img)
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, minNeighbors=minimum_neighbors, minSize=min_object_size)
if len(faces) == 0:
st.warning(
"No Face Detected in Image. Make sure your face is visible in the camera with proper lighting. "
"Also try adjusting detection parameters")
else:
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), color=bbox_color, thickness=bbox_thickness)
# Do preprocessing based on model
face_crop = img[y:y + h, x:x + w]
face_crop = cv2.resize(face_crop, (224, 224))
face_crop = img_to_array(face_crop)
face_crop = face_crop / 255
face_crop = np.expand_dims(face_crop, axis = 0)
# Predict gender
prediction = model.predict(face_crop)[0]
# Get the max accuracy
idx = prediction.argmax(axis=-1)
# Get the label using the max accuracy
label_class = classes[idx]
# Create the format for label and confidence (%) to be displayed
label = '{}: {:2f}%'.format(label_class, prediction[idx] * 100)
# # Write label and confidence above the face rectangle
cv2.putText(img, label, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Display the output
st.image(img)
if len(faces) > 1:
st.success("Total of " + str(
len(faces)) + " faces detected inside the image. Try adjusting minimum object size if we missed anything")
else:
st.success(
"Only 1 face detected inside the image. Try adjusting minimum object size if we missed anything")
# Download the image
img = Image.fromarray(img)
buffered = BytesIO()
img.save(buffered, format="JPEG")
# Creating columns to center button
col1, col2, col3 = st.columns(3)
with col1:
pass
with col3:
pass
with col2:
st.download_button(
label="Download image",
data=buffered.getvalue(),
file_name="output.png",
mime="image/png")
# -------------Webcam Real-time Section------------------------------------------------
if detection_mode == "Webcam Real-time":
# load face detection model
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
st.warning("NOTE : In order to use this mode, you need to give webcam access. "
"After clicking 'Start' , it takes about 10-20 seconds to ready the webcam.")
spinner_message = "Wait a sec, getting some things done..."
with st.spinner(spinner_message):
class VideoProcessor:
def recv(self, frame):
img = frame.to_ndarray(format = 'bgr24')
faces = face_cascade.detectMultiScale(image=img, scaleFactor=1.1, minNeighbors=minimum_neighbors, minSize=min_object_size)
for (x, y, w, h) in faces:
# Draw rectangle over face
cv2.rectangle(img = img, pt1 = (x, y), pt2 = (x + w, y + h), color = (0, 255, 0), thickness = 2)
# Do preprocessing based on model
face_crop = img[y:y + h, x:x + w]
face_crop = cv2.resize(face_crop, (224, 224))
face_crop = img_to_array(face_crop)
face_crop = face_crop / 255
face_crop = np.expand_dims(face_crop, axis = 0)
# Predict gender
prediction = model.predict(face_crop)[0]
# Get the max accuracy
idx = prediction.argmax(axis=-1)
# Get the label using the max accuracy
label_class = classes[idx]
# Create the format for label and confidence (%) to be displayed
label = '{}: {:2f}%'.format(label_class, prediction[idx] * 100)
# # Write label and confidence above the face rectangle
cv2.putText(img, label, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
return av.VideoFrame.from_ndarray(img, format = 'bgr24')
webrtc_streamer(key = 'example',
rtc_configuration = RTC_CONFIGURATION,
video_processor_factory = VideoProcessor,
media_stream_constraints = {
'video': True,
'audio': False
}
)
# -------------Hide Streamlit Watermark------------------------------------------------
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)