xmrt commited on
Commit
eef5e41
·
1 Parent(s): 918bea5
Files changed (1) hide show
  1. app.py +17 -30
app.py CHANGED
@@ -30,23 +30,6 @@ 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)
@@ -66,6 +49,13 @@ def show_tracking(video_content):
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
 
@@ -106,28 +96,27 @@ def track_blocks(video_content):
106
  block = gr.Blocks()
107
  with block:
108
  with gr.Column():
109
- with gr.Tab("Upload video"):
110
  with gr.Column():
111
  with gr.Row():
112
  with gr.Column():
113
- video_input = gr.Video(source="upload", type="filepath", height=256)
 
114
  with gr.Row():
115
- submit_detect_file = gr.Button("Detect and track objects", variant="primary")
116
-
117
  with gr.Row():
118
- video_output4 = gr.Video(height=512, label = "Detection and tracking", show_label=True, format="mp4")
119
 
120
- with gr.Tab("Record video with webcam"):
121
-
122
  with gr.Column():
123
  with gr.Row():
124
  with gr.Column():
125
- webcam_input = gr.Video(source="webcam", height=256)
126
-
127
  with gr.Row():
128
- submit_detect_web = gr.Button("Detect and track objects", variant="primary")
 
129
  with gr.Row():
130
- webcam_output4 = gr.Video(height=716, label = "Detection and tracking", show_label=True, format="mp4")
131
 
132
  with gr.Tab("General information"):
133
  gr.Markdown("""
@@ -138,8 +127,6 @@ with block:
138
  \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.
139
 
140
  \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.""")
141
- gr.Markdown("You can load the keypoints in python in the following way: ")
142
-
143
 
144
  # From file
145
  submit_detect_file.click(fn=track_blocks,
 
30
  track_model = YOLO('yolov8n.pt') # Load an official Detect model
31
  print("[INFO]: Downloaded models!")
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def tracking(video, model, boxes=True):
34
  print("[INFO] Is cuda available? ", torch.cuda.is_available())
35
  print(device)
 
49
  # https://docs.ultralytics.com/datasets/detect/coco/
50
  video = cv2.VideoCapture(video_content)
51
 
52
+ fps = video.get(cv2.CAP_PROP_FPS) # OpenCV v2.x used "CV_CAP_PROP_FPS"
53
+ frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
54
+ duration = frame_count/fps
55
+
56
+ if duration > 10:
57
+ raise gr.Error("Please provide or record a video shorter than 10 seconds...")
58
+
59
  # Track
60
  video_track = tracking(video_content, track_model.track)
61
 
 
96
  block = gr.Blocks()
97
  with block:
98
  with gr.Column():
99
+ with gr.Tab("Record video with webcam"):
100
  with gr.Column():
101
  with gr.Row():
102
  with gr.Column():
103
+ webcam_input = gr.Video(source="webcam", height=256)
104
+
105
  with gr.Row():
106
+ submit_detect_web = gr.Button("Detect and track objects", variant="primary")
 
107
  with gr.Row():
108
+ webcam_output4 = gr.Video(height=716, label = "Detection and tracking", show_label=True, format="mp4")
109
 
110
+ with gr.Tab("Upload video"):
 
111
  with gr.Column():
112
  with gr.Row():
113
  with gr.Column():
114
+ video_input = gr.Video(source="upload", type="filepath", height=256)
 
115
  with gr.Row():
116
+ submit_detect_file = gr.Button("Detect and track objects", variant="primary")
117
+
118
  with gr.Row():
119
+ video_output4 = gr.Video(height=512, label = "Detection and tracking", show_label=True, format="mp4")
120
 
121
  with gr.Tab("General information"):
122
  gr.Markdown("""
 
127
  \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.
128
 
129
  \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.""")
 
 
130
 
131
  # From file
132
  submit_detect_file.click(fn=track_blocks,