File size: 16,313 Bytes
0526d20
1b323a6
 
 
 
 
88ed57b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0526d20
1b323a6
 
 
 
 
88ed57b
 
 
cb63f36
88ed57b
cb63f36
 
 
 
 
88ed57b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb63f36
88ed57b
cb63f36
88ed57b
 
 
 
 
 
cb63f36
88ed57b
 
 
 
 
 
 
cb63f36
88ed57b
 
cb63f36
88ed57b
 
cb63f36
88ed57b
cb63f36
88ed57b
 
cb63f36
1b323a6
 
 
 
 
 
 
88ed57b
 
 
 
1b323a6
88ed57b
 
 
 
 
1b323a6
88ed57b
 
 
1b323a6
88ed57b
 
 
1b323a6
88ed57b
 
 
 
1b323a6
88ed57b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b323a6
88ed57b
 
1b323a6
88ed57b
 
1b323a6
88ed57b
 
 
 
1b323a6
88ed57b
 
 
 
 
 
1b323a6
 
88ed57b
1b323a6
 
 
 
 
88ed57b
 
 
 
 
1b323a6
88ed57b
 
 
 
1b323a6
88ed57b
 
 
 
 
 
1b323a6
88ed57b
 
 
1b323a6
 
 
 
 
 
 
88ed57b
1b323a6
 
 
 
 
 
 
 
 
 
 
 
88ed57b
1b323a6
88ed57b
 
 
 
 
 
 
1b323a6
88ed57b
1b323a6
88ed57b
 
 
 
 
 
1b323a6
88ed57b
 
 
 
 
 
 
1b323a6
88ed57b
 
1b323a6
88ed57b
 
1b323a6
88ed57b
1b323a6
88ed57b
 
1b323a6
 
88ed57b
1b323a6
 
cb63f36
 
88ed57b
cb63f36
 
 
 
 
 
88ed57b
 
cb63f36
 
1b323a6
cb63f36
 
1b323a6
 
 
 
 
88ed57b
 
1b323a6
 
 
 
cb63f36
1b323a6
 
 
 
 
88ed57b
 
1b323a6
 
 
88ed57b
1b323a6
88ed57b
 
 
1b323a6
 
 
88ed57b
 
cb63f36
 
 
 
 
 
88ed57b
cb63f36
 
 
 
 
 
88ed57b
 
 
 
 
 
 
 
1b323a6
 
88ed57b
1b323a6
 
88ed57b
1b323a6
 
88ed57b
 
 
 
 
 
cb63f36
 
88ed57b
1b323a6
88ed57b
1b323a6
 
 
88ed57b
1b323a6
 
cb63f36
88ed57b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b323a6
 
 
cb63f36
 
1b323a6
 
 
 
 
 
 
 
88ed57b
 
1b323a6
 
88ed57b
1b323a6
 
 
 
 
 
 
88ed57b
1b323a6
88ed57b
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import gradio as gr
import cv2
import numpy as np
from ultralytics import YOLO
import threading
import time
import os
import tempfile
from pathlib import Path

# Initialize model with error handling
try:
    # Check if model file exists
    if not os.path.exists("best.pt"):
        # Fallback to a default YOLO model if custom model not available
        print("Warning: best.pt not found. Using default YOLOv8n model.")
        model = YOLO("yolov8n.pt")  # This will auto-download
        CLASS_NAMES = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
                       "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
                       "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
                       "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
                       "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
                       "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
                       "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake",
                       "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop",
                       "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
                       "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"]
    else:
        model = YOLO("best.pt")
        CLASS_NAMES = ["hard hat", "mask"]
except Exception as e:
    print(f"Error loading model: {e}")
    model = YOLO("yolov8n.pt")
    CLASS_NAMES = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
                   "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
                   "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
                   "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
                   "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
                   "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
                   "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake",
                   "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop",
                   "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
                   "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"]

# Global variables for camera streaming
camera_active = False
current_frame = None
frame_lock = threading.Lock()

# Create temp directory for outputs
temp_dir = tempfile.mkdtemp()

# -------------------
# Image Processing
# -------------------
def predict_image(input_image, selected_classes):
    if input_image is None:
        return None, "No image uploaded"

    try:
        # Handle different input types
        if isinstance(input_image, str):  # File path
            frame = cv2.imread(input_image)
            if frame is None:
                return None, "Could not load image file"
        elif isinstance(input_image, np.ndarray):
            frame = input_image
        else:
            frame = np.array(input_image)

        # Convert RGB to BGR if needed (OpenCV uses BGR)
        if len(frame.shape) == 3 and frame.shape[2] == 3:
            # Check if it's RGB (Gradio format) and convert to BGR
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        # Run YOLO detection
        results = model.predict(frame, conf=0.25, verbose=False)
        frame_out = frame.copy()

        detection_count = {cls: 0 for cls in selected_classes}

        for r in results:
            if r.boxes is not None:
                for box in r.boxes:
                    cls_id = int(box.cls[0])
                    conf = float(box.conf[0])
                    label = CLASS_NAMES[cls_id] if cls_id < len(CLASS_NAMES) else f"cls{cls_id}"

                    if label in selected_classes:
                        x1, y1, x2, y2 = map(int, box.xyxy[0])
                        cv2.rectangle(frame_out, (x1, y1), (x2, y2), (0, 255, 0), 2)
                        cv2.putText(frame_out, f"{label} {conf:.2f}",
                                    (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
                                    (0, 255, 0), 2)
                        detection_count[label] += 1

        # Convert BGR back to RGB for display
        frame_rgb = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)

        # Create tally text
        tally_text = "\n".join([f"{cls}: {count} detections" for cls, count in detection_count.items()])

        return frame_rgb, tally_text

    except Exception as e:
        return None, f"Error processing image: {str(e)}"

# -------------------
# Video File Processing
# -------------------
def predict_video(input_file, selected_classes):
    if input_file is None:
        return None, "No file uploaded"

    try:
        cap = cv2.VideoCapture(input_file)
        if not cap.isOpened():
            return None, "Could not read input file"

        # Video writer setup
        fourcc = cv2.VideoWriter_fourcc(*"mp4v")
        fps = max(cap.get(cv2.CAP_PROP_FPS), 20)  # Ensure minimum FPS
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

        # Use temp directory for output
        out_path = os.path.join(temp_dir, "output.mp4")
        out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))

        tally_counts = {cls: 0 for cls in selected_classes}
        frame_count = 0
        max_frames = 1000  # Limit processing to prevent timeout

        while frame_count < max_frames:
            ret, frame = cap.read()
            if not ret:
                break

            results = model.predict(frame, conf=0.25, verbose=False)
            frame_out = frame.copy()

            for r in results:
                if r.boxes is not None:
                    for box in r.boxes:
                        cls_id = int(box.cls[0])
                        conf = float(box.conf[0])
                        label = CLASS_NAMES[cls_id] if cls_id < len(CLASS_NAMES) else f"cls{cls_id}"

                        if label in selected_classes:
                            x1, y1, x2, y2 = map(int, box.xyxy[0])
                            cv2.rectangle(frame_out, (x1, y1), (x2, y2), (0, 255, 0), 2)
                            cv2.putText(frame_out, f"{label} {conf:.2f}",
                                        (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
                                        (0, 255, 0), 2)
                            tally_counts[label] += 1

            out.write(frame_out)
            frame_count += 1

        cap.release()
        out.release()

        if frame_count >= max_frames:
            tally_text = f"Processed first {max_frames} frames.\n"
        else:
            tally_text = ""

        tally_text += "\n".join([f"{cls}: {count} detections" for cls, count in tally_counts.items()])

        return out_path, tally_text

    except Exception as e:
        return None, f"Error processing video: {str(e)}"

# -------------------
# Live Camera Functions (Note: Limited on HF Spaces)
# -------------------
def camera_thread():
    """Background thread to capture camera frames"""
    global camera_active, current_frame

    try:
        cap = cv2.VideoCapture(0)
        if not cap.isOpened():
            print("Warning: Could not open camera - this is expected on Hugging Face Spaces")
            return

        # Set camera properties for better performance
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
        cap.set(cv2.CAP_PROP_FPS, 30)

        while camera_active:
            ret, frame = cap.read()
            if ret:
                with frame_lock:
                    current_frame = frame.copy()
            time.sleep(0.033)  # ~30 FPS

        cap.release()
    except Exception as e:
        print(f"Camera error: {e}")

def start_camera():
    """Start the camera streaming"""
    global camera_active
    camera_active = True
    camera_thread_obj = threading.Thread(target=camera_thread, daemon=True)
    camera_thread_obj.start()
    return "Camera started (Note: Camera access may be limited on Hugging Face Spaces)"

def stop_camera():
    """Stop the camera streaming"""
    global camera_active
    camera_active = False
    return "Camera stopped"

def get_camera_frame(selected_classes):
    """Get current camera frame with detections"""
    global current_frame

    if not camera_active or current_frame is None:
        return None, "Camera not available or no frame captured"

    try:
        with frame_lock:
            frame = current_frame.copy()

        # Run YOLO detection
        results = model.predict(frame, conf=0.25, verbose=False)
        frame_out = frame.copy()

        detection_count = {cls: 0 for cls in selected_classes}

        for r in results:
            if r.boxes is not None:
                for box in r.boxes:
                    cls_id = int(box.cls[0])
                    conf = float(box.conf[0])
                    label = CLASS_NAMES[cls_id] if cls_id < len(CLASS_NAMES) else f"cls{cls_id}"

                    if label in selected_classes:
                        x1, y1, x2, y2 = map(int, box.xyxy[0])
                        cv2.rectangle(frame_out, (x1, y1), (x2, y2), (0, 255, 0), 2)
                        cv2.putText(frame_out, f"{label} {conf:.2f}",
                                    (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
                                    (0, 255, 0), 2)
                        detection_count[label] += 1

        # Convert BGR to RGB for Gradio
        frame_rgb = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)

        # Create tally text
        tally_text = "\n".join([f"{cls}: {count} detections" for cls, count in detection_count.items()])

        return frame_rgb, tally_text

    except Exception as e:
        return None, f"Error processing camera frame: {str(e)}"

# -------------------
# Dynamic UI Update
# -------------------
def update_ui(mode):
    if mode == "Upload Image":
        return (
            gr.update(visible=True),   # input_file
            gr.update(visible=False),  # input_video
            gr.update(visible=False),  # output_video
            gr.update(visible=True),   # output_img
            gr.update(visible=True),   # run_btn
            gr.update(visible=False),  # start_btn
            gr.update(visible=False),  # stop_btn
            gr.update(visible=False),  # refresh_btn
            gr.update(visible=False)   # camera_warning
        )
    elif mode == "Upload Video":
        return (
            gr.update(visible=False),  # input_file
            gr.update(visible=True),   # input_video
            gr.update(visible=True),   # output_video
            gr.update(visible=False),  # output_img
            gr.update(visible=True),   # run_btn
            gr.update(visible=False),  # start_btn
            gr.update(visible=False),  # stop_btn
            gr.update(visible=False),  # refresh_btn
            gr.update(visible=False)   # camera_warning
        )
    else:  # Live Camera
        return (
            gr.update(visible=False),  # input_file
            gr.update(visible=False),  # input_video
            gr.update(visible=False),  # output_video
            gr.update(visible=True),   # output_img
            gr.update(visible=False),  # run_btn
            gr.update(visible=True),   # start_btn
            gr.update(visible=True),   # stop_btn
            gr.update(visible=True),   # refresh_btn
            gr.update(visible=True)    # camera_warning
        )

# -------------------
# Gradio Interface
# -------------------
with gr.Blocks(title="YOLO Detector") as demo:
    gr.Markdown("## 🦺 YOLO Object Detector")
    gr.Markdown("Upload images or videos for object detection. Note: Live camera may not work on Hugging Face Spaces due to browser security restrictions.")

    with gr.Row():
        with gr.Column():
            mode = gr.Radio(["Upload Image", "Upload Video", "Live Camera"], value="Upload Image", label="Detection Mode")

            input_file = gr.File(
                label="Upload Image",
                type="filepath",
                file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".webp"],
                visible=True
            )

            input_video = gr.File(
                label="Upload Video",
                type="filepath",
                file_types=[".mp4", ".avi", ".mov", ".mkv", ".wmv", ".flv", ".webm"],
                visible=False
            )

            # Dynamic class selection based on loaded model
            available_classes = CLASS_NAMES[:10] if len(CLASS_NAMES) > 10 else CLASS_NAMES
            class_toggle = gr.CheckboxGroup(
                available_classes,
                value=available_classes[:2],
                label="Select classes to detect"
            )

            # Buttons
            run_btn = gr.Button("Run Detection", variant="primary", visible=True)
            start_btn = gr.Button("Start Camera", visible=False)
            stop_btn = gr.Button("Stop Camera", visible=False)
            refresh_btn = gr.Button("Refresh Live Feed", visible=False)

        with gr.Column():
            # Camera warning
            camera_warning = gr.Markdown(
                "⚠️ **Note:** Live camera access is typically not available on Hugging Face Spaces due to security restrictions. Use image/video upload instead.",
                visible=False
            )

            output_video = gr.Video(label="Detection Output", visible=False)
            output_img = gr.Image(type="numpy", label="Detection Output", visible=True)
            tally_box = gr.Textbox(label="Detection Count", interactive=False, lines=5)

    # Event handlers
    mode.change(
        update_ui,
        inputs=mode,
        outputs=[input_file, input_video, output_video, output_img, run_btn, start_btn, stop_btn, refresh_btn, camera_warning]
    )

    def run_detection(input_file, input_video, selected_classes, mode):
        if not selected_classes:
            return None, None, "Please select at least one class to detect"

        try:
            if mode == "Upload Image":
                if input_file is None:
                    return None, None, "No image uploaded"
                result_img, tally = predict_image(input_file, selected_classes)
                return result_img, None, tally
            elif mode == "Upload Video":
                if input_video is None:
                    return None, None, "No video uploaded"
                result_video, tally = predict_video(input_video, selected_classes)
                return None, result_video, tally
            return None, None, "Invalid mode selected"
        except Exception as e:
            return None, None, f"Error: {str(e)}"

    run_btn.click(
        run_detection,
        inputs=[input_file, input_video, class_toggle, mode],
        outputs=[output_img, output_video, tally_box]
    )

    # Camera controls
    start_btn.click(start_camera, outputs=tally_box)
    stop_btn.click(stop_camera, outputs=tally_box)

    # Live camera feed update
    def update_live_feed(selected_classes):
        if not selected_classes:
            return None, "Please select at least one class to detect"
        if camera_active:
            return get_camera_frame(selected_classes)
        return None, "Camera not active"

    refresh_btn.click(
        update_live_feed,
        inputs=[class_toggle],
        outputs=[output_img, tally_box]
    )

# Launch configuration for Hugging Face
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",  # Required for HF Spaces
        server_port=7860,       # Standard port for HF Spaces
        share=False             # Set to True only if needed
    )