xmrt commited on
Commit
8f31dbb
β€’
1 Parent(s): 0bfbdde
Files changed (2) hide show
  1. app.py +156 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultralytics
2
+ from ultralytics import YOLO
3
+ import torch
4
+
5
+ # Gradio
6
+ import gradio as gr
7
+ import moviepy.editor as moviepy
8
+
9
+
10
+ # System and files
11
+ import os
12
+ import glob
13
+ import uuid
14
+
15
+ # Image manipulation
16
+ import numpy as np
17
+ import cv2
18
+
19
+ print(torch.__version__)
20
+
21
+ # Use GPU if available
22
+ if torch.cuda.is_available():
23
+ device = torch.device("cuda")
24
+ else:
25
+ device = torch.device("cpu")
26
+
27
+ os.system("nvidia-smi")
28
+
29
+ print("[INFO]: Imported modules!")
30
+ track_model = YOLO('yolov8n.pt') # Load an official Detect model
31
+ print("[INFO]: Downloaded models!")
32
+
33
+ def check_extension(video):
34
+ split_tup = os.path.splitext(video)
35
+
36
+ # extract the file name and extension
37
+ file_name = split_tup[0]
38
+ file_extension = split_tup[1]
39
+
40
+ if file_extension != ".mp4":
41
+ print("Converting to mp4")
42
+ clip = moviepy.VideoFileClip(video)
43
+
44
+ video = file_name+".mp4"
45
+ clip.write_videofile(video)
46
+
47
+ return video
48
+
49
+
50
+ def tracking(video, model, boxes=True):
51
+ print("[INFO] Is cuda available? ", torch.cuda.is_available())
52
+ print(device)
53
+
54
+ print("[INFO] Loading model...")
55
+ # Load an official or custom model
56
+
57
+ # Perform tracking with the model
58
+ print("[INFO] Starting tracking!")
59
+ # https://docs.ultralytics.com/modes/predict/
60
+ annotated_frame = model(video, boxes=boxes, device=device)
61
+
62
+ return annotated_frame
63
+
64
+ def show_tracking(video_content):
65
+
66
+ # https://docs.ultralytics.com/datasets/detect/coco/
67
+ video = cv2.VideoCapture(video_content)
68
+
69
+ # Track
70
+ video_track = tracking(video_content, track_model.track)
71
+
72
+ # Prepare to save video
73
+ #out_file = os.path.join(vis_out_dir, "track.mp4")
74
+ out_file = "track.mp4"
75
+ print("[INFO]: TRACK", out_file)
76
+
77
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for MP4 video
78
+ fps = video.get(cv2.CAP_PROP_FPS)
79
+ height, width, _ = video_track[0][0].orig_img.shape
80
+ size = (width,height)
81
+
82
+ out_track = cv2.VideoWriter(out_file, fourcc, fps, size)
83
+
84
+ # Go through frames and write them
85
+ for frame_track in video_track:
86
+ result_track = frame_track[0].plot() # plot a BGR numpy array of predictions
87
+ out_track.write(result_track)
88
+
89
+ print("[INFO] Done with frames")
90
+ #print(type(result_pose)) numpy ndarray
91
+
92
+ out_track.release()
93
+
94
+ video.release()
95
+ cv2.destroyAllWindows() # Closing window
96
+
97
+ return out_file
98
+
99
+ block = gr.Blocks()
100
+ with block:
101
+ with gr.Column():
102
+ with gr.Tab("Upload video"):
103
+ with gr.Column():
104
+ with gr.Row():
105
+ with gr.Column():
106
+ video_input = gr.Video(source="upload", type="filepath", height=612)
107
+ with gr.Row():
108
+ submit_detect_file = gr.Button("Detect and track objects", variant="primary")
109
+
110
+ with gr.Row():
111
+ video_output4 = gr.Video(height=512, label = "Detection and tracking", show_label=True, format="mp4")
112
+
113
+ with gr.Tab("Record video with webcam"):
114
+
115
+ with gr.Column():
116
+ with gr.Row():
117
+ with gr.Column():
118
+ webcam_input = gr.Video(source="webcam", height=612)
119
+
120
+ with gr.Row():
121
+ submit_detect_web = gr.Button("Detect and track objects", variant="primary")
122
+ with gr.Row():
123
+ webcam_output4 = gr.Video(height=716, label = "Detection and tracking", show_label=True, format="mp4")
124
+
125
+ with gr.Tab("General information"):
126
+ gr.Markdown("""
127
+ \n # Information about the models
128
+
129
+ \n ## Detection and tracking:
130
+
131
+ \n The tracking method in the Ultralight's YOLOv8 model is used for object tracking in videos. It takes a video file or a camera stream as input and returns the tracked objects in each frame. The method uses the COCO dataset classes for object detection and tracking.
132
+
133
+ \n The COCO dataset contains 80 classes of objects such as person, car, bicycle, etc. See https://docs.ultralytics.com/datasets/detect/coco/ for all available classes. The tracking method uses the COCO classes to detect and track the objects in the video frames. The tracked objects are represented as bounding boxes with labels indicating the class of the object.""")
134
+ gr.Markdown("You can load the keypoints in python in the following way: ")
135
+
136
+
137
+ # From file
138
+ submit_detect_file.click(fn=show_tracking,
139
+ inputs= video_input,
140
+ outputs = video_output4,
141
+ queue=False)
142
+
143
+
144
+ submit_detect_web.click(fn=show_tracking,
145
+ inputs= webcam_input,
146
+ outputs = webcam_output4,
147
+ queue=False)
148
+
149
+
150
+ if __name__ == "__main__":
151
+ block.queue(#concurrency_count=5, # When you increase the concurrency_count parameter in queue(), max_threads() in launch() is automatically increased as well.
152
+ #max_size=25, # Maximum number of requests that the queue processes
153
+ api_open = False # When creating a Gradio demo, you may want to restrict all traffic to happen through the user interface as opposed to the programmatic API that is automatically created for your Gradio demo.
154
+ ).launch()
155
+
156
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ numpy
3
+ opencv-python
4
+ ultralytics
5
+ lap
6
+ MoviePy
7
+ torch
8
+ torchvision