Spaces:
No application file
No application file
Rename app (1).py to app .py
Browse files- app (1).py +0 -30
- app .py +32 -0
app (1).py
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
from ultralytics import YOLO
|
| 2 |
-
import cv2
|
| 3 |
-
|
| 4 |
-
# Load the YOLO model (YOLOv8 in this case)
|
| 5 |
-
model = YOLO('yolov8n.pt')
|
| 6 |
-
|
| 7 |
-
# Open webcam
|
| 8 |
-
cap = cv2.VideoCapture(0)
|
| 9 |
-
|
| 10 |
-
while True:
|
| 11 |
-
ret, frame = cap.read()
|
| 12 |
-
if not ret:
|
| 13 |
-
break
|
| 14 |
-
|
| 15 |
-
# Make prediction
|
| 16 |
-
results = model(frame)
|
| 17 |
-
|
| 18 |
-
# results is a list, so we need to iterate through it
|
| 19 |
-
for result in results:
|
| 20 |
-
# Visualize results
|
| 21 |
-
annotated_frame = result.plot()
|
| 22 |
-
|
| 23 |
-
# Display the frame
|
| 24 |
-
cv2.imshow("Real-time Object Detection", annotated_frame)
|
| 25 |
-
|
| 26 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 27 |
-
break
|
| 28 |
-
|
| 29 |
-
cap.release()
|
| 30 |
-
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app .py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Load the YOLO model
|
| 6 |
+
model = YOLO('yolov8n.pt')
|
| 7 |
+
|
| 8 |
+
def detect_objects(image):
|
| 9 |
+
# Convert PIL image (from Gradio webcam) → OpenCV format (BGR)
|
| 10 |
+
frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 11 |
+
|
| 12 |
+
# Perform object detection
|
| 13 |
+
results = model(frame)
|
| 14 |
+
|
| 15 |
+
# Plot detections (draw bounding boxes)
|
| 16 |
+
annotated_frame = results[0].plot()
|
| 17 |
+
|
| 18 |
+
# Convert back to RGB for displaying in Gradio
|
| 19 |
+
annotated_frame = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
| 20 |
+
|
| 21 |
+
return annotated_frame
|
| 22 |
+
|
| 23 |
+
# Create Gradio interface (this works in Hugging Face Spaces)
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=detect_objects,
|
| 26 |
+
inputs=gr.Image(source="webcam", streaming=True), # Enable live camera
|
| 27 |
+
outputs="image",
|
| 28 |
+
title="🎥 Real-time Object Detection (YOLOv8)",
|
| 29 |
+
description="Detects and labels objects in live camera feed using YOLOv8."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
demo.launch()
|