Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the YOLOv7 model
|
| 7 |
+
model = torch.hub.load('WongKinYiu/yolov7', 'yolov7')
|
| 8 |
+
|
| 9 |
+
def detect_objects(image):
|
| 10 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 11 |
+
results = model(img) # Perform inference
|
| 12 |
+
|
| 13 |
+
# Process results
|
| 14 |
+
detections = results.xyxy[0].numpy() # Get detections in xyxy format
|
| 15 |
+
annotated_image = image.copy()
|
| 16 |
+
|
| 17 |
+
for *box, conf, cls in detections:
|
| 18 |
+
# Draw bounding boxes on the image
|
| 19 |
+
x1, y1, x2, y2 = map(int, box)
|
| 20 |
+
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 21 |
+
label = f'{model.names[int(cls)]}: {conf:.2f}'
|
| 22 |
+
cv2.putText(annotated_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
| 23 |
+
|
| 24 |
+
return annotated_image
|
| 25 |
+
|
| 26 |
+
def detect_live_objects(video):
|
| 27 |
+
# Capture frames from the video feed and process them
|
| 28 |
+
img = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
|
| 29 |
+
results = model(img)
|
| 30 |
+
|
| 31 |
+
# Process results
|
| 32 |
+
detections = results.xyxy[0].numpy()
|
| 33 |
+
annotated_image = video.copy()
|
| 34 |
+
|
| 35 |
+
for *box, conf, cls in detections:
|
| 36 |
+
x1, y1, x2, y2 = map(int, box)
|
| 37 |
+
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 38 |
+
label = f'{model.names[int(cls)]}: {conf:.2f}'
|
| 39 |
+
cv2.putText(annotated_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
| 40 |
+
|
| 41 |
+
return annotated_image
|
| 42 |
+
|
| 43 |
+
# Create Gradio interface
|
| 44 |
+
with gr.Blocks() as app:
|
| 45 |
+
gr.Markdown("# YOLOv7 Object Detection App")
|
| 46 |
+
|
| 47 |
+
with gr.Tab("Image Classification"):
|
| 48 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
| 49 |
+
output_image = gr.Image(label="Detected Objects", type="numpy")
|
| 50 |
+
|
| 51 |
+
classify_button = gr.Button("Detect Objects")
|
| 52 |
+
|
| 53 |
+
classify_button.click(fn=detect_objects, inputs=image_input, outputs=output_image)
|
| 54 |
+
|
| 55 |
+
with gr.Tab("Live Detection"):
|
| 56 |
+
video_input = gr.Video(label="Webcam Feed", type="numpy")
|
| 57 |
+
output_video = gr.Video(label="Live Detected Objects", type="numpy")
|
| 58 |
+
|
| 59 |
+
video_button = gr.Button("Start Live Detection")
|
| 60 |
+
|
| 61 |
+
video_button.click(fn=detect_live_objects, inputs=video_input, outputs=output_video)
|
| 62 |
+
|
| 63 |
+
# Launch the interface
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
app.launch(debug=True)
|