Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,62 @@
|
|
1 |
-
import
|
2 |
import cv2
|
3 |
-
import
|
4 |
-
|
5 |
from ultralytics import YOLO
|
6 |
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
video_path = [['sheep.mp4']]
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
for result in results.boxes.data.tolist():
|
19 |
-
x1, y1, x2, y2, score, class_id = result
|
20 |
-
|
21 |
-
if score > threshold:
|
22 |
-
cv2.rectangle(image_copy, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
|
23 |
-
cv2.putText(image_copy, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
|
24 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
|
25 |
-
cv2.putText(image_copy, str(score), (int(x1), int(y2 + 10)),
|
26 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3, cv2.LINE_AA)
|
27 |
-
return cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)
|
28 |
-
|
29 |
-
inputs_image = [
|
30 |
-
gr.components.Image(type="filepath", label="Input Image"),
|
31 |
-
]
|
32 |
-
outputs_image = [
|
33 |
-
gr.components.Image(type="numpy", label="Output Image"),
|
34 |
-
]
|
35 |
-
interface_image = gr.Interface(
|
36 |
-
fn=show_preds_image,
|
37 |
-
inputs=inputs_image,
|
38 |
-
outputs=outputs_image,
|
39 |
-
title="Animal detector using YOLOV8 NANO",
|
40 |
-
examples=path,
|
41 |
-
cache_examples=False,
|
42 |
-
)
|
43 |
|
44 |
-
|
45 |
-
cap = cv2.VideoCapture(video_path)
|
46 |
-
while(cap.isOpened()):
|
47 |
ret, frame = cap.read()
|
48 |
-
if ret:
|
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 |
-
tab_names=['Image inference', 'Video inference']
|
81 |
-
).launch()
|
|
|
1 |
+
import torch
|
2 |
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
from ultralytics import YOLO
|
6 |
|
7 |
+
# Check if CUDA (GPU support) is available
|
8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
|
10 |
+
# Load the YOLOv8 model
|
11 |
+
model = YOLO('yolov8n-seg.pt').to(device)
|
|
|
12 |
|
13 |
+
# Define the function to process the video on GPU
|
14 |
+
def process_video(input_video_path):
|
15 |
+
cap = cv2.VideoCapture(input_video_path)
|
16 |
+
if not cap.isOpened():
|
17 |
+
print("Error: Couldn't open the video file.")
|
18 |
+
return
|
19 |
|
20 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
21 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
22 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
23 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
24 |
+
out = cv2.VideoWriter("output_video.mp4", fourcc, fps, (frame_width, frame_height))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
while cap.isOpened():
|
|
|
|
|
27 |
ret, frame = cap.read()
|
28 |
+
if not ret:
|
29 |
+
break
|
30 |
+
|
31 |
+
threshold = 0.1
|
32 |
+
frame_copy = frame.copy()
|
33 |
+
# Convert frame to torch tensor and move it to GPU
|
34 |
+
frame_tensor = torch.from_numpy(frame_copy).permute(2, 0, 1).unsqueeze(0).float().to(device) / 255.0
|
35 |
+
results = model(frame_tensor)[0]
|
36 |
+
for result in results.boxes.data.tolist():
|
37 |
+
x1, y1, x2, y2, score, class_id = result
|
38 |
+
if score > threshold:
|
39 |
+
cv2.rectangle(frame_copy, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
|
40 |
+
cv2.putText(frame_copy, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
|
41 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
|
42 |
+
cv2.putText(frame_copy, str(score), (int(x1), int(y2 + 10)),
|
43 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3, cv2.LINE_AA)
|
44 |
+
out.write(frame_copy)
|
45 |
+
|
46 |
+
cap.release()
|
47 |
+
out.release()
|
48 |
+
|
49 |
+
# Define the input and output interfaces for Gradio
|
50 |
+
inputs_video = gr.Video(label="Input Video")
|
51 |
+
outputs_video = gr.Video(label="Output Video")
|
52 |
+
|
53 |
+
# Create the Gradio interface
|
54 |
+
demo = gr.Interface(
|
55 |
+
fn=process_video,
|
56 |
+
inputs=inputs_video,
|
57 |
+
outputs=outputs_video,
|
58 |
+
title="Animal detector using YOLOv8 NANO for Videos (GPU)",
|
59 |
)
|
60 |
|
61 |
+
# Launch the interface
|
62 |
+
demo.launch()
|
|
|
|