-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
executable file
·42 lines (30 loc) · 949 Bytes
/
common.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
import imutils
import cv2
from contextlib import contextmanager
def clock():
return cv2.getTickCount() / cv2.getTickFrequency()
@contextmanager
def Timer(msg):
print(msg, '...',)
start = clock()
try:
yield
finally:
print("%.2f s" % ((clock() - start)))
class VideoWriter:
def __init__(self, capture, out_file="/tmp/video.mp4", codec="X264"):
self.fourcc = cv2.VideoWriter_fourcc(*codec)
self.out_file = out_file
self.capture = capture
self.writer = None
def write(self, frame):
if self.writer is None:
h, w = frame.shape[:2]
fps = self.capture.get(cv2.CAP_PROP_FPS)
self.writer = cv2.VideoWriter(self.out_file, self.fourcc, fps, (w, h), True)
self.writer.write(frame)
def __del__(self):
self.writer.release()
def prepare_frame(frame):
frame = imutils.resize(frame, width=640)
return frame