-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_3.py
103 lines (77 loc) · 2.39 KB
/
a_3.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
import yaml
import requests
class VideoFrame:
def __init__(self, data: bytes, frame: int):
self.data = data
self.meta = dict(
frame=frame,
size=None,
detector=Detector(self.data)
)
def __repr__(self):
return f'{self.meta}'
def resize(self, x, y):
"""resize image"""
return
class VideoProcessor:
def __init__(self, filename: str = None, video: bytes = None):
self.filename = filename
self.data = video
self.meta = None
self.frames = None
def download_video(self, url: str) -> bytes:
self.data = requests.get(url).content
self.meta = self.extract_meta(self.data)
return True
def extract_meta(self, video: bytes) -> dict:
"""extra metadata from video"""
return dict(
size=None,
codec=None,
fps=None,
exif=None
)
def extract_frames(self, video: bytes) -> [VideoFrame]:
return [VideoFrame]
def set_framerate(self, fps: int):
"""reduce to N fps"""
original_framerate = self.meta['fps']
# pick every N number of frames
n = original_framerate / fps
self.frames = self.frames[::n]
return True
def toYaml(self) -> yaml:
meta = [(self.filename, x.meta) for x in self.frames]
return yaml.dump(meta)
def toFile(self, filename: str):
with open(filename, 'w') as f:
f.write(f'{self.toYaml()}')
return True
def trim(self, start: int, end: int):
"""trim to start and end time (in seconds)"""
start_frame = self.meta['fps'] * start
end_frame = self.meta['fps'] * end
self.frames = self.frames[start_frame:end_frame]
return True
class Detector:
def __init__(self, frame: VideoFrame):
self.frame = frame
self.meta = dict(
bounding_box=self.bounding_box(),
objects=self.objects()
)
def bounding_box(self, frame: bytes):
return {}
def objects(self, frame: bytes):
return {}
def __repr__(self):
return f'{self.meta}'
def main():
url = 'http://download.openalpr.com/road.mp4'
video = VideoProcessor()
video.download_video(url)
video.trim(0, 60)
video.set_framerate(5)
video.toFile('video.yaml')
if __name__ == "__main__":
main()