sindhuhegde commited on
Commit
b015d09
1 Parent(s): c4c6512

Update app

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -72,7 +72,6 @@ def bb_intersection_over_union(boxA, boxB):
72
  return iou
73
 
74
  def track_shot(scenefaces):
75
- print("Tracking video...")
76
  iouThres = 0.5 # Minimum IOU between consecutive face detections
77
  tracks = []
78
 
@@ -128,7 +127,6 @@ def read_video(o, start_idx):
128
  return video_stream
129
 
130
  def crop_video(avi_dir, tmp_dir, track, cropfile, tight_scale=1):
131
- print("Cropping video...")
132
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
133
  vOut = cv2.VideoWriter(cropfile + '.avi', fourcc, frame_rate, (480, 270))
134
 
@@ -239,7 +237,6 @@ def inference_video(avi_dir, work_dir, padding=0):
239
  return dets
240
 
241
  def scene_detect(avi_dir, work_dir):
242
- print("Detecting scenes in the video...")
243
  video_manager = VideoManager([os.path.join(avi_dir, 'video.avi')])
244
  stats_manager = StatsManager()
245
  scene_manager = SceneManager(stats_manager)
@@ -300,38 +297,55 @@ def process_video_asd(file, sd_root, work_root, data_root, avi_dir, tmp_dir, wor
300
  command = ("ffmpeg -hide_banner -loglevel panic -y -i %s -qscale:v 2 -async 1 -r 25 %s" % (videofile,
301
  os.path.join(avi_dir,
302
  'video.avi')))
303
- output = subprocess.call(command, shell=True, stdout=None)
304
- if output != 0:
305
- return
 
306
 
307
  command = ("ffmpeg -hide_banner -loglevel panic -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 16000 %s" % (os.path.join(avi_dir,
308
  'video.avi'),
309
  os.path.join(avi_dir,
310
  'audio.wav')))
311
- output = subprocess.call(command, shell=True, stdout=None)
312
- if output != 0:
313
- return
 
314
 
315
- faces = inference_video(avi_dir, work_dir)
 
 
 
 
316
 
 
317
  try:
318
  scene = scene_detect(avi_dir, work_dir)
319
- except scenedetect.video_stream.VideoOpenFailure:
320
- return
321
-
322
 
 
323
  allscenes = []
324
- for shot in scene:
325
- if shot[1].frame_num - shot[0].frame_num >= min_track:
326
- allscenes.append(track_shot(faces[shot[0].frame_num:shot[1].frame_num]))
 
 
 
 
327
 
 
328
  alltracks = []
329
- for sc_num in range(len(allscenes)):
330
- vidtracks = []
331
- for ii, track in enumerate(allscenes[sc_num]):
332
- os.makedirs(os.path.join(crop_dir, 'scene_'+str(sc_num)), exist_ok=True)
333
- vidtracks.append(crop_video(avi_dir, tmp_dir, track, os.path.join(crop_dir, 'scene_'+str(sc_num), '%05d' % ii)))
334
- alltracks.append(vidtracks)
 
 
 
 
335
 
336
  savepath = os.path.join(work_dir, 'tracks.pckl')
337
 
@@ -344,6 +358,9 @@ def process_video_asd(file, sd_root, work_root, data_root, avi_dir, tmp_dir, wor
344
  copytree(crop_dir, sd_dest_folder)
345
  copytree(work_dir, work_dest_folder)
346
 
 
 
 
347
  @spaces.GPU(duration=140)
348
  def preprocess_video(path, result_folder, apply_preprocess, padding=20):
349
 
@@ -1205,14 +1222,13 @@ def preprocess_asd(video_path, result_folder_input):
1205
  crop_dir = os.path.join(data_dir, 'pycrop')
1206
  frames_dir = os.path.join(data_dir, 'pyframes')
1207
 
1208
- process_video_asd(file, sd_root, work_root, data_root, avi_dir, tmp_dir, work_dir, crop_dir, frames_dir)
1209
-
1210
  print("Pre-processing the input video...")
1211
- status = subprocess.call("python preprocess/inference_preprocess.py --data_dir={}/temp --sd_root={}/crops --work_root={}/metadata --data_root={}".format(result_folder_input, result_folder_input, result_folder_input, video_path), shell=True)
1212
- if status != 0:
1213
- msg = "Error in pre-processing the input video, please check the input video and try again..."
1214
- return msg
1215
-
 
1216
  return "success"
1217
 
1218
  def process_video_syncoffset(video_path, num_avg_frames, apply_preprocess):
 
72
  return iou
73
 
74
  def track_shot(scenefaces):
 
75
  iouThres = 0.5 # Minimum IOU between consecutive face detections
76
  tracks = []
77
 
 
127
  return video_stream
128
 
129
  def crop_video(avi_dir, tmp_dir, track, cropfile, tight_scale=1):
 
130
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
131
  vOut = cv2.VideoWriter(cropfile + '.avi', fourcc, frame_rate, (480, 270))
132
 
 
237
  return dets
238
 
239
  def scene_detect(avi_dir, work_dir):
 
240
  video_manager = VideoManager([os.path.join(avi_dir, 'video.avi')])
241
  stats_manager = StatsManager()
242
  scene_manager = SceneManager(stats_manager)
 
297
  command = ("ffmpeg -hide_banner -loglevel panic -y -i %s -qscale:v 2 -async 1 -r 25 %s" % (videofile,
298
  os.path.join(avi_dir,
299
  'video.avi')))
300
+ status = subprocess.call(command, shell=True, stdout=None)
301
+ if status != 0:
302
+ msg = "Error in pre-processing the video, please check the input video and try again"
303
+ return msg
304
 
305
  command = ("ffmpeg -hide_banner -loglevel panic -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 16000 %s" % (os.path.join(avi_dir,
306
  'video.avi'),
307
  os.path.join(avi_dir,
308
  'audio.wav')))
309
+ status = subprocess.call(command, shell=True, stdout=None)
310
+ if status != 0:
311
+ msg = "Error in pre-processing the video, please check the input video and try again"
312
+ return msg
313
 
314
+ try:
315
+ faces = inference_video(avi_dir, work_dir)
316
+ except:
317
+ msg = "Error in pre-processing the video, please check the input video and try again"
318
+ return msg
319
 
320
+ print("Detecting scenes in the video...")
321
  try:
322
  scene = scene_detect(avi_dir, work_dir)
323
+ except:
324
+ msg = "Error in detecting the scenes in the video, please check the input video and try again"
325
+ return msg
326
 
327
+ print("Tracking video...")
328
  allscenes = []
329
+ try:
330
+ for shot in scene:
331
+ if shot[1].frame_num - shot[0].frame_num >= min_track:
332
+ allscenes.append(track_shot(faces[shot[0].frame_num:shot[1].frame_num]))
333
+ except:
334
+ msg = "Error in pre-processing the video, please check the input video and try again"
335
+ return msg
336
 
337
+ print("Cropping video...")
338
  alltracks = []
339
+ try:
340
+ for sc_num in range(len(allscenes)):
341
+ vidtracks = []
342
+ for ii, track in enumerate(allscenes[sc_num]):
343
+ os.makedirs(os.path.join(crop_dir, 'scene_'+str(sc_num)), exist_ok=True)
344
+ vidtracks.append(crop_video(avi_dir, tmp_dir, track, os.path.join(crop_dir, 'scene_'+str(sc_num), '%05d' % ii)))
345
+ alltracks.append(vidtracks)
346
+ except:
347
+ msg = "Error in pre-processing the video, please check the input video and try again"
348
+ return msg
349
 
350
  savepath = os.path.join(work_dir, 'tracks.pckl')
351
 
 
358
  copytree(crop_dir, sd_dest_folder)
359
  copytree(work_dir, work_dest_folder)
360
 
361
+ return "success"
362
+
363
+
364
  @spaces.GPU(duration=140)
365
  def preprocess_video(path, result_folder, apply_preprocess, padding=20):
366
 
 
1222
  crop_dir = os.path.join(data_dir, 'pycrop')
1223
  frames_dir = os.path.join(data_dir, 'pyframes')
1224
 
 
 
1225
  print("Pre-processing the input video...")
1226
+ status = process_video_asd(file, sd_root, work_root, data_root, avi_dir, tmp_dir, work_dir, crop_dir, frames_dir)
1227
+
1228
+ if status != "success":
1229
+ return status
1230
+ print("Successfully pre-processed the video")
1231
+
1232
  return "success"
1233
 
1234
  def process_video_syncoffset(video_path, num_avg_frames, apply_preprocess):