-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_read_images_from_video.py
81 lines (59 loc) · 1.57 KB
/
extract_read_images_from_video.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
import cv2 #print(cv2.__version__)
import os
"""
Main Functions
video = cv2.VideoCapture(File_path)
success, image = vidcap.read()
imwrite(filename, img[, params])
"""
FILE_VID = "test/video1.avi"
FILE_DIR = "./first_test_extract_images_from_video"
AVI = '.avi'
JPEG = '.jpg'
#VideoCapture(File_path) : Read the video(.XXX format)
#rtype: tuple(boolean, numpy.ndarray)
vidcap = cv2.VideoCapture(FILE_VID)
"""
In [16]: vidcap.read()[1].shape
Out[16]: (240, 320, 3)
"""
success, image = vidcap.read()
"""
In [25]: image.shape
Out[25]: (240, 320, 3)
"""
def createFolder(file_name):
try:
if not os.path.exists(file_name):
os.makedirs(file_name)
except OSError:
print ('Error: Creating directory of data')
count = 0
success = True
tmp = 0
STRIDE = 10
createFolder(FILE_DIR)
while success:
if count % STRIDE == 0:
tmp +=1
file_im = "{}/{}_frame_{}{}".format(FILE_DIR, FILE_VID[:-len(AVI)], tmp, JPEG)
cv2.imwrite(file_im, image)
success, image = vidcap.read()
count += 1
vidcap.release()
#Rappel
#function_object = lambda arguments : expression
add = lambda x, y : x + y
add(1, 2) # out: 3
#map(function_object, iterable1, iterable2,...)
list(map(add, [1, 2, 3, 4], [1, 2, 3, 4]))
#filter(function_object, iterable)
import random
l1 = [1, 5, 8]
l2 = [0, 0, 1]
l3 = list(zip(l1, l2))
random.shuffle(l3)
trainx, trainy = list(zip(*l3))
# /t/ with [p], [k], [f], [th], [gh], [s], [sh], [ch]
# /ID/ with [t], [d]
# /d/ with other