SkalskiP commited on
Commit
e75f111
1 Parent(s): f34df61

Old video removal mechanism.

Browse files
Files changed (2) hide show
  1. app.py +32 -2
  2. utils/video.py +32 -0
app.py CHANGED
@@ -10,11 +10,34 @@ from tqdm import tqdm
10
  from inference.models import YOLOWorld
11
 
12
  from utils.efficient_sam import load, inference_with_boxes
13
- from utils.video import generate_file_name, calculate_end_frame_index, create_directory
 
 
 
 
 
14
 
15
  MARKDOWN = """
16
  # YOLO-World + EfficientSAM 🔥
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  This is a demo of zero-shot object detection and instance segmentation using
19
  [YOLO-World](https://github.com/AILab-CVC/YOLO-World) and
20
  [EfficientSAM](https://github.com/yformer/EfficientSAM).
@@ -35,6 +58,7 @@ RESULTS = "results"
35
 
36
  IMAGE_EXAMPLES = [
37
  ['https://media.roboflow.com/dog.jpeg', 'dog, eye, nose, tongue, car', 0.005, 0.1, True, False, False],
 
38
  ]
39
  VIDEO_EXAMPLES = [
40
  ['https://media.roboflow.com/supervision/video-examples/croissant-1280x720.mp4', 'croissant', 0.01, 0.2, False, False, False],
@@ -51,7 +75,7 @@ BOUNDING_BOX_ANNOTATOR = sv.BoundingBoxAnnotator()
51
  MASK_ANNOTATOR = sv.MaskAnnotator()
52
  LABEL_ANNOTATOR = sv.LabelAnnotator()
53
 
54
-
55
  create_directory(directory_path=RESULTS)
56
 
57
 
@@ -89,6 +113,9 @@ def process_image(
89
  with_confidence: bool = False,
90
  with_class_agnostic_nms: bool = False,
91
  ) -> np.ndarray:
 
 
 
92
  categories = process_categories(categories)
93
  YOLO_WORLD_MODEL.set_classes(categories)
94
  results = YOLO_WORLD_MODEL.infer(input_image, confidence=confidence_threshold)
@@ -124,6 +151,9 @@ def process_video(
124
  with_class_agnostic_nms: bool = False,
125
  progress=gr.Progress(track_tqdm=True)
126
  ) -> str:
 
 
 
127
  categories = process_categories(categories)
128
  YOLO_WORLD_MODEL.set_classes(categories)
129
  video_info = sv.VideoInfo.from_video_path(input_video)
 
10
  from inference.models import YOLOWorld
11
 
12
  from utils.efficient_sam import load, inference_with_boxes
13
+ from utils.video import (
14
+ generate_file_name,
15
+ calculate_end_frame_index,
16
+ create_directory,
17
+ remove_files_older_than
18
+ )
19
 
20
  MARKDOWN = """
21
  # YOLO-World + EfficientSAM 🔥
22
 
23
+ <div>
24
+ <a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/zero-shot-object-detection-with-yolo-world.ipynb">
25
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Colab" style="display:inline-block;">
26
+ </a>
27
+ <a href="https://blog.roboflow.com/what-is-yolo-world/">
28
+ <img src="https://raw.githubusercontent.com/roboflow-ai/notebooks/main/assets/badges/roboflow-blogpost.svg" alt="Roboflow" style="display:inline-block;">
29
+ </a>
30
+ <a href="https://www.youtube.com/watch?v=X7gKBGVz4vs">
31
+ <img src="https://badges.aleen42.com/src/youtube.svg" alt="YouTube" style="display:inline-block;">
32
+ </a>
33
+ <a href="https://github.com/AILab-CVC/YOLO-World">
34
+ <img src="https://badges.aleen42.com/src/github.svg" alt="GitHub" style="display:inline-block;">
35
+ </a>
36
+ <a href="https://arxiv.org/abs/2401.17270">
37
+ <img src="https://img.shields.io/badge/arXiv-2401.17270-b31b1b.svg" alt="arXiv" style="display:inline-block;">
38
+ </a>
39
+ </div>
40
+
41
  This is a demo of zero-shot object detection and instance segmentation using
42
  [YOLO-World](https://github.com/AILab-CVC/YOLO-World) and
43
  [EfficientSAM](https://github.com/yformer/EfficientSAM).
 
58
 
59
  IMAGE_EXAMPLES = [
60
  ['https://media.roboflow.com/dog.jpeg', 'dog, eye, nose, tongue, car', 0.005, 0.1, True, False, False],
61
+ ['https://media.roboflow.com/albert-4x.png', 'hand, hair', 0.005, 0.1, True, False, False],
62
  ]
63
  VIDEO_EXAMPLES = [
64
  ['https://media.roboflow.com/supervision/video-examples/croissant-1280x720.mp4', 'croissant', 0.01, 0.2, False, False, False],
 
75
  MASK_ANNOTATOR = sv.MaskAnnotator()
76
  LABEL_ANNOTATOR = sv.LabelAnnotator()
77
 
78
+ # creating video results directory
79
  create_directory(directory_path=RESULTS)
80
 
81
 
 
113
  with_confidence: bool = False,
114
  with_class_agnostic_nms: bool = False,
115
  ) -> np.ndarray:
116
+ # cleanup of old video files
117
+ remove_files_older_than(RESULTS, 30)
118
+
119
  categories = process_categories(categories)
120
  YOLO_WORLD_MODEL.set_classes(categories)
121
  results = YOLO_WORLD_MODEL.infer(input_image, confidence=confidence_threshold)
 
151
  with_class_agnostic_nms: bool = False,
152
  progress=gr.Progress(track_tqdm=True)
153
  ) -> str:
154
+ # cleanup of old video files
155
+ remove_files_older_than(RESULTS, 30)
156
+
157
  categories = process_categories(categories)
158
  YOLO_WORLD_MODEL.set_classes(categories)
159
  video_info = sv.VideoInfo.from_video_path(input_video)
utils/video.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import datetime
3
  import uuid
 
4
 
5
  import supervision as sv
6
 
@@ -14,6 +15,37 @@ def generate_file_name(extension="mp4"):
14
  return f"{current_datetime}_{unique_id}.{extension}"
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def calculate_end_frame_index(source_video_path: str) -> int:
18
  video_info = sv.VideoInfo.from_video_path(source_video_path)
19
  return min(
 
1
  import os
2
  import datetime
3
  import uuid
4
+ from typing import List
5
 
6
  import supervision as sv
7
 
 
15
  return f"{current_datetime}_{unique_id}.{extension}"
16
 
17
 
18
+ def list_files_older_than(directory: str, diff_minutes: int) -> List[str]:
19
+ diff_seconds = diff_minutes * 60
20
+ now = datetime.datetime.now()
21
+ older_files: List[str] = []
22
+
23
+ for filename in os.listdir(directory):
24
+ file_path = os.path.join(directory, filename)
25
+ if os.path.isfile(file_path):
26
+ file_mod_time = os.path.getmtime(file_path)
27
+ file_mod_datetime = datetime.datetime.fromtimestamp(file_mod_time)
28
+ time_diff = now - file_mod_datetime
29
+ if time_diff.total_seconds() > diff_seconds:
30
+ older_files.append(file_path)
31
+
32
+ return older_files
33
+
34
+
35
+ def remove_files_older_than(directory: str, diff_minutes: int) -> None:
36
+ older_files = list_files_older_than(directory, diff_minutes)
37
+ file_count = len(older_files)
38
+
39
+ for file_path in older_files:
40
+ os.remove(file_path)
41
+
42
+ now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
43
+ print(
44
+ f"[{now}] Removed {file_count} files older than {diff_minutes} minutes from "
45
+ f"'{directory}' directory."
46
+ )
47
+
48
+
49
  def calculate_end_frame_index(source_video_path: str) -> int:
50
  video_info = sv.VideoInfo.from_video_path(source_video_path)
51
  return min(