Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,28 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
-
from tensorflow.keras.models import load_model
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
-
import
|
| 6 |
-
import cv2
|
| 7 |
-
|
| 8 |
-
# Load the model safely
|
| 9 |
-
model = load_model("mask_mobilenet.h5", compile=False)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
|
|
|
| 14 |
def predict_mask(img):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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(
|
| 32 |
-
outputs=
|
| 33 |
-
title="Mask Detection"
|
| 34 |
-
description="Upload an image or use webcam to detect face mask"
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
iface.launch()
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from tensorflow.keras.preprocessing import image
|
| 4 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load the SavedModel folder
|
| 7 |
+
model = tf.keras.models.load_model("mask_mobilenet_savedmodel")
|
| 8 |
|
| 9 |
+
# Function to predict mask or no mask
|
| 10 |
def predict_mask(img):
|
| 11 |
+
img = img.resize((224, 224))
|
| 12 |
+
x = np.array(img) / 255.0
|
| 13 |
+
x = np.expand_dims(x, axis=0)
|
| 14 |
+
pred = model.predict(x)[0]
|
| 15 |
+
if pred[0] > pred[1]:
|
| 16 |
+
return "Mask"
|
| 17 |
+
else:
|
| 18 |
+
return "No Mask"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Gradio interface
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=predict_mask,
|
| 23 |
+
inputs=gr.Image(type="pil"),
|
| 24 |
+
outputs="text",
|
| 25 |
+
title="Mask Detection"
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
+
iface.launch()
|
|
|