shaoyent commited on
Commit
7351d1c
1 Parent(s): bae4467

Update app

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -148,8 +148,8 @@ def extract_images_and_embeds(video_id, video_path, subtitles, output, expanded=
148
  if success:
149
  img_fname = f'{video_id}_{idx:06d}'
150
  img_fpath = os.path.join(output, 'frames', img_fname + '.jpg')
151
- image = maintain_aspect_ratio_resize(image, height=350) # save frame as JPEG file
152
- cv2.imwrite( img_fpath, image) # save frame as JPEG file
153
 
154
  count += 1
155
  anno.append({
@@ -181,6 +181,7 @@ def extract_images_and_embeds(video_id, video_path, subtitles, output, expanded=
181
  'text': batch_list[i]['text'],
182
  'image_filepath': batch_list[i]['image_filepath'],
183
  'start_time': batch_list[i]['start_time'],
 
184
  })
185
  batch_list = []
186
 
@@ -195,6 +196,7 @@ def extract_images_and_embeds(video_id, video_path, subtitles, output, expanded=
195
  'text': batch_list[i]['text'],
196
  'image_filepath': batch_list[i]['image_filepath'],
197
  'start_time': batch_list[i]['start_time'],
 
198
  })
199
 
200
  with open(os.path.join(output, 'annotations.json'), 'w') as fh:
@@ -203,7 +205,9 @@ def extract_images_and_embeds(video_id, video_path, subtitles, output, expanded=
203
  with open(os.path.join(output, 'embeddings.pkl'), 'wb') as fh:
204
  pickle.dump(embeddings, fh)
205
 
206
- def run_query(video_id, text_query, path='/tmp'):
 
 
207
 
208
  embeddings_filepath = os.path.join(path, 'embeddings.pkl')
209
  faiss_filepath = os.path.join(path, 'faiss_index.pkl')
@@ -228,7 +232,14 @@ def run_query(video_id, text_query, path='/tmp'):
228
  print('Running FAISS search')
229
  _, I = faiss_index.search(emb_query, 6)
230
 
231
- clip_images = [embeddings[idx]['image_filepath'] for idx in I[0]]
 
 
 
 
 
 
 
232
  transcripts = [f"({embeddings[idx]['start_time']}) {embeddings[idx]['text']}" for idx in I[0]]
233
  return clip_images, transcripts
234
 
@@ -253,12 +264,12 @@ def get_video_id_from_url(video_url):
253
  return url.path.split('/')[2]
254
  if url.path[:3] == '/v/':
255
  return url.path.split('/')[2]
256
-
257
  return None
258
 
259
 
260
  def process(video_url, text_query):
261
- tmp_dir = os.path.join(os.getcwd(), 'cache')
262
  video_id = get_video_id_from_url(video_url)
263
  output_dir = os.path.join(tmp_dir, video_id)
264
  video_file = download_video(video_url, path=output_dir)
@@ -303,4 +314,4 @@ with gr.Blocks() as demo:
303
  outputs=[video_player, gallery],
304
  )
305
 
306
- demo.launch()
 
148
  if success:
149
  img_fname = f'{video_id}_{idx:06d}'
150
  img_fpath = os.path.join(output, 'frames', img_fname + '.jpg')
151
+ # image = maintain_aspect_ratio_resize(image, height=350) # save frame as JPEG file
152
+ # cv2.imwrite( img_fpath, image) # save frame as JPEG file
153
 
154
  count += 1
155
  anno.append({
 
181
  'text': batch_list[i]['text'],
182
  'image_filepath': batch_list[i]['image_filepath'],
183
  'start_time': batch_list[i]['start_time'],
184
+ 'frame_no': frame_no,
185
  })
186
  batch_list = []
187
 
 
196
  'text': batch_list[i]['text'],
197
  'image_filepath': batch_list[i]['image_filepath'],
198
  'start_time': batch_list[i]['start_time'],
199
+ 'frame_no': frame_no,
200
  })
201
 
202
  with open(os.path.join(output, 'annotations.json'), 'w') as fh:
 
205
  with open(os.path.join(output, 'embeddings.pkl'), 'wb') as fh:
206
  pickle.dump(embeddings, fh)
207
 
208
+ def run_query(video_path, text_query, path='/tmp'):
209
+
210
+ vidcap = cv2.VideoCapture(video_path)
211
 
212
  embeddings_filepath = os.path.join(path, 'embeddings.pkl')
213
  faiss_filepath = os.path.join(path, 'faiss_index.pkl')
 
232
  print('Running FAISS search')
233
  _, I = faiss_index.search(emb_query, 6)
234
 
235
+ clip_images = []
236
+ for idx in I[0]:
237
+ frame_no = embeddings[idx]['frame_no']
238
+ vidcap.set(1, frame_no) # added this line
239
+ success, image = vidcap.read()
240
+ clip_images.append(image)
241
+
242
+ # clip_images = [embeddings[idx]['image_filepath'] for idx in I[0]]
243
  transcripts = [f"({embeddings[idx]['start_time']}) {embeddings[idx]['text']}" for idx in I[0]]
244
  return clip_images, transcripts
245
 
 
264
  return url.path.split('/')[2]
265
  if url.path[:3] == '/v/':
266
  return url.path.split('/')[2]
267
+
268
  return None
269
 
270
 
271
  def process(video_url, text_query):
272
+ tmp_dir = os.environ.get('TMPDIR', '/tmp')
273
  video_id = get_video_id_from_url(video_url)
274
  output_dir = os.path.join(tmp_dir, video_id)
275
  video_file = download_video(video_url, path=output_dir)
 
314
  outputs=[video_player, gallery],
315
  )
316
 
317
+ demo.launch(share=True)