Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,10 +28,17 @@ elif os.path.exists("yolov11nbest.pt"):
|
|
| 28 |
else:
|
| 29 |
raise FileNotFoundError("No model file found. Please ensure 'best.pt' or 'yolov11nbest.pt' exists.")
|
| 30 |
|
| 31 |
-
# Define the prediction function
|
| 32 |
-
def predict(image):
|
|
|
|
|
|
|
|
|
|
| 33 |
results = model(image) # Run YOLO model on the uploaded image
|
|
|
|
|
|
|
| 34 |
results_img = results[0].plot() # Get image with bounding boxes
|
|
|
|
|
|
|
| 35 |
return Image.fromarray(results_img)
|
| 36 |
|
| 37 |
# Get example images from the images folder
|
|
@@ -44,14 +51,56 @@ def get_example_images():
|
|
| 44 |
examples.append(os.path.join(image_folder, filename))
|
| 45 |
return examples
|
| 46 |
|
| 47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
interface = gr.Interface(
|
| 49 |
fn=predict,
|
| 50 |
-
inputs=gr.Image(type="pil"),
|
| 51 |
-
outputs=gr.Image(type="pil"),
|
| 52 |
-
title=
|
| 53 |
-
description=f"
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
# Launch the interface
|
|
|
|
| 28 |
else:
|
| 29 |
raise FileNotFoundError("No model file found. Please ensure 'best.pt' or 'yolov11nbest.pt' exists.")
|
| 30 |
|
| 31 |
+
# Define the prediction function with progress updates
|
| 32 |
+
def predict(image, progress=gr.Progress()):
|
| 33 |
+
progress(0, desc="Starting detection...")
|
| 34 |
+
|
| 35 |
+
progress(0.3, desc="Running AI model (this may take 20-40 seconds on CPU)...")
|
| 36 |
results = model(image) # Run YOLO model on the uploaded image
|
| 37 |
+
|
| 38 |
+
progress(0.8, desc="Drawing bounding boxes...")
|
| 39 |
results_img = results[0].plot() # Get image with bounding boxes
|
| 40 |
+
|
| 41 |
+
progress(1.0, desc="Done!")
|
| 42 |
return Image.fromarray(results_img)
|
| 43 |
|
| 44 |
# Get example images from the images folder
|
|
|
|
| 51 |
examples.append(os.path.join(image_folder, filename))
|
| 52 |
return examples
|
| 53 |
|
| 54 |
+
# Custom CSS for better visual appeal
|
| 55 |
+
custom_css = """
|
| 56 |
+
.progress-bar-wrap {
|
| 57 |
+
border-radius: 8px !important;
|
| 58 |
+
}
|
| 59 |
+
.progress-bar {
|
| 60 |
+
background: linear-gradient(90deg, #4CAF50, #2196F3) !important;
|
| 61 |
+
}
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
# Create Gradio interface with better UX
|
| 65 |
interface = gr.Interface(
|
| 66 |
fn=predict,
|
| 67 |
+
inputs=gr.Image(type="pil", label="📤 Upload Image"),
|
| 68 |
+
outputs=gr.Image(type="pil", label="🎯 Detection Result"),
|
| 69 |
+
title="🪖 Helmet Detection with YOLO",
|
| 70 |
+
description=f"""
|
| 71 |
+
**Currently using: {model_name}**
|
| 72 |
+
|
| 73 |
+
Upload an image to detect helmets and safety equipment. The AI will identify:
|
| 74 |
+
- ✅ People wearing helmets (accept-Helmet-)
|
| 75 |
+
- ❌ People not wearing helmets (non-Helmet-)
|
| 76 |
+
|
| 77 |
+
⏱️ **Please be patient**: Detection takes 20-40 seconds on CPU. Watch the progress bar below!
|
| 78 |
+
""",
|
| 79 |
+
article="""
|
| 80 |
+
### 📊 About This Model
|
| 81 |
+
This app uses YOLOv8/YOLOv11 for real-time helmet detection in construction and workplace safety scenarios.
|
| 82 |
+
|
| 83 |
+
**Tips for best results:**
|
| 84 |
+
- Use clear, well-lit images
|
| 85 |
+
- Ensure people are visible in the frame
|
| 86 |
+
- Works best with construction site or workplace photos
|
| 87 |
+
|
| 88 |
+
**Performance:** Running on CPU (free tier), so inference takes ~30 seconds per image.
|
| 89 |
+
|
| 90 |
+
### ⭐ Advanced Version Available!
|
| 91 |
+
Check out the **[Helmet + License Plate Detection](https://huggingface.co/spaces/Abs6187/Helmet-License-Plate-Detection)** -
|
| 92 |
+
an upgraded version that detects BOTH helmets AND license plates!
|
| 93 |
+
|
| 94 |
+
### 🚀 While you wait...
|
| 95 |
+
- Try the example images below
|
| 96 |
+
- Read about YOLO object detection: [Ultralytics Docs](https://docs.ultralytics.com)
|
| 97 |
+
- Star the repo if you find this useful!
|
| 98 |
+
""",
|
| 99 |
+
examples=get_example_images(),
|
| 100 |
+
cache_examples=False, # Disable caching to speed up startup and reduce storage
|
| 101 |
+
allow_flagging="never",
|
| 102 |
+
theme=gr.themes.Soft(),
|
| 103 |
+
css=custom_css
|
| 104 |
)
|
| 105 |
|
| 106 |
# Launch the interface
|