mahesh1209 commited on
Commit
c7e63a3
·
verified ·
1 Parent(s): 08e9ed7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ import gradio as gr
5
+ from ultralytics import YOLO
6
+
7
+ # Load model: fallback to YOLOv8n if custom weights are missing
8
+ model_path = "best.pt"
9
+ model = YOLO(model_path) if os.path.exists(model_path) else YOLO("yolov8n.pt")
10
+
11
+ def detect_mask(img, conf_thresh, iou_thresh):
12
+ try:
13
+ img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
14
+ results = model.predict(img_bgr, conf=conf_thresh, iou=iou_thresh)[0]
15
+ annotated = results.plot()
16
+ return cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
17
+ except Exception as e:
18
+ print(f"Error: {e}")
19
+ return np.zeros_like(img)
20
+
21
+ demo = gr.Interface(
22
+ fn=detect_mask,
23
+ inputs=[
24
+ gr.Image(type="numpy", label="📷 Upload Image"),
25
+ gr.Slider(0.3, 1.0, value=0.5, label="Confidence Threshold"),
26
+ gr.Slider(0.3, 1.0, value=0.5, label="IoU Threshold")
27
+ ],
28
+ outputs=gr.Image(label="🧠 Detection Result"),
29
+ title="🎯 Face Mask Detection (YOLOv8)",
30
+ description="Detects 'Mask' and 'No Mask' using YOLOv8. Upload an image and adjust thresholds. Automatically falls back to YOLOv8n if custom weights are missing."
31
+ )
32
+
33
+ if __name__ == "__main__":
34
+ demo.launch()