Ivan Murabito commited on
Commit
a1c9872
·
1 Parent(s): 2ccde80

add frame extractor gradio

Browse files
.gitignore CHANGED
@@ -7,3 +7,4 @@
7
  /.env
8
  /__pycache__
9
  yologp/__pycache__
 
 
7
  /.env
8
  /__pycache__
9
  yologp/__pycache__
10
+ /data
requirements.txt CHANGED
@@ -11,4 +11,5 @@ gradio==3.50
11
  huggingface_hub
12
  autodistill>=0.1.15
13
  autodistill_grounded_sam>=0.1.1
14
- python-dotenv
 
 
11
  huggingface_hub
12
  autodistill>=0.1.15
13
  autodistill_grounded_sam>=0.1.1
14
+ python-dotenv
15
+ pytube
yologp/frame_extractor_gradio_app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytube import YouTube
2
+ import gradio as gr
3
+ from pathlib import Path
4
+ import os
5
+ from supervision import (
6
+ ImageSink,
7
+ get_video_frames_generator,
8
+ list_files_with_extensions,
9
+ )
10
+ from tqdm import tqdm
11
+ from helpers import zoom_center
12
+ import shutil
13
+
14
+ data_path = Path(__file__).parent.parent / "data"
15
+ print("DATA PATH: ", data_path)
16
+
17
+
18
+ def download_youtube_url(url, out_dir) -> str:
19
+ yt = YouTube(url=url)
20
+ files = yt.streams.filter(file_extension="mp4", only_video=True)
21
+ itag = files[0].itag
22
+ video = yt.streams.get_by_itag(int(itag))
23
+ path = video.download(output_path=out_dir)
24
+ return path
25
+
26
+
27
+ def extract_frames(
28
+ url,
29
+ video_path,
30
+ stride,
31
+ start,
32
+ end,
33
+ resize_w,
34
+ zoom,
35
+ progress=gr.Progress(track_tqdm=True),
36
+ ):
37
+ if video_path is not None:
38
+ v_path = Path(video_path.name)
39
+ elif len(url) > 0:
40
+ progress(0.1, "Downloading..")
41
+ d_path = download_youtube_url(url, data_path)
42
+ v_path = Path(d_path)
43
+ print("video path:", v_path)
44
+ video_name = str(v_path.stem).replace(" ", "")
45
+ target_dir = Path(f"{data_path}/{video_name}_frames")
46
+ cont = 0
47
+ with ImageSink(
48
+ target_dir_path=target_dir,
49
+ image_name_pattern="image_{:05d}.jpg",
50
+ overwrite=True,
51
+ ) as sink:
52
+ for image in tqdm(
53
+ get_video_frames_generator(
54
+ source_path=str(v_path), stride=stride, start=start
55
+ )
56
+ ):
57
+ if zoom > 1:
58
+ image = zoom_center(img=image.copy(), zoom_factor=zoom)
59
+ sink.save_image(image=image.copy())
60
+ cont += 1
61
+ progress(0.8, "Zipping..")
62
+ print("Target_dir", target_dir)
63
+ frames = list_files_with_extensions(directory=target_dir, extensions=["jpg", "png"])
64
+ print(len(frames))
65
+ archive_ = shutil.make_archive(
66
+ target_dir,
67
+ "zip",
68
+ target_dir,
69
+ )
70
+
71
+ print(archive_)
72
+
73
+ v_path.unlink()
74
+ return frames[0:10], [archive_]
75
+
76
+
77
+ inputs = [
78
+ gr.Textbox(label="Youtube_url"),
79
+ gr.File(label="mp4 or mov", file_types=["video"]),
80
+ gr.Slider(label="Stride", value=60, maximum=1200),
81
+ gr.Number(label="Start Frame", value=0),
82
+ gr.Number(label="End Frame", value=-1),
83
+ gr.Number(label="Resize Width (px)", value=-1),
84
+ gr.Slider(label="Image Zoom", minimum=1.0, maximum=2.99, value=1.4),
85
+ ]
86
+ outputs = [gr.Gallery(label="preview"), gr.File()]
87
+ interface = gr.Interface(
88
+ fn=extract_frames,
89
+ inputs=inputs,
90
+ outputs=outputs,
91
+ examples=[["https://www.youtube.com/watch?v=XDhjS_fzhsQ"]],
92
+ allow_flagging=False,
93
+ )
94
+
95
+
96
+ if __name__ == "__main__":
97
+ interface.queue(max_size=10).launch(server_name="0.0.0.0")
yologp/inference_gradio_app.py CHANGED
@@ -24,9 +24,9 @@ classes = ["curb", "curb", "helmet", "wheel", "moto", "moto", "rider", "road"]
24
  selected_classes = [0, 2, 3, 5, 6]
25
 
26
 
27
- def inference(image, progress=gr.Progress()):
28
  frame = cv2.resize(image, (960, 640))
29
- res = model(frame, imgsz=(960, 640), conf=0.3, iou=0.4)[0]
30
  detections = sv.Detections.from_ultralytics(res)
31
  detections = detections[np.isin(detections.class_id, selected_classes)]
32
  if len(detections) > 0:
@@ -45,7 +45,11 @@ with gr.Blocks() as inference_app:
45
  with gr.Row():
46
  with gr.Column():
47
  image = gr.Image()
48
- button = gr.Button(variant="primary")
 
 
 
 
49
  examples = gr.Examples(
50
  examples=[
51
  ["./assets/Rossi_Lorenzo_Catalunya2009.png"],
@@ -56,8 +60,7 @@ with gr.Blocks() as inference_app:
56
  with gr.Column():
57
  output_im = gr.Image()
58
 
59
- button.click(fn=inference, inputs=[image], outputs=output_im)
60
-
61
 
62
  if __name__ == "__main__":
63
  inference_app.queue(max_size=10).launch(server_name="0.0.0.0")
 
24
  selected_classes = [0, 2, 3, 5, 6]
25
 
26
 
27
+ def inference(image, conf: float, iou: float, progress=gr.Progress()):
28
  frame = cv2.resize(image, (960, 640))
29
+ res = model(frame, imgsz=(960, 640), conf=conf, iou=iou)[0]
30
  detections = sv.Detections.from_ultralytics(res)
31
  detections = detections[np.isin(detections.class_id, selected_classes)]
32
  if len(detections) > 0:
 
45
  with gr.Row():
46
  with gr.Column():
47
  image = gr.Image()
48
+ conf = gr.Slider(label="Confidence", minimum=0, maximum=0.99, value=0.3)
49
+ iou = gr.Slider(label="IoU", minimum=0, maximum=0.99, value=0.45)
50
+
51
+ with gr.Row():
52
+ button = gr.Button(variant="primary")
53
  examples = gr.Examples(
54
  examples=[
55
  ["./assets/Rossi_Lorenzo_Catalunya2009.png"],
 
60
  with gr.Column():
61
  output_im = gr.Image()
62
 
63
+ button.click(fn=inference, inputs=[image, conf, iou], outputs=output_im)
 
64
 
65
  if __name__ == "__main__":
66
  inference_app.queue(max_size=10).launch(server_name="0.0.0.0")
yologp/live_inference.py CHANGED
@@ -39,7 +39,7 @@ while True:
39
  frame = cv2.resize(frame, (960, 640))
40
  res = model(frame, imgsz=(960, 640), conf=0.25, iou=0.45)[0]
41
  if len(res) > 0:
42
- detections = sv.Detections.from_yolov8(res)
43
  detections = detections[np.isin(detections.class_id, selected_classes)]
44
  helmets_wheel = detections[np.isin(detections.class_id, [2, 3, 4])]
45
  curbs = detections[np.isin(detections.class_id, [0, 1, 7])]
 
39
  frame = cv2.resize(frame, (960, 640))
40
  res = model(frame, imgsz=(960, 640), conf=0.25, iou=0.45)[0]
41
  if len(res) > 0:
42
+ detections = sv.Detections.from_ultralytics(res)
43
  detections = detections[np.isin(detections.class_id, selected_classes)]
44
  helmets_wheel = detections[np.isin(detections.class_id, [2, 3, 4])]
45
  curbs = detections[np.isin(detections.class_id, [0, 1, 7])]