-
Notifications
You must be signed in to change notification settings - Fork 1
/
capture_videostream.py
80 lines (58 loc) · 2.21 KB
/
capture_videostream.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Videostream capture.
======================
Displays in real-time the videostream of any recording plugged device.
Usage:
capture_videostream.py [--video-source 0 --quality hd]
Options:
video-source (int): Capture device ID.
quality (str): Input quality.
"""
from utils import *
from config import *
from argparse import ArgumentParser
__description__ = "Displays in real-time the videostream of any recording plugged device."
# Parse args.
parser = ArgumentParser(description=__description__)
parser.add_argument("-v-source", "--video-source", dest="video_source",
type=int,
default=DEVICE_CONFIG["id"],
help="Capture device identifier, default is {default}.".format(default=DEVICE_CONFIG["id"]))
parser.add_argument("-q", "--quality", dest="quality",
type=str,
default=DEVICE_CONFIG["resolution"],
help="Input quality, default is {default}.".format(default=DEVICE_CONFIG["resolution"]))
args = parser.parse_args()
def main():
"""
Main program.
:return: void.
"""
# Get the first video input.
try:
# Retrieve video input.
cap = cv.VideoCapture(args.video_source)
width, height = INPUT_RESOLUTION[args.quality]["width"], INPUT_RESOLUTION[args.quality]["height"]
# Set input properties.
cap.set(get_prop_id("FRAME_WIDTH"), width)
cap.set(get_prop_id("FRAME_HEIGHT"), height)
while True:
# Capture frame-by-frame.
ret, frame = cap.read()
if ret:
# Display the frame.
cv.imshow("Webcam videostream ({width} x {height})".format(width=width, height=height), frame)
# Exit program on the Q click.
if cv.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release capture and close windows.
cap.release()
cv.destroyAllWindows()
except Exception as ee:
print("Device {device} not found : {error}.".format(device=args.device, error=ee))
if __name__ == "__main__":
main()