Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import tensorflow as tf
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
-
from tensorflow.keras.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
model = load_model("mask_mobilenet_fixed.h5", compile=False)
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
|
| 16 |
-
img = preprocess_input(img)
|
| 17 |
-
img = np.expand_dims(img, axis=0)
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=predict_mask,
|
| 28 |
-
inputs=gr.Image(type="numpy"),
|
| 29 |
outputs=gr.Label(num_top_classes=2),
|
| 30 |
-
title="
|
| 31 |
-
description="Upload an image
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import cv2
|
|
|
|
| 7 |
|
| 8 |
+
# Load the model safely
|
| 9 |
+
model = load_model("mask_mobilenet.h5", compile=False)
|
| 10 |
|
| 11 |
+
# Classes
|
| 12 |
+
classes = ["Mask", "No Mask"]
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def predict_mask(img):
|
| 15 |
+
# Resize and preprocess the input image
|
| 16 |
+
img_resized = cv2.resize(img, (224, 224))
|
| 17 |
+
img_array = image.img_to_array(img_resized)
|
| 18 |
+
img_array = np.expand_dims(img_array, axis=0) # shape (1, 224, 224, 3)
|
| 19 |
+
img_array = img_array / 255.0 # normalize
|
| 20 |
|
| 21 |
+
# Make prediction
|
| 22 |
+
preds = model.predict(img_array)
|
| 23 |
+
class_idx = np.argmax(preds, axis=1)[0]
|
| 24 |
+
confidence = float(np.max(preds))
|
| 25 |
+
|
| 26 |
+
return {classes[class_idx]: confidence}
|
| 27 |
|
| 28 |
+
# Gradio interface
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=predict_mask,
|
| 31 |
+
inputs=gr.Image(source="webcam", type="numpy"),
|
| 32 |
outputs=gr.Label(num_top_classes=2),
|
| 33 |
+
title="Mask Detection",
|
| 34 |
+
description="Upload an image or use webcam to detect face mask"
|
| 35 |
)
|
| 36 |
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
iface.launch()
|