-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdata.py
More file actions
83 lines (60 loc) · 1.93 KB
/
data.py
File metadata and controls
83 lines (60 loc) · 1.93 KB
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
import cv2 as cv
import argparse
def record(fname):
print("recording ", fname)
cam = cv.VideoCapture(0)
# Use whatever width and height possible
frame_width = int(cam.get(cv.CAP_PROP_FRAME_WIDTH))
frame_height = int(cam.get(cv.CAP_PROP_FRAME_HEIGHT))
fourcc = cv.VideoWriter_fourcc(*'mp4v')
out = cv.VideoWriter(fname, fourcc, 60.0, (frame_width, frame_height))
while True:
_, img = cam.read()
img = cv.flip(img, 1)
out.write(img)
cv.imshow('Recording', img)
if cv.waitKey(1) & 0xFF == ord('q'):
break
out.release()
cam.release()
cv.destroyAllWindows()
print("recording complete. shutting down.")
def replay(fname):
print("replaying", fname)
cap = cv.VideoCapture(fname)
print("captured")
if (not cap.isOpened()):
print("Error opening video file")
return
print("waiting to open")
while cap.isOpened(): # and (cv.waitKey(0) & 0xFF != ord('q')):
ret, img = cap.read()
# replay is completed when the video capture no longer has any frames to read.
if ret:
cv.imshow('Camera', img)
else:
break
print("img", img.size)
cap.release()
cv.destroyAllWindows()
print("replay complete", fname)
def main():
parser = argparse.ArgumentParser(
prog='data.py',
description='data collections tools'
)
parser.add_argument("-m", "--mode")
parser.add_argument("-f", "--filename")
args = parser.parse_args()
if not args.filename.endswith(".mp4"):
print(f"filename({args.filename}) must end with .mp4")
return False
if args.mode == 'replay':
replay(args.filename)
elif args.mode == "record":
record(args.filename)
else:
print(f"data mode must fall into ['replay', 'record'], provided {args.mode}")
return False
if __name__ == "__main__":
main()