/processor.py

https://github.com/blackeagle01/Abnormal_Event_Detection
Python | 120 lines | 44 code | 34 blank | 42 comment | 4 complexity | c40d81855d9db8cd5001e6ab4d0242f8 MD5 | raw file
  1. ''' This module extracts frames a Video
  2. performs preprocessing on the frames and stores them in a Numpy array for
  3. furthur use by the spatiotemporal autoencoder
  4. ___________________________________________________________________
  5. Dependencies: ffmpeg
  6. If you dont have ffmpeg installed:
  7. Install it with :
  8. 1. sudo apt-get install ffmpeg for Linux Users
  9. 2. brew install ffmpeg for macOS
  10. __________________________________________________________________
  11. Usage:
  12. python3 processor.py video_dir_path time_in_seconds_to_extract_one_frame
  13. eg;python3 processor.py ./train 5 will search for train directory and for each video in train directory
  14. It will extract 1 frame every 5 seconds and store it.
  15. __________________________________________________________
  16. Author: Harsh Tiku
  17. '''
  18. from keras.preprocessing.image import img_to_array,load_img
  19. import numpy as np
  20. import glob
  21. import os
  22. from scipy.misc import imresize
  23. import argparse
  24. imagestore=[]
  25. parser=argparse.ArgumentParser(description='Source Video path')
  26. parser.add_argument('source_vid_path',type=str)
  27. parser.add_argument('fps',type=int)
  28. args=parser.parse_args()
  29. video_source_path= args.source_vid_path
  30. fps=args.fps
  31. def create_dir(path):
  32. if not os.path.exists(path):
  33. os.makedirs(path)
  34. def remove_old_images(path):
  35. filelist = glob.glob(os.path.join(path, "*.png"))
  36. for f in filelist:
  37. os.remove(f)
  38. def store(image_path):
  39. img=load_img(image_path)
  40. img=img_to_array(img)
  41. #Resize the Image to (227,227,3) for the network to be able to process it.
  42. img=imresize(img,(227,227,3))
  43. #Convert the Image to Grayscale
  44. gray=0.2989*img[:,:,0]+0.5870*img[:,:,1]+0.1140*img[:,:,2]
  45. imagestore.append(gray)
  46. #List of all Videos in the Source Directory.
  47. videos=os.listdir(video_source_path)
  48. print("Found ",len(videos)," training video")
  49. #Make a temp dir to store all the frames
  50. create_dir(video_source_path+'/frames')
  51. #Remove old images
  52. remove_old_images(video_source_path+'/frames')
  53. framepath=video_source_path+'/frames'
  54. for video in videos:
  55. os.system( 'ffmpeg -i {}/{} -r 1/{} {}/frames/%03d.jpg'.format(video_source_path,video,fps,video_source_path))
  56. images=os.listdir(framepath)
  57. for image in images:
  58. image_path=framepath+ '/'+ image
  59. store(image_path)
  60. imagestore=np.array(imagestore)
  61. a,b,c=imagestore.shape
  62. #Reshape to (227,227,batch_size)
  63. imagestore.resize(b,c,a)
  64. #Normalize
  65. imagestore=(imagestore-imagestore.mean())/(imagestore.std())
  66. #Clip negative Values
  67. imagestore=np.clip(imagestore,0,1)
  68. np.save('training.npy',imagestore)
  69. #Remove Buffer Directory
  70. os.system('rm -r {}'.format(framepath))