awais0300 commited on
Commit
35de7c8
·
verified ·
1 Parent(s): aa84f28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -1,38 +1,28 @@
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()
 
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()