Spaces:
Sleeping
Sleeping
ok
Browse files
app.py
CHANGED
|
@@ -2,24 +2,26 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
|
|
|
| 5 |
|
| 6 |
# Load the trained model
|
| 7 |
-
model = tf.keras.models.load_model("face_mask_model.h5") #
|
| 8 |
|
| 9 |
# Class labels
|
| 10 |
labels = ["With Mask", "Without Mask"]
|
| 11 |
|
| 12 |
# Function to predict mask
|
| 13 |
def predict(img):
|
| 14 |
-
img = img.
|
|
|
|
| 15 |
img_array = image.img_to_array(img) / 255.0 # Normalize
|
| 16 |
img_array = np.expand_dims(img_array, axis=0)
|
| 17 |
-
|
| 18 |
prediction = model.predict(img_array)
|
| 19 |
return labels[np.argmax(prediction)] # Return class label
|
| 20 |
|
| 21 |
# Create Gradio interface
|
| 22 |
-
iface = gr.Interface(fn=predict, inputs="
|
| 23 |
|
| 24 |
# Launch the Gradio interface
|
| 25 |
iface.launch()
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
# Load the trained model
|
| 8 |
+
model = tf.keras.models.load_model("face_mask_model.h5") # Ensure this file exists
|
| 9 |
|
| 10 |
# Class labels
|
| 11 |
labels = ["With Mask", "Without Mask"]
|
| 12 |
|
| 13 |
# Function to predict mask
|
| 14 |
def predict(img):
|
| 15 |
+
img = img.convert("RGB") # Ensure RGB format
|
| 16 |
+
img = img.resize((224, 224)) # Resize for model input
|
| 17 |
img_array = image.img_to_array(img) / 255.0 # Normalize
|
| 18 |
img_array = np.expand_dims(img_array, axis=0)
|
| 19 |
+
|
| 20 |
prediction = model.predict(img_array)
|
| 21 |
return labels[np.argmax(prediction)] # Return class label
|
| 22 |
|
| 23 |
# Create Gradio interface
|
| 24 |
+
iface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text")
|
| 25 |
|
| 26 |
# Launch the Gradio interface
|
| 27 |
iface.launch()
|