jschwab21 commited on
Commit
4df7b58
·
verified ·
1 Parent(s): f9b1ae8

Update video_processing.py

Browse files
Files changed (1) hide show
  1. video_processing.py +20 -5
video_processing.py CHANGED
@@ -11,6 +11,7 @@ import uuid
11
  from torchvision import models, transforms
12
  from torch.nn import functional as F
13
  from cachetools import cached, TTLCache
 
14
 
15
 
16
  categories = ["Joy", "Trust", "Fear", "Surprise", "Sadness", "Disgust", "Anger", "Anticipation"]
@@ -28,6 +29,22 @@ scene_cache = TTLCache(maxsize=100, ttl=86400) # cache up to 100 items, each fo
28
  frame_cache = TTLCache(maxsize=100, ttl=86400)
29
  analysis_cache = TTLCache(maxsize=100, ttl=86400)
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def classify_frame(frame):
32
  preprocess = transforms.Compose([
33
  transforms.Resize(256),
@@ -64,7 +81,7 @@ def download_video(url):
64
  def sanitize_filename(filename):
65
  return "".join([c if c.isalnum() or c in " .-_()" else "_" for c in filename])
66
 
67
- @cached(scene_cache, key=lambda video_path: video_path)
68
  def find_scenes(video_path):
69
  video_manager = VideoManager([video_path])
70
  scene_manager = SceneManager()
@@ -81,7 +98,7 @@ def convert_timestamp_to_seconds(timestamp):
81
  h, m, s = map(float, timestamp.split(':'))
82
  return int(h) * 3600 + int(m) * 60 + s
83
 
84
- @cached(frame_cache, key=lambda video_path, start_time, end_time: f"{video_path}_{start_time}_{end_time}")
85
  def extract_frames(video_path, start_time, end_time):
86
  frames = []
87
  start_seconds = convert_timestamp_to_seconds(start_time)
@@ -93,9 +110,7 @@ def extract_frames(video_path, start_time, end_time):
93
  frames.append(frame)
94
  return frames
95
 
96
- import numpy as np
97
-
98
- @cached(analysis_cache, key=lambda video_path, scenes, description: f"{video_path}_{hash(tuple(scenes))}_{hash(description)}")
99
  def analyze_scenes(video_path, scenes, description):
100
  scene_scores = []
101
  negative_descriptions = [
 
11
  from torchvision import models, transforms
12
  from torch.nn import functional as F
13
  from cachetools import cached, TTLCache
14
+ import numpy as np
15
 
16
 
17
  categories = ["Joy", "Trust", "Fear", "Surprise", "Sadness", "Disgust", "Anger", "Anticipation"]
 
29
  frame_cache = TTLCache(maxsize=100, ttl=86400)
30
  analysis_cache = TTLCache(maxsize=100, ttl=86400)
31
 
32
+
33
+ def cache_info_decorator(func, cache):
34
+ """Decorator to add caching and logging to a function."""
35
+ key_func = lambda *args, **kwargs: "_".join(map(str, args)) # Simple key func based on str(args)
36
+
37
+ @cached(cache, key=key_func)
38
+ def wrapper(*args, **kwargs):
39
+ key = key_func(*args, **kwargs)
40
+ if key in cache:
41
+ logging.info(f"Cache hit for key: {key}")
42
+ else:
43
+ logging.info(f"Cache miss for key: {key}. Caching result.")
44
+ return func(*args, **kwargs)
45
+ return wrapper
46
+
47
+
48
  def classify_frame(frame):
49
  preprocess = transforms.Compose([
50
  transforms.Resize(256),
 
81
  def sanitize_filename(filename):
82
  return "".join([c if c.isalnum() or c in " .-_()" else "_" for c in filename])
83
 
84
+ @cache_info_decorator
85
  def find_scenes(video_path):
86
  video_manager = VideoManager([video_path])
87
  scene_manager = SceneManager()
 
98
  h, m, s = map(float, timestamp.split(':'))
99
  return int(h) * 3600 + int(m) * 60 + s
100
 
101
+ @cache_info_decorator
102
  def extract_frames(video_path, start_time, end_time):
103
  frames = []
104
  start_seconds = convert_timestamp_to_seconds(start_time)
 
110
  frames.append(frame)
111
  return frames
112
 
113
+ @cache_info_decorator
 
 
114
  def analyze_scenes(video_path, scenes, description):
115
  scene_scores = []
116
  negative_descriptions = [