-
Notifications
You must be signed in to change notification settings - Fork 5
/
processor.py
139 lines (95 loc) · 3.49 KB
/
processor.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
''' This module extracts frames a Video
performs preprocessing on the frames and stores them in a Numpy array for
furthur use by the spatiotemporal autoencoder
___________________________________________________________________
Dependencies: ffmpeg
If you dont have ffmpeg installed:
Install it with :
1. sudo apt-get install ffmpeg for Linux Users
2. brew install ffmpeg for macOS
__________________________________________________________________
Usage:
python3 processor.py video_dir_path time_in_seconds_to_extract_one_frame
eg;python3 processor.py ./train 5 will search for train directory and for each video in train directory
It will extract 1 frame every 5 seconds and store it.
__________________________________________________________
Author: Harsh Tiku
'''
import cv2
import ffmpeg
from keras.preprocessing.image import img_to_array, load_img
import numpy as np
import glob
import os
from PIL import Image
# from scipy.misc import imresize
import argparse
imagestore = []
# parser=argparse.ArgumentParser(description='Source Video path')
# parser=argparse.ArgumentParser('train')
# parser.add_argument('train')
# parser.add_argument('fps',type=int)
# parser.add_argument(3)
# args=parser.parse_args()
# video_source_path= args.source_vid_path
# fps=args.fps
video_source_path = 'train'
fps = 5
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def remove_old_images(path):
filelist = glob.glob(os.path.join(path, "*.png"))
for f in filelist:
os.remove(f)
def store(image_path):
img = load_img(image_path)
img = img_to_array(img)
# Resize the Image to (227,227,3) for the network to be able to process it.
# frame = Image.fromarray(frame).resize(size=(227, 227, 3))
img = Image.fromarray(img).resize(size=(227, 227, 3))
# Convert the Image to Grayscale
gray = 0.2989 * img[:, :, 0] + 0.5870 * img[:, :, 1] + 0.1140 * img[:, :, 2]
imagestore.append(gray)
# List of all Videos in the Source Directory.
videos = os.listdir(video_source_path)
print("Found ", len(videos), " training video")
# Make a temp dir to store all the frames
create_dir(video_source_path + '/frames')
# Remove old images
remove_old_images(video_source_path + '/frames')
framepath = video_source_path + '/frames'
# for video in videos:
# # os.system("ffmpeg -i {}/{} -r 1/{} {}/frames/%03d.jpg".format(video_source_path, video, fps, video_source_path))
# os.system(f"ffmpeg -i {video_source_path}/{video} -r 0.25 output_%04d.jpg")
# images = os.listdir(framepath)
# for image in images:
# image_path = framepath + '/' + image
# store(image_path)
currentframe = 0
for video in videos:
cap = cv2.VideoCapture(video)
print("cap is {}".format(cap))
while (True):
ret, frame = cap.read()
if ret:
# "ffmpeg -i {}/{} -r 1/{} {}/frames/%03d.jpg".format(video_source_path,video,fps,video_source_path)
x = 'train/frames/' + str(currentframe) + '.jpg'
# print(x)
cv2.imwrite(x, frame)
# store_inarray(x,frame)
store(x)
currentframe += 1
else:
break
imagestore = np.array(imagestore)
a, b, c = imagestore.shape
# Reshape to (227,227,batch_size)
imagestore.resize(b, c, a)
# Normalize
imagestore = (imagestore - imagestore.mean()) / (imagestore.std())
# Clip negative Values
imagestore = np.clip(imagestore, 0, 1)
np.save('training.npy', imagestore)
# Remove Buffer Directory
os.system('rm -r {}'.format(framepath))