-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetCamStream.py
71 lines (64 loc) · 2.71 KB
/
GetCamStream.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
import cv2
import argparse
import sys
from utils.perf import get_meter
CV_WINDOW_NAME = 'Camera'
def Get_args():
'''
Parses arguments from the command line
'''
parser = argparse.ArgumentParser(description = 'Get video from camera source')
parser.add_argument( '--cam_width', metavar = 'cam resolution width',
type=int, default = 640,
help = 'Camera capture resolution width. default=640')
parser.add_argument( '--cam_height', metavar = 'cam resolution height',
type=int, default = 480,
help = 'Camera capture resolution height. default=480')
parser.add_argument( '-s', '--cam_source', metavar = 'Cam source v4linux or MIPI CSI',
type=str, default = '/dev/video0',
help = 'Camera source. Default = /dev/video0')
parser.add_argument('-m', '--use-MIPI',
dest='MIPI', action='store_true', default=False,
help='Iput source is MIPI CSI. Default = False')
parser.add_argument('-v', '--use-video',
dest='video', action='store_true', default=False,
help='Iput source is video. Default = False')
return parser.parse_args()
def main():
ARGS = Get_args()
if ARGS.MIPI:
print("Use MIPI CSI camera: {}\n".format(ARGS.MIPI))
cam = cv2.VideoCapture(ARGS.cam_source, cv2.CAP_GSTREAMER)
elif ARGS.video:
print("Use video: {}\n".format(ARGS.video))
cam = cv2.VideoCapture(ARGS.cam_source)
else:
print("Use v4l2 camera")
cam = cv2.VideoCapture(ARGS.cam_source, cv2.CAP_V4L2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, ARGS.cam_width)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, ARGS.cam_height)
# Create display window
cv2.namedWindow(CV_WINDOW_NAME)
overall_meter = get_meter("background")
while (cam.isOpened()):
overall_meter.begin()
ret_val, frame = cam.read()
overall_meter.end()
if (not ret_val):
print("No image from camera, exiting...")
break
cv2.putText(frame, "(Overall Speed) {:.1f} FPS".format(overall_meter.smooth_speed()), (10,50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (38,0,255), 1, cv2.LINE_AA)
prop_val = cv2.getWindowProperty(CV_WINDOW_NAME, cv2.WND_PROP_ASPECT_RATIO)
if (prop_val < 0.0):
print('Window closed')
break
cv2.imshow(CV_WINDOW_NAME, frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
print('User press quit')
break
else:
print("Cannot get cam stream!")
cam.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
sys.exit(main())