/examples/viddec/gen_vid_thunks.py

https://github.com/StanfordSNR/gg
Python | 93 lines | 67 code | 20 blank | 6 comment | 9 complexity | 5aedb07ec5dcbcd80f489a5efd5168ff MD5 | raw file
  1. #! /usr/bin/python
  2. import sys
  3. import os
  4. import stat
  5. import glob
  6. import argparse
  7. import subprocess as sp
  8. from timeit import default_timer as now
  9. SUFFIX_TO_CLEAR = ['jpg', 'out']
  10. out_script = 'gen_thunks.sh'
  11. def get_args():
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument('--video', '-v', type=str, required=False,
  14. dest='vidToProcess', default='all_vid.txt', help='file with all vid hashes')
  15. parser.add_argument('--libin', '-l', type=str, required=False,
  16. dest='listaticbin', default='li25-static', help='Label Image Binary (li25-static or li-static)')
  17. return parser.parse_args()
  18. def get_hash(filename):
  19. hash_proc = sp.Popen(['gg-hash', filename], stdout=sp.PIPE)
  20. hash_out = hash_proc.communicate()[0].strip().decode('utf-8')
  21. if hash_out == '':
  22. print("Error getting hash of %s. Now exiting..." % filename)
  23. sys.exit(1)
  24. return hash_out
  25. def collect_files(all_files):
  26. cf_cmd = ['gg-collect'] + all_files
  27. cf_proc = sp.Popen(cf_cmd, stdout=sp.PIPE, stderr=sp.PIPE)
  28. cf_out = cf_proc.communicate()[1].strip().decode('utf-8')
  29. if cf_out == '':
  30. print("Error calling gg-collect...")
  31. sys.exit(1)
  32. def clear_chunks():
  33. to_delete = []
  34. for stc in SUFFIX_TO_CLEAR:
  35. to_delete.extend(glob.glob('*.' + stc))
  36. for todel in to_delete:
  37. if os.path.exists(todel):
  38. os.remove(todel)
  39. def main(args):
  40. all_hashes = args.vidToProcess
  41. listatic = args.listaticbin
  42. all_vid_raw = open(all_hashes, 'r').readlines()
  43. # Get hashes of all files
  44. ffmpeg_hash = get_hash('ffmpeg')
  45. listatic_hash = get_hash(listatic)
  46. incept_hash = get_hash('inception_v3_2016_08_28_frozen.pb')
  47. inet_hash = get_hash('imagenet_slim_labels.txt')
  48. # Collect all files
  49. all_files_to_col = ['ffmpeg', listatic, 'inception_v3_2016_08_28_frozen.pb', \
  50. 'imagenet_slim_labels.txt']
  51. collect_files(all_files_to_col)
  52. # Build command
  53. cmd = []
  54. if listatic == 'li-static':
  55. cmd.append('./gen_vid_thunks')
  56. else:
  57. cmd.append('./gen_vid_thunks25')
  58. cmd.extend([ffmpeg_hash, listatic_hash, incept_hash, inet_hash])
  59. all_chunks = 60*(all_vid_raw[4:8])
  60. for ac in all_chunks:
  61. cmd.append(ac.strip())
  62. # Create output script
  63. fd = open(out_script, 'w')
  64. fd.write('#!/bin/bash -e\n')
  65. fd.write(' '.join(cmd))
  66. fd.close()
  67. # Make executable
  68. st = os.stat(out_script)
  69. os.chmod(out_script, st.st_mode | stat.S_IEXEC)
  70. if __name__ == '__main__':
  71. clear_chunks()
  72. parsed_args = get_args()
  73. main(parsed_args)