File size: 5,128 Bytes
8ffc27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Gradio Web Interface for Object Detection
"""

import gradio as gr
import cv2
import numpy as np
import tempfile
import os
from detector import ObjectDetector
from PIL import Image

# Initialize detector
print("πŸ” Initializing Object Detector...")
detector = ObjectDetector()
print("βœ… Detector ready!")

def process_image(image):
    """
    Process uploaded image and return annotated image with stats
    """
    if image is None:
        return None, "Please upload an image."
    
    # Convert to BGR (OpenCV format)
    if isinstance(image, np.ndarray):
        frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    else:
        frame = np.array(image)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    
    # Detect objects
    detections = detector.detect_frame(frame)
    
    # Draw annotations
    annotated = detector.draw_detections(frame, detections)
    
    # Generate statistics
    stats = detector.get_stats(detections)
    stats_text = "πŸ“Š Detection Summary:\n" + "\n".join([
        f"β€’ {cls}: {count}" for cls, count in stats.items()
    ])
    
    if not stats:
        stats_text = "πŸ” No objects detected."
    
    # Convert back to RGB for Gradio
    annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
    
    return annotated_rgb, stats_text

def process_video(video):
    """
    Process uploaded video and return annotated video
    """
    if video is None:
        return None
    
    # Create temporary output
    temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
    output_path = temp_output.name
    temp_output.close()
    
    # Open video
    cap = cv2.VideoCapture(video)
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    # Video writer
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
    
    # Process each frame
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    processed = 0
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        # Detect and annotate
        detections = detector.detect_frame(frame)
        annotated = detector.draw_detections(frame, detections)
        
        # Write frame
        out.write(annotated)
        processed += 1
        
        # Progress indicator (every 30 frames)
        if processed % 30 == 0:
            print(f"Processing: {processed}/{total_frames} frames")
    
    # Cleanup
    cap.release()
    out.release()
    
    print(f"βœ… Video processed: {processed} frames")
    return output_path

# Create Gradio Interface
with gr.Blocks(
    title="Safety Equipment Detection",
    theme=gr.themes.Soft()
) as demo:
    
    gr.Markdown("""
    # 🦺 Safety Equipment Detection System
    
    Upload an **image** or **video** to detect:
    - πŸͺ– **Helmets** (Green)
    - 🚫 **No-Helmet** (Red) - Safety violation!
    - 🦺 **Safety Vests** (Yellow)
    - πŸ‘· **People** (Blue)
    
    *Powered by YOLO11 Object Detection*
    """)
    
    with gr.Tab("πŸ“Έ Image Detection"):
        with gr.Row():
            with gr.Column():
                image_input = gr.Image(
                    label="Upload Image",
                    type="pil"
                )
                image_button = gr.Button("πŸ” Detect Objects", variant="primary")
            
            with gr.Column():
                image_output = gr.Image(
                    label="Detected Objects",
                    type="numpy"
                )
                stats_output = gr.Textbox(
                    label="Detection Summary",
                    lines=8,
                    interactive=False
                )
        
        image_button.click(
            process_image,
            inputs=[image_input],
            outputs=[image_output, stats_output]
        )
    
    with gr.Tab("πŸŽ₯ Video Detection"):
        with gr.Row():
            video_input = gr.Video(
                label="Upload Video"
            )
            video_output = gr.Video(
                label="Processed Video"
            )
        
        video_button = gr.Button("🎬 Process Video", variant="primary")
        video_button.click(
            process_video,
            inputs=[video_input],
            outputs=[video_output]
        )
    
    with gr.Tab("ℹ️ About"):
        gr.Markdown("""
        ### About This Project
        
        **Object Detection System** using YOLO (You Only Look Once)
        
        **Features:**
        - Real-time object detection
        - Safety equipment monitoring
        - Support for images and videos
        - Visual bounding boxes with labels
        
        **Classes:** Helmet, No-Helmet, Vest, Person
        
        **Tech Stack:**
        - Ultralytics YOLO11
        - OpenCV
        - Gradio
        - Python
        """)

if __name__ == "__main__":
    # Launch the app
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        debug=False
    )