awais0300 commited on
Commit
3402a2e
·
verified ·
1 Parent(s): a517d5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
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.applications.mobilenet_v2 import preprocess_input
7
-
8
- # Load model
9
- model = load_model("mask_mobilenet_fixed.h5", compile=False)
10
 
11
- IMG_SIZE = 224
 
12
 
13
- def predict_mask(image):
14
- img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
15
- img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
16
- img = preprocess_input(img)
17
- img = np.expand_dims(img, axis=0)
18
 
19
- prob = model.predict(img, verbose=0)[0][0]
 
 
 
 
 
20
 
21
- return {
22
- "Mask 😷": float(1 - prob),
23
- "No Mask ❌": float(prob)
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="Face Mask Detection",
31
- description="Upload an image to check whether a person is wearing a mask."
32
  )
33
 
34
- iface.launch()
 
 
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()