randomshit11 commited on
Commit
b281904
·
verified ·
1 Parent(s): e60211d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -71
app.py CHANGED
@@ -1,81 +1,62 @@
1
- import gradio as gr
2
  import cv2
3
- import os
4
-
5
  from ultralytics import YOLO
6
 
 
 
7
 
8
- model = YOLO('yolov8n-seg.pt')
9
- path = [['3891186464_00d76e10a2_z.jpg'], ['images (1).jpeg']]
10
- video_path = [['sheep.mp4']]
11
 
 
 
 
 
 
 
12
 
13
- def show_preds_image(image_path):
14
- image = cv2.imread(image_path)
15
- image_copy=image.copy()
16
- threshold = 0.1
17
- results = model(image)[0]
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
- def show_preds_video(video_path):
45
- cap = cv2.VideoCapture(video_path)
46
- while(cap.isOpened()):
47
  ret, frame = cap.read()
48
- if ret:
49
- threshold = 0.1
50
- frame_copy = frame.copy()
51
- results = model(frame)[0]
52
- for result in results.boxes.data.tolist():
53
- x1, y1, x2, y2, score, class_id = result
54
-
55
- if score > threshold:
56
- cv2.rectangle(frame_copy, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
57
- cv2.putText(frame_copy, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
58
- cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
59
- cv2.putText(frame_copy, str(score), (int(x1), int(y2 + 10)),
60
- cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3, cv2.LINE_AA)
61
-
62
- yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
63
- inputs = [
64
- gr.Video(label='Input Video')]
65
- outputs = gr.Video(label='Output Video')
66
-
67
-
68
-
69
- interface_video = gr.Interface(
70
- fn=show_preds_video,
71
- inputs=inputs,
72
- outputs=outputs,
73
- title="Cattle detector using YOLOV8 NANO",
74
- examples=video_path,
75
- cache_examples=False,
 
 
 
76
  )
77
 
78
- gr.TabbedInterface(
79
- [interface_image, interface_video],
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()