Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- .gitattributes +1 -0
- app.py +72 -0
- face-mask.png +0 -0
- haarcascade_frontalface_default.xml +0 -0
- mask_detector.keras +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
mask_detector.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
import streamlit as st
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
st.image(r"face-mask.png", width = 150)
|
10 |
+
st.write("# Mask detector App")
|
11 |
+
|
12 |
+
# load model
|
13 |
+
@st.cache_resource
|
14 |
+
def cache_model(model_add):
|
15 |
+
model = tf.keras.models.load_model(model_add)
|
16 |
+
return model
|
17 |
+
|
18 |
+
model = cache_model("mask_detector.keras")
|
19 |
+
|
20 |
+
labels = ['WithMask', 'WithoutMask']
|
21 |
+
def predict(img,model,labels = labels):
|
22 |
+
# read_image
|
23 |
+
# img = cv2.imread(img_add)
|
24 |
+
# resize image to 224, 224
|
25 |
+
img = cv2.resize(img, (224,224))
|
26 |
+
# st.write("resize_cv2", img.shape)
|
27 |
+
# convert to RGB
|
28 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
29 |
+
img_expand = tf.expand_dims(img_rgb, axis = 0)
|
30 |
+
y_pred_prob = model.predict(img_expand)
|
31 |
+
y_pred_prob_round = np.round(y_pred_prob[0][0], 3)
|
32 |
+
st.write(y_pred_prob[0][0])
|
33 |
+
y_pred = int(np.round(y_pred_prob_round))
|
34 |
+
label = labels[y_pred]
|
35 |
+
return (label, y_pred_prob_round)
|
36 |
+
|
37 |
+
def face_detect(img, model, labels = labels):
|
38 |
+
# img = cv2.imread(img_add)
|
39 |
+
face_detector=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
|
40 |
+
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
41 |
+
faces = face_detector.detectMultiScale(img_gray)
|
42 |
+
if len(faces) > 0:
|
43 |
+
for face in faces:
|
44 |
+
x,y,w,h = face
|
45 |
+
x_max = x + w
|
46 |
+
y_max = y + h
|
47 |
+
face_crp = img[y:y_max+1, x:x_max+1]
|
48 |
+
label, prob = predict(face_crp,model,labels = labels)
|
49 |
+
if label == labels[0]:
|
50 |
+
cv2.rectangle(img, (x,y), (x_max, y_max), (0,255,0), 3)
|
51 |
+
cv2.putText(img, f"{label} -: {1 - prob}%", (x,y-10), cv2.FONT_HERSHEY_SIMPLEX,
|
52 |
+
1, (0,255,0), 2)
|
53 |
+
else:
|
54 |
+
cv2.rectangle(img, (x,y), (x_max, y_max), (255,0,0), 3)
|
55 |
+
cv2.putText(img, f"{label} -: {prob}%", (x,y-10), cv2.FONT_HERSHEY_SIMPLEX,
|
56 |
+
1, (255,0,0), 2)
|
57 |
+
return img
|
58 |
+
|
59 |
+
|
60 |
+
# File uploader
|
61 |
+
uploaded_file = st.file_uploader("upload image", type=["jpg", "jpeg", "png"])
|
62 |
+
submit_btn = st.button("Submit")
|
63 |
+
|
64 |
+
if submit_btn:
|
65 |
+
if uploaded_file:
|
66 |
+
img_array = np.array(Image.open(uploaded_file))
|
67 |
+
# st.write(img_array.shape)
|
68 |
+
result_img = face_detect(img_array, model)
|
69 |
+
# result_img_rgb = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
70 |
+
st.image(result_img)
|
71 |
+
else:
|
72 |
+
st.write("Please upload an image")
|
face-mask.png
ADDED
![]() |
haarcascade_frontalface_default.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
mask_detector.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be03cad9db624364f78da80d545ca3fdaba751c25d6fe5113f3f2a9ecd5fa50f
|
3 |
+
size 23988034
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit == 1.36.0
|
2 |
+
numpy == 1.26.4
|
3 |
+
opencv-python == 4.10.0.84
|
4 |
+
tensorflow == 2.16.1
|