-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.py
More file actions
447 lines (418 loc) · 15.7 KB
/
example.py
File metadata and controls
447 lines (418 loc) · 15.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import cv2
import base64
from openai import OpenAI, AzureOpenAI
import os
import numpy as np
import json
import dotenv
import time
import argparse
import openai
# Resize the image while keeping aspect ratio
def image_resize_for_vlm(frame, inter=cv2.INTER_AREA):
height, width = frame.shape[:2]
aspect_ratio = width / height
max_short_side = 768
max_long_side = 2000
if aspect_ratio > 1:
new_width = min(width, max_long_side)
new_height = int(new_width / aspect_ratio)
if new_height > max_short_side:
new_height = max_short_side
new_width = int(new_height * aspect_ratio)
else:
new_height = min(height, max_long_side)
new_width = int(new_height * aspect_ratio)
if new_width > max_short_side:
new_width = max_short_side
new_height = int(new_width / aspect_ratio)
resized_frame = cv2.resize(
frame, (new_width, new_height), interpolation=inter)
return resized_frame
# Extract JSON part from the response
def extract_json_part(text):
text = text.strip().replace(" ", "").replace("\n", "")
try:
start = text.index('{"points":')
text_json = text[start:].strip()
end = text_json.index('}') + 1
text_json = text_json[:end].strip()
return text_json
except ValueError:
raise ValueError("JSON part not found in the response")
# Perform scene understanding on the frame
def scene_understanding(credentials, frame, prompt_message):
frame = image_resize_for_vlm(frame)
_, buffer = cv2.imencode(".jpg", frame)
base64Frame = base64.b64encode(buffer).decode("utf-8")
PROMPT_MESSAGES = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt_message
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64Frame}",
"detail": "high"
},
}
]
},
]
if len(credentials["AZURE_OPENAI_API_KEY"]) == 0:
client_gpt4v = OpenAI(
api_key=credentials["OPENAI_API_KEY"]
)
params = {
"model": "gpt-4o",
"messages": PROMPT_MESSAGES,
"max_tokens": 200,
"temperature": 0.1,
"top_p": 0.5,
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
}
else:
client_gpt4v = AzureOpenAI(
api_version="2024-02-01",
azure_endpoint=credentials["AZURE_OPENAI_ENDPOINT"],
api_key=credentials["AZURE_OPENAI_API_KEY"]
)
params = {
"model": credentials["AZURE_OPENAI_DEPLOYMENT_NAME"],
"messages": PROMPT_MESSAGES,
"max_tokens": 200,
"temperature": 0.1,
"top_p": 0.5,
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
}
count = 0
while True:
if count > 5:
raise Exception("Failed to get response from Azure OpenAI")
try:
result = client_gpt4v.chat.completions.create(**params)
response_json = extract_json_part(result.choices[0].message.content)
break
except openai.BadRequestError as e:
print(e)
print('Bad Request error.')
return None, None
except openai.RateLimitError as e:
print(e)
print('Rate Limit. Waiting for 5 seconds...')
time.sleep(5)
count += 1
except openai.APIStatusError as e:
print(e)
print('APIStatusError. Waiting for 1 second...')
time.sleep(1)
count += 1
except Exception as e:
print(e)
print('Other error. Waiting for 1 second...')
time.sleep(1)
count += 1
json_dict = json.loads(response_json, strict=False)
if len(json_dict['points']) == 0:
return None
if len(json_dict['points']) > 1:
print("Warning: More than one point detected")
return json_dict['points'][0], result.choices[0].message.content
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter)
return resized
# Create a grid of frames
def create_frame_grid(video_path, center_time, interval, grid_size):
spacer = 0
video = cv2.VideoCapture(video_path)
fps = video.get(cv2.CAP_PROP_FPS)
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
center_frame = int(center_time * fps)
interval_frames = int(interval * fps)
num_frames = grid_size**2
half_num_frames = num_frames // 2
frame_indices = [max(0,
min(center_frame + i * interval_frames,
total_frames - 1)) for i in range(-half_num_frames,
half_num_frames + 1)]
frames = []
actual_indices = []
for index in frame_indices:
video.set(cv2.CAP_PROP_POS_FRAMES, index)
success, frame = video.read()
if success:
frame = image_resize(frame, width=200)
frames.append(frame)
actual_indices.append(index)
else:
print(f"Warning: Frame {index} not found")
print(f"Total frames: {total_frames}")
video.set(cv2.CAP_PROP_POS_FRAMES, 0)
success, frame = video.read()
frame = image_resize(frame, width=200)
frame = frame * 0
frames.append(frame)
actual_indices.append(index)
video.release()
if len(frames) < grid_size**2:
raise ValueError("Not enough frames to create the grid.")
frame_height, frame_width = frames[0].shape[:2]
grid_height = grid_size * frame_height + (grid_size - 1) * spacer
grid_width = grid_size * frame_width + (grid_size - 1) * spacer
grid_img = np.ones((grid_height, grid_width, 3), dtype=np.uint8) * 255
for i in range(grid_size):
for j in range(grid_size):
index = i * grid_size + j
frame = frames[index]
cX, cY = frame.shape[1] // 2, frame.shape[0] // 2
max_dim = int(min(frame.shape[:2]) * 0.5)
overlay = frame.copy()
if render_pos == 'center':
circle_center = (cX, cY)
else:
circle_center = (frame.shape[1] - max_dim // 2, max_dim // 2)
cv2.circle(overlay, circle_center,
max_dim // 2, (255, 255, 255), -1)
alpha = 0.3
frame = cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0)
cv2.circle(frame, circle_center, max_dim // 2, (255, 255, 255), 2)
font_scale = max_dim / 50
text_size = cv2.getTextSize(
str(index + 1), cv2.FONT_HERSHEY_SIMPLEX, font_scale, 2)[0]
if render_pos == 'center':
text_x = cX - text_size[0] // 2
text_y = cY + text_size[1] // 2
else:
text_x = frame.shape[1] - text_size[0] // 2 - max_dim // 2
text_y = text_size[1] // 2 + max_dim // 2
cv2.putText(frame, str(index + 1), (text_x, text_y),
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 0), 2)
y1 = i * (frame_height + spacer)
y2 = y1 + frame_height
x1 = j * (frame_width + spacer)
x2 = x1 + frame_width
grid_img[y1:y2, x1:x2] = frame
return grid_img, actual_indices
def add_text_with_background(
frame,
text,
position,
font,
font_scale,
font_color,
font_thickness,
bg_color):
text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
text_x, text_y = position
top_left = (text_x - 10, text_y - text_size[1] - 10)
bottom_right = (text_x + text_size[0] + 10, text_y + 10)
cv2.rectangle(frame, top_left, bottom_right, bg_color, -1)
cv2.putText(frame, text, (text_x, text_y), font, font_scale,
font_color, font_thickness, cv2.LINE_AA)
# Annotate the video with task times
def trim_video_with_annotations(
video_path,
start_time,
end_time,
text,
output_path,
buffer=0.5):
"""Trim and annotate video with specified start and end times and text."""
if os.path.exists(output_path):
return
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Could not open video file {video_path}")
return
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
start_frame = int(start_time * fps)
end_frame = int(end_time * fps)
cap.set(cv2.CAP_PROP_POS_FRAMES, max(0, start_frame - int(buffer * fps)))
while cap.isOpened():
ret, frame = cap.read()
if not ret or cap.get(
cv2.CAP_PROP_POS_FRAMES) > end_frame + int(buffer * fps):
break
if start_frame <= cap.get(cv2.CAP_PROP_POS_FRAMES) <= end_frame:
add_text_with_background(
frame,
text,
(10,
height - 10),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0,
0,
255),
2,
(255,
255,
255))
out.write(frame)
cap.release()
out.release()
# Process each task in parallel
def process_task(
credentials,
video_path,
action,
center_time,
interval,
fps,
grid_size,
search_anchor,
iter_num=4):
"""Process a task to identify the start or end of an action in a video."""
prompt_start = (
f"I will show an image sequence of human cooking. "
f"I have annotated the images with numbered circles. "
f"Choose the number that is closest to the moment when the ({action}) has started. "
f"You are a five-time world champion in this game. "
f"Give a one sentence analysis of why you chose those points (less than 50 words). "
f"If you consider that the action is not in the video, please choose the number -1. "
f"Provide your answer at the end in a json file of this format: {{\"points\": []}}"
)
prompt_end = (
f"I will show an image sequence of human cooking. "
f"I have annotated the images with numbered circles. "
f"Choose the number that is closest to the moment when the ({action}) has ended. "
f"You are a five-time world champion in this game. "
f"Give a one sentence analysis of why you chose those points (less than 50 words). "
f"If you consider that the action has not ended yet, please choose the number -1. "
f"Provide your answer at the end in a json file of this format: {{\"points\": []}}"
)
prompt_message = prompt_start if search_anchor == 'start' else prompt_end
for iter_idx in range(iter_num): # Iterate to narrow down the time
image, used_frame_indices = create_frame_grid(
video_path, center_time, interval, grid_size)
print(used_frame_indices)
if iter_idx == 0:
cv2.imwrite(
os.path.join(
output_folder,
f"grid_image_sample.png"),
image)
description, reason = scene_understanding(
credentials, image, prompt_message)
print(reason)
if description:
if description == -1:
return None
if int(description) - 1 > len(used_frame_indices) - 1:
print("Warning: Invalid frame index selected")
print(f"Selected frame index: {description}")
# description is 1-indexed
index_specified = max(
min(int(description) - 1, len(used_frame_indices) - 1), 0)
selected_frame_index = used_frame_indices[index_specified]
center_time = selected_frame_index / fps # Convert frame index back to time
print(
f"Selected frame index: {selected_frame_index}, sample time duration: {interval}")
interval /= 2
if int(interval * fps) == 0:
break
return center_time
def convert_video(video_file_path: str, action: str, credentials, grid_size: int):
video = cv2.VideoCapture(video_file_path)
fps = video.get(cv2.CAP_PROP_FPS)
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Total frames: {total_frames}")
duration = float(total_frames) / fps
center_time = duration / 2
interval = duration / (grid_size**2 - 1)
result_start = process_task(
credentials,
video_file_path,
action,
center_time,
interval,
fps,
grid_size,
search_anchor='start')
if result_start is None:
return None, None
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)
) - int(result_start * fps)
duration = float(total_frames) / fps
center_time = duration / 2 + result_start
interval = max(duration / (grid_size**2 - 1), 1.0 / fps)
result_end = process_task(
credentials,
video_file_path,
action,
center_time,
interval,
fps,
grid_size,
search_anchor='end')
if result_end is None:
return None, None
video.release()
return result_start, result_end
parser = argparse.ArgumentParser()
parser.add_argument("--credentials", help="credentials file")
parser.add_argument("--grid", help="grid size", default=3)
parser.add_argument(
"--video_path",
help="video path",
default="sample_video/sample.mp4")
parser.add_argument(
"--action",
help="action label",
default="grabbing towards the can")
pargs, unknown = parser.parse_known_args()
credentials = dotenv.dotenv_values(pargs.credentials)
required_keys = ["OPENAI_API_KEY", "AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT"]
if not all(key in credentials for key in required_keys):
raise ValueError("Required keys are missing in the credentials file")
render_pos = 'topright' # center or topright
grid_size = int(pargs.grid)
video_path = pargs.video_path
action = pargs.action
folder_name = action.replace(" ", "_")
output_folder = f"results/{folder_name}"
os.makedirs(output_folder, exist_ok=True)
if __name__ == "__main__":
if os.path.exists(video_path):
print(f"Processing {video_path}")
start_time, completed_time = convert_video(
video_path, action, credentials, grid_size)
print(f"Start time: {start_time}, End time: {completed_time}")
if start_time is not None and completed_time is not None:
output_file_name = f"{
action.replace(
' ',
'_')}_segment_{
round(
start_time,
2)}_{
round(
completed_time,
2)}.mp4"
output_file_path = os.path.join(output_folder, output_file_name)
trim_video_with_annotations(
video_path,
start_time,
completed_time,
action,
output_file_path)