Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,42 @@
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 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="
|
| 24 |
-
outputs="
|
| 25 |
-
title="Mask Detection"
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
# Load the SavedModel
|
| 7 |
+
model = tf.keras.models.load_model("mask_mobilenet_savedmodel") # Folder path
|
| 8 |
+
|
| 9 |
+
# Prediction function
|
| 10 |
+
def predict_mask(image):
|
| 11 |
+
try:
|
| 12 |
+
# Convert to RGB if needed
|
| 13 |
+
if image.shape[2] == 4: # RGBA
|
| 14 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 15 |
+
|
| 16 |
+
# Resize to model input
|
| 17 |
+
image = cv2.resize(image, (224, 224))
|
| 18 |
+
image = image / 255.0 # Normalize
|
| 19 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 20 |
+
|
| 21 |
+
# Predict
|
| 22 |
+
preds = model.predict(image)
|
| 23 |
+
print("Preds:", preds) # Logs in console
|
| 24 |
|
| 25 |
+
# Interpret prediction
|
| 26 |
+
result = "Mask" if preds[0][0] > 0.5 else "No Mask"
|
| 27 |
+
return result
|
| 28 |
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print("Error:", e) # Logs in console
|
| 31 |
+
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Gradio interface
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=predict_mask,
|
| 36 |
+
inputs=gr.Image(type="numpy"),
|
| 37 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 38 |
+
title="Mask Detection",
|
| 39 |
+
description="Upload an image to check if a person is wearing a mask or not."
|
| 40 |
)
|
| 41 |
|
| 42 |
iface.launch()
|