krishnamishra8848 commited on
Commit
63f3815
·
verified ·
1 Parent(s): 3e8533c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from huggingface_hub import hf_hub_download
3
+ from ultralytics import YOLO
4
+ import cv2
5
+ import numpy as np
6
+ from PIL import Image
7
+
8
+ # Define repository and file path
9
+ repo_id = "krishnamishra8848/Face_Mask_Detection"
10
+ filename = "best.pt" # File name in your Hugging Face repo
11
+
12
+ # Download the model file
13
+ model_path = hf_hub_download(repo_id=repo_id, filename=filename)
14
+
15
+ # Load the YOLO model
16
+ model = YOLO(model_path)
17
+
18
+ # Streamlit UI
19
+ st.title("Face Mask Detection with YOLOv8")
20
+ st.write("Upload an image to detect face masks.")
21
+
22
+ # File upload
23
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
24
+
25
+ if uploaded_file:
26
+ # Load image
27
+ image = Image.open(uploaded_file)
28
+ image_np = np.array(image)
29
+
30
+ # Run inference
31
+ st.write("Running inference...")
32
+ results = model.predict(source=image_np, conf=0.5)
33
+
34
+ # Annotate image
35
+ annotated_image = None
36
+ for result in results:
37
+ annotated_image = result.plot()
38
+
39
+ # Convert annotated image for Streamlit
40
+ if annotated_image is not None:
41
+ annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
42
+ st.image(annotated_image_rgb, caption="Prediction Results", use_column_width=True)
43
+
44
+ # Display bounding boxes and confidence scores
45
+ st.write("Detection Results:")
46
+ for result in results:
47
+ for box in result.boxes.data.cpu().numpy():
48
+ x1, y1, x2, y2, conf, cls = box
49
+ st.write(f"Class: {int(cls)}, Confidence: {conf:.2f}, Box: [{x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f}]")