forked from bafana5/ava-dataset-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideos_to_images.py
129 lines (106 loc) · 4.62 KB
/
videos_to_images.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
# __version__ = '0.0'
# __author__ = 'Meshack B. Shabalala'
import os
import cv2
import numpy as np
import pandas as pd
# from os.path import basename
from ava_dataset_downloader import dir_exists
class VideoExtractor:
r"""
"""
def __init__(self, inputvideopath, outputframepath = 'trainimages'):
r"""
- frame_size = (416, 416)
- inputvideopath
- outputframepath
"""
self.frame_size = (416, 416)
self.inputvideopath = inputvideopath
self.outputframepath = outputframepath
self.ava_dataset_path = "ava_dataset"
self.train_annotations_path = "annotations"
def extractAll(self):
r"""
"""
pass
def extractFrame(self):
r"""
"""
pass
def extractMultipleFrames(self):
r"""
"""
# Read annotaions and images
annotationsfile = "ava_train_v2.1.csv"
annotationsfilepath = os.path.join(self.ava_dataset_path, annotationsfile)
df = pd.read_csv(annotationsfilepath, delimiter=',')
data = df[df.columns[:]].values
# Collect the video paths
path = "train"
video_file_paths = []
videos_in_dir = os.listdir(path)
for index, file in enumerate(videos_in_dir):
file_path = os.path.join(path, file)
video_file_paths.append(file_path)
# video_names = df['video_id'].unique()
video_names = videos_in_dir
if not dir_exists(self.train_annotations_path):
return
if not dir_exists(self.outputframepath):
return
file = os.path.join(self.train_annotations_path, 'train.txt')
f = open(file, "a")
for video_name, video_path in zip(video_names, video_file_paths):
df2 = df.loc[df.video_id == video_name[:-4], :] # remove .mp4
video_timestamp = df2['middle_frame_timestamp'].unique()
for timestep in video_timestamp:
df3 = df2.loc[df2.middle_frame_timestamp == timestep, :]
videodata = df3[df3.columns[:-1]].values
cap = cv2.VideoCapture(video_path)
# Check if camera opened successfully
if (cap.isOpened()):
# fps = cap.get(cv2.CAP_PROP_FPS)
# print("Frames per second using video.get(cv2.CAP_PROP_FPS): {:2.2f}".format(fps))
# Read until video is completed
# while(cap.isOpened()):
# Capture frame-by-frame
for indx, i in enumerate(videodata):
if indx is 0:
sec = i[1]
w, h = (416, 416)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
cap.set(cv2.CAP_PROP_POS_MSEC, sec*1000)
success, image = cap.read()
if success:
# Read frame
imagename = self.outputframepath + "/" + video_name[:-4] + "_%06d.jpg" % sec
image = cv2.resize(
image, (w, h), interpolation=cv2.INTER_AREA)
# save frame as JPEG file
cv2.imwrite(imagename, image)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
f.write("%s %i,%i,%i,%i,%i" % (imagename, int(
w*i[2]), int(w*i[3]), int(w*i[4]), int(w*i[5]), int(i[6])))
else:
f.write(" %i,%i,%i,%i,%i" % (
int(w*i[2]), int(w*i[3]), int(w*i[4]), int(w*i[5]), int(i[6])))
f.write("\n")
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
# framename = val_arou_txt[:-20]+"_%06d.jpg"% labels[indx,0]
# f.write("%s,%.15f,%.15f\n"%(framename, labels[indx, 1], labels[indx, 2]))
f.close()
def main():
vid_extractor = VideoExtractor('train', outputframepath='trainimages')
vid_extractor.extractMultipleFrames()
if __name__ == '__main__':
main()