-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlf.py
248 lines (225 loc) · 9.79 KB
/
lf.py
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
import cv2
import os
import time
import json
import numpy as np
import datetime
import argparse
from test_cases import test_cases
from outputs import VideoResult
from frame_processor import vlm_processor,visualize_results
from dino_processor import dino_processor
import torch
from typing import Union
def process_video_owl(
video_path:str,
text_queries=None,
image_queries=None,
interval=6,
result_dir=None,
max_frame=None,
result_video=None,
):
if text_queries is None and image_queries is None:
print('Suply either image query or lang query')
return
if text_queries is not None and image_queries is not None:
print('Suply either image query or lang query')
return
if text_queries is not None:
query_type='lang'
else:
query_type='image'
print(f"Searching with {query_type} Queries: {text_queries if text_queries else image_queries} in video {video_path}")
device='cuda' if torch.cuda.is_available() else 'cpu'
all_frame_results=[]
vlm_processor.model.to(device)
print("Using " + device)
if query_type=='image':
# convert BGR to RGB
image_queries_cv2=[cv2.imread(name) for name in image_queries]
image_queries_cv2=[cv2.cvtColor(i,cv2.COLOR_BGR2RGB) for i in image_queries_cv2]
video = cv2.VideoCapture(video_path)
if result_video is not None:
#codec must be avc1 (h.264) to allowing playing in <video> element of html
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer=cv2.VideoWriter(result_video,fourcc,round(30.0/interval),(int(video.get(3)),int(video.get(4))))
if result_dir is not None:
os.makedirs(f'./results/{result_dir}',exist_ok=True)
frame_count=0
print(f"Number of frames: {int(video.get(cv2.CAP_PROP_FRAME_COUNT))} Processing interval: {interval}")
while video.isOpened():
ret, frame = video.read()
if not ret:
break
#Very important: OpenCV read an image as BGR yet pytorch models assume an image is RGB
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
if max_frame is not None:
if max_frame<=frame_count:
break
if frame_count%interval==0:
start=time.perf_counter()
if query_type=='lang':
result=vlm_processor.process_image(frame,text_queries,device)
else:
result=vlm_processor.image_query(frame,image_queries_cv2,device)
result['frame']=frame_count
all_frame_results.append(result)
end=time.perf_counter()
print(f"Results of frame {frame_count}: {result['scores']} Time:{end-start}s")
if query_type=='lang':
class_string=", ".join([f"{index+1}->{c}" for index,c in enumerate(text_queries)])
format_string=f"Classes: [{class_string}]"
visualized_image=visualize_results(frame,result,text_queries,format_string)
else:
format_string=f"Image: {', '.join(image_queries)}"
visualized_image=visualize_results(frame,result,image_queries,format_string)
if result_dir is not None:
result_path=f'./results/{result_dir}/frame_{frame_count}.jpg'
cv2.imwrite(result_path,visualized_image)
if result_video is not None:
video_writer.write(visualized_image)
else:
all_frame_results.append({'frame':frame_count})
frame_count=frame_count+1
if result_video is not None:
video_writer.release()
video.release()
return all_frame_results
def process_video_gdino(
video_path:str,
text_queries=None,
image_queries=None,
interval=6,
result_dir=None,
max_frame=None,
result_video=None,
):
if text_queries is None and image_queries is None:
print('Suply either image query or lang query')
return
if text_queries is not None and image_queries is not None:
print('Suply either image query or lang query')
return
if text_queries is not None:
query_type='lang'
else:
query_type='image'
print(f"Searching with {query_type} Queries: {text_queries if text_queries else image_queries} in video {video_path}")
device='cuda' if torch.cuda.is_available() else 'cpu'
all_frame_results=[]
print("Using " + device)
video = cv2.VideoCapture(video_path)
if result_video is not None:
#codec must be avc1 (h.264) to allowing playing in <video> element of html
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer=cv2.VideoWriter(result_video,fourcc,round(30.0/interval),(int(video.get(3)),int(video.get(4))))
if result_dir is not None:
os.makedirs(f'./results/{result_dir}',exist_ok=True)
frame_count=0
print(f"Number of frames: {int(video.get(cv2.CAP_PROP_FRAME_COUNT))} Processing interval: {interval}")
while video.isOpened():
ret, frame = video.read()
if not ret:
break
#Very important: OpenCV read an image as BGR yet pytorch models assume an image is RGB
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
if max_frame is not None:
if max_frame<=frame_count:
break
if frame_count%interval==0:
start=time.perf_counter()
if query_type=='lang':
result,visualized_image=dino_processor.process_image(frame,text_queries,visualize=result_video is not None)
else:
result,visualized_image=dino_processor.image_query(frame,image_queries_cv2)
result['frame']=frame_count
all_frame_results.append(result)
end=time.perf_counter()
print(f"Results of frame {frame_count}: {result['scores']} Time:{end-start}s")
if result_dir is not None:
result_path=f'./results/{result_dir}/frame_{frame_count}.jpg'
cv2.imwrite(result_path,visualized_image)
if result_video is not None:
video_writer.write(visualized_image)
else:
all_frame_results.append({'frame':frame_count})
frame_count=frame_count+1
if result_video is not None:
video_writer.release()
video.release()
return all_frame_results
processor_mapping={
'owl-vit':process_video_owl,
'gdino':process_video_gdino,
}
#TODO: create QueryExecutor class
def run_video(
video_name:str,
queries:list[str],
run_type:str,
interval:int=3,
max_frame:Union[int,None]=None,
visualize_all:bool=False,
top_k:Union[int,None]=None,
chunk_size:Union[int,None]=None,
model_name='owl-vit',
):
print(f"Using model {model_name}")
os.makedirs('results',exist_ok=True)
video_raw_name=video_name.split('/')[-1]
str_max_frame=''
if max_frame is not None:
str_max_frame=f'_frame_{max_frame}'
#append time stamp into name to avoid duplication
current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y%m%d%H%M")
if visualize_all:
result_video_name=f'./results/{video_raw_name}{str_max_frame}_{formatted_time}_{run_type}.mp4'
else:
result_video_name=None
result_json_name=f'./results/{video_raw_name}{str_max_frame}_{formatted_time}_{run_type}.json'
print(f'Result Video Path: {result_video_name}')
if run_type not in ['image','lang']:
print("Invalid run_type: must be image or lang ")
return
process_video_method=processor_mapping[model_name]
if run_type=='image':
result=process_video_method(video_name,image_queries=queries,interval=interval,result_dir=None,max_frame=max_frame,result_video=result_video_name)
elif run_type=='lang':
result=process_video_method(video_name,text_queries=queries,interval=interval,result_dir=None,max_frame=max_frame,result_video=result_video_name)
result={
'query':queries,
'type':run_type,
'result':result,
}
with open(result_json_name,'w') as f:
print(f"Result Json: {result_json_name}")
json.dump(result,f)
if top_k is not None:
video_result=VideoResult()
video_result.from_data_dict(result)
sorted_chunks_ma=video_result.sort_logits_chunks_ma(chunk_size)
result_dirs=video_result.dump_top_k_chunks(video_name,sorted_chunks_ma,top_k)
return result_dirs
else:
return []
#If you are using CUHK CSE slurm cluster
#export SLURM_CONF=/opt1/slurm/gpu-slurm.conf
#srun --gres=gpu:1 -w gpu39 --pty /bin/bash
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('--video_name',type=str)
parser.add_argument('--query_index',type=int)
parser.add_argument('--max_frame',type=int,default=None)
parser.add_argument('--interval',type=int,default=10,help="the number of frame between every model execution")
parser.add_argument('--visualize_all', action='store_true', default=False,help='visualize all bounding boxes of the video')
parser.add_argument('--top_k',type=int,default=None,help="top k chunks to output, if None, no chunk will be output")
parser.add_argument('--chunk_size',type=int,default=None,help="Number of frames in a chunk") # 2 seconds
parser.add_argument('--model_name',type=str,default=None,choices=['owl-vit','gdino'])
args=parser.parse_args()
video_name=args.video_name
query=test_cases[args.query_index]['object']
query_type=test_cases[args.query_index]['type']
results_dirs=run_video(video_name,query,query_type,interval=args.interval,visualize_all=args.visualize_all,top_k=args.top_k,chunk_size=args.chunk_size,model_name=args.model_name)
print(f"Results saved to {results_dirs}")