Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Load the YOLOv7 model
|
| 7 |
model = torch.hub.load('WongKinYiu/yolov7', 'yolov7', force_reload=True)
|
| 8 |
|
| 9 |
def detect_objects(image):
|
|
@@ -14,23 +14,6 @@ def detect_objects(image):
|
|
| 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 |
-
img = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
|
| 28 |
-
results = model(img)
|
| 29 |
-
|
| 30 |
-
# Process results
|
| 31 |
-
detections = results.xyxy[0].numpy()
|
| 32 |
-
annotated_image = video.copy()
|
| 33 |
-
|
| 34 |
for *box, conf, cls in detections:
|
| 35 |
x1, y1, x2, y2 = map(int, box)
|
| 36 |
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
|
@@ -43,22 +26,13 @@ def detect_live_objects(video):
|
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
gr.Markdown("# YOLOv7 Object Detection App")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
classify_button.click(fn=detect_objects, inputs=image_input, outputs=output_image)
|
| 53 |
-
|
| 54 |
-
with gr.Tab("Live Detection"):
|
| 55 |
-
video_input = gr.Video(label="Webcam Feed", type="numpy")
|
| 56 |
-
output_video = gr.Video(label="Live Detected Objects", type="numpy")
|
| 57 |
-
|
| 58 |
-
video_button = gr.Button("Start Live Detection")
|
| 59 |
-
|
| 60 |
-
video_button.click(fn=detect_live_objects, inputs=video_input, outputs=output_video)
|
| 61 |
|
| 62 |
# Launch the interface
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
app.launch(
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load the YOLOv7 model
|
| 7 |
model = torch.hub.load('WongKinYiu/yolov7', 'yolov7', force_reload=True)
|
| 8 |
|
| 9 |
def detect_objects(image):
|
|
|
|
| 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 |
x1, y1, x2, y2 = map(int, box)
|
| 19 |
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
|
|
|
| 26 |
with gr.Blocks() as app:
|
| 27 |
gr.Markdown("# YOLOv7 Object Detection App")
|
| 28 |
|
| 29 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
| 30 |
+
output_image = gr.Image(label="Detected Objects", type="numpy")
|
| 31 |
+
|
| 32 |
+
classify_button = gr.Button("Detect Objects")
|
| 33 |
+
|
| 34 |
+
classify_button.click(fn=detect_objects, inputs=image_input, outputs=output_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Launch the interface
|
| 37 |
if __name__ == "__main__":
|
| 38 |
+
app.launch()
|