-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
36 lines (35 loc) · 1.1 KB
/
preprocess.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
import cv2
import sys
import glob
import os
def video2frame(filename, split_ratio=0.9):
vidcap = cv2.VideoCapture(filename + '.mp4')
image_folder = filename + '/frames/'
counter = 0
while(True):
if counter % 100 == 0:
print(counter)
success,image = vidcap.read()
if success == True:
cv2.imwrite(image_folder + str(counter).zfill(5) + '.png', image)
counter += 1
else:
break
def crop_images(filename):
image_folder = filename + '/frames/'
frames = glob.glob(os.path.join(image_folder, '*.png'))
counter = 0
for name in frames:
if counter % 100 == 0:
print(counter)
img = cv2.imread(name)
crop_img = img[180:350, 70:570]
#print(name.split('/')[3:])
tmp = name.split('/')
#print('/'.join(tmp[:2]) + '/frames_c/' + tmp[3])
cv2.imwrite('/'.join(tmp[:2]) + '/frames_c/' + tmp[3], crop_img)
#print(len(img), len(img[0]), len(img[0][0]))
counter += 1
if __name__ == '__main__':
video2frame(sys.argv[1])
crop_images(sys.argv[1])