Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +11 -1
video_processing.py
CHANGED
@@ -10,16 +10,23 @@ from PIL import Image
|
|
10 |
import uuid
|
11 |
from torchvision import models, transforms
|
12 |
from torch.nn import functional as F
|
|
|
13 |
|
14 |
-
categories = ["Joy", "Trust", "Fear", "Surprise", "Sadness", "Disgust", "Anger", "Anticipation"]
|
15 |
|
|
|
16 |
|
|
|
17 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
|
19 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
20 |
|
|
|
21 |
resnet50 = models.resnet50(pretrained=True).eval().to(device)
|
22 |
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def classify_frame(frame):
|
25 |
preprocess = transforms.Compose([
|
@@ -57,6 +64,7 @@ def download_video(url):
|
|
57 |
def sanitize_filename(filename):
|
58 |
return "".join([c if c.isalnum() or c in " .-_()" else "_" for c in filename])
|
59 |
|
|
|
60 |
def find_scenes(video_path):
|
61 |
video_manager = VideoManager([video_path])
|
62 |
scene_manager = SceneManager()
|
@@ -73,6 +81,7 @@ def convert_timestamp_to_seconds(timestamp):
|
|
73 |
h, m, s = map(float, timestamp.split(':'))
|
74 |
return int(h) * 3600 + int(m) * 60 + s
|
75 |
|
|
|
76 |
def extract_frames(video_path, start_time, end_time):
|
77 |
frames = []
|
78 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
@@ -86,6 +95,7 @@ def extract_frames(video_path, start_time, end_time):
|
|
86 |
|
87 |
import numpy as np
|
88 |
|
|
|
89 |
def analyze_scenes(video_path, scenes, description):
|
90 |
scene_scores = []
|
91 |
negative_descriptions = [
|
|
|
10 |
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"]
|
17 |
|
18 |
+
#initializing CLIP
|
19 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
20 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
|
21 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
22 |
|
23 |
+
#initializing ZG placeholder
|
24 |
resnet50 = models.resnet50(pretrained=True).eval().to(device)
|
25 |
|
26 |
+
#initialize caches
|
27 |
+
scene_cache = TTLCache(maxsize=100, ttl=86400) # cache up to 100 items, each for 1 day
|
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([
|
|
|
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 |
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)
|
|
|
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 = [
|