-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
249 lines (211 loc) · 9.01 KB
/
main.py
File metadata and controls
249 lines (211 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from audioop import mul
from functools import partial, reduce
from itertools import repeat
import os
import time
from typing import Literal, Optional
from cv2.gapi.streaming import timestamp
from dotenv import load_dotenv
from more_itertools import chunked
from videoparser import VideoFrameSampler
from llm import LlamaAnomalyDetection, LlamaImageExplainer, LlamaImageDetector, LlamaPromptTemplates
from groq import APIStatusError, Groq
import os
import sys
from moviepy import VideoFileClip
import multiprocessing as mp
load_dotenv()
GROQ_API_KEY = os.environ["GROQ_API_KEY"]
def llm_inference_single(frame_base64, api_key, prompt_input, model, method):
cls = LlamaImageDetector if method == "detection" else LlamaAnomalyDetection
try:
reply = cls(api_key, model=model).inference(frame_base64, prompt_input)
except APIStatusError as e:
raise Exception(str(e))
return reply
def detection_in_video(
video_path: str,
prompt_input: str,
method: Literal["detection", "anomaly"] = "detection",
every_n_seconds: float = 2.0,
max_frames: int = 20,
model: str = "meta-llama/llama-4-scout-17b-16e-instruct",
multiframe: bool = False,
):
sampler = VideoFrameSampler(video_path)
frames_base64 = sampler.sample_frames(
every_n_seconds=every_n_seconds,
output_format="base64",
max_frames=max_frames,
multiframe=multiframe,
)
assert method in ["detection", "anomaly"]
# if method == "detection":
# llm = LlamaImageDetector(GROQ_API_KEY, model=model)
# elif method == "anomaly":
# llm = LlamaAnomalyDetection(GROQ_API_KEY, model=model)
# else:
# raise ValueError("method must be 'detection' or 'anomaly'")
# explainer = LLamaImageExplainer(GROQ_API_KEY, model=model)
matched_timestamps = []
timestamps = [round(i * every_n_seconds * 4 if multiframe else i * every_n_seconds, 5) for i in range(len(frames_base64))]
replies = []
temp = time.time()
print("Querying Groq API...")
with mp.Pool(4) as pool:
try:
replies = pool.map(
partial(llm_inference_single, api_key=GROQ_API_KEY, prompt_input=prompt_input, model=model, method=method),
[f"data:image/jpeg;base64,{frame}" for frame in frames_base64]
)
except Exception as e:
print(f"[!] Error: {e}")
print(f"{time.time()-temp:.2f}s")
for reply, timestamp_sec in zip(replies, timestamps):
if reply.lower() == "yes":
matched_timestamps.append(timestamp_sec)
if multiframe:
print(f"[✓] Object detected at {timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec")
else:
print(f"[✓] Object detected at {timestamp_sec} sec")
elif reply == "no":
if multiframe:
print(f"[ ] No object at {timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec")
else:
print(f"[ ] No object at {timestamp_sec} sec")
# explanation = explainer.explain(image)
# print(f"EXPLANATION: {explanation}")
else:
print(f"RESPONSE ({timestamp_sec} sec): {reply}")
return matched_timestamps
def detection_in_video_batched(
video_path: str,
prompt_input: str,
method: Literal["detection", "anomaly"] = "detection",
every_n_seconds: float = 2.0,
max_frames: int = 20,
model: str = "meta-llama/llama-4-scout-17b-16e-instruct",
multiframe: bool = False,
batch_size: int = 4,
api_key: str = GROQ_API_KEY,
):
sampler = VideoFrameSampler(video_path)
if method == "detection":
llm = LlamaImageDetector(api_key, model=model)
elif method == "anomaly":
llm = LlamaAnomalyDetection(api_key, model=model)
else:
raise ValueError("method must be 'detection' or 'anomaly'")
# explainer = LLamaImageExplainer(GROQ_API_KEY, model=model)
frames_base64 = sampler.sample_frames(
every_n_seconds=every_n_seconds,
output_format="base64",
max_frames=max_frames,
multiframe=multiframe,
)
matched_timestamps = []
timestamps = [round(i * every_n_seconds * 4 if multiframe else i * every_n_seconds, 5) for i in range(len(frames_base64))]
for i, (batch_frames_base64, batch_timestamps) in enumerate(zip(chunked(frames_base64, batch_size), chunked(timestamps, batch_size))):
images = [f"data:image/jpeg;base64,{frame_base64}" for frame_base64 in batch_frames_base64]
try:
replies = llm.batched_inference(images, prompt_input)
for reply, timestamp_sec in zip(replies, batch_timestamps):
if reply.lower() == "yes":
matched_timestamps.append(timestamp_sec)
if multiframe:
print(f"[✓] Object/Anomaly detected at {timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec")
else:
print(f"[✓] Object/Anomaly detected at {timestamp_sec} sec")
elif reply == "no":
if multiframe:
print(f"[ ] No Object/Anomaly at {timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec")
else:
print(f"[ ] No Object/Anomaly at {timestamp_sec} sec")
else:
print(f"RESPONSE ({timestamp_sec} sec): {reply}")
except Exception as e:
print(f"[!] Error at frame {i} ({batch_timestamps[0]} sec): {e}")
return matched_timestamps
def detect_event_in_video(
video_path: str,
event_description: str,
every_n_seconds: float = 2.0,
max_frames: int = 20,
model: str = "meta-llama/llama-4-scout-17b-16e-instruct",
):
multiframe: bool = True
sampler = VideoFrameSampler(video_path)
interpreter = LlamaImageDetector(
GROQ_API_KEY,
model=model,
prompt_template=LlamaPromptTemplates.event_detection_prompt_template
)
# explainer = LLamaImageExplainer(GROQ_API_KEY, model=model)
frames_base64 = sampler.sample_frames(
every_n_seconds=every_n_seconds,
output_format="base64",
max_frames=max_frames,
multiframe=multiframe,
)
matched_timestamps = []
for i, frame_base64 in enumerate(frames_base64):
timestamp_sec = i * every_n_seconds * 4 if multiframe else i * every_n_seconds
timestamp_sec = round(timestamp_sec, 5)
image = f"data:image/jpeg;base64,{frame_base64}"
try:
reply = interpreter.inference(image, event_description)
if reply.lower() == "yes":
matched_timestamps.append(timestamp_sec)
print(f"[✓] Event detected at {timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec")
elif reply == "no":
print(f"[ ] No event at {timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec")
# explanation = explainer.explain(image)
# print(f"EXPLANATION: {explanation}")
else:
print(f"RESPONSE ({timestamp_sec}-{timestamp_sec+every_n_seconds*4} sec): {reply}")
except Exception as e:
print(f"[!] Error at frame {i} ({timestamp_sec}-{timestamp_sec+every_n_seconds*4}s): {e}")
return matched_timestamps
def transcribe_audio(filename):
audio_path = convert_video_to_audio_moviepy(filename)
client = Groq(api_key=GROQ_API_KEY)
with open(audio_path, "rb") as file:
transcription = None
try:
transcription = client.audio.transcriptions.create(
file=(audio_path, file.read()),
model="whisper-large-v3",
prompt='Make sure to transcribe any sound effects in the background of the audio, to make it accessible for deaf audiences.',
response_format="verbose_json", # Optional
timestamp_granularities = ["segment"],
language="en", # Optional
temperature=0.0 # Optional
)
except Exception as e:
print(f"Error {e}")
for segment in transcription.segments:
print(segment['start'], "-", segment['end'], ': ')
print(segment['text'])
return transcription.segments
def convert_video_to_audio_moviepy(video_file, output_ext="mp3"):
"""Converts video to audio using MoviePy library
that uses `ffmpeg` under the hood"""
filename, ext = os.path.splitext(video_file)
clip = VideoFileClip(video_file)
if clip.audio:
clip.audio.write_audiofile(f"{filename}.{output_ext}")
else:
raise Exception(f"clip.audio is None. {clip.audio}")
return (f"{filename}.{output_ext}")
if __name__ == "__main__":
detected_timestamps = detection_in_video(
video_path="sample_data/IMG_5362.MOV",
prompt_input="""
There must be someone wearing a bandana.
""",
method="anomaly",
every_n_seconds=1,
max_frames=40,
)
# transcribe_audio('sample_data/IMG_5362.MOV')
print(detected_timestamps)