-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam_stream.py
More file actions
99 lines (75 loc) · 3.38 KB
/
webcam_stream.py
File metadata and controls
99 lines (75 loc) · 3.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Copyright (C) Square Factory SA - All Rights Reserved.
This source code is protected under international copyright law. All rights
reserved and protected by the copyright holders.
This file is confidential and only available to authorized individuals with the
permission of the copyright holders. If you encounter this file and do not have
permission, please contact the copyright holders and delete this file.
"""
import argparse
import asyncio
import time
import warnings
import cv2
import imutils
import numpy as np
from rich.live import Live
from rich.spinner import Spinner
from i2_client import I2Client
parser = argparse.ArgumentParser()
parser.add_argument("--url", type=str, help="", required=True)
parser.add_argument("--access_uuid", type=str, help="") # DEPRACATED
parser.add_argument("--frame_rate", type=int, help="", default=15) # DEPRACATED
parser.add_argument("--resize_width", type=int, help="", default=None) # DEPRACATED
parser.add_argument("--access-key", type=str, help="")
parser.add_argument("--frame-rate", type=int, help="", default=15)
parser.add_argument("--resize-width", type=int, help="", default=None)
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()
if args.access_uuid is not None:
warnings.warn("--access_uuid is deprecated, use --access-key", DeprecationWarning)
args.access_key = args.access_uuid
if args.access_uuid is None and args.access_key is None:
raise ValueError("You have to provide an access key with --access-key")
async def main():
"""Main async function."""
cam = cv2.VideoCapture(0)
prev = 0
async with I2Client(args.url, args.access_key, args.debug) as client:
spinner = Spinner("dots2", "connecting...")
with Live(spinner, refresh_per_second=20):
durations = []
while True:
# 1. get webcam frame
time_elapsed = time.time() - prev
check, frame = cam.read()
if time_elapsed < 1.0 / args.frame_rate:
# force the webcam frame rate so the bottleneck is the
# inference, not the camera performance.
continue
prev = time.time()
if args.resize_width is not None:
frame = imutils.resize(frame, width=args.resize_width)
# 2. inference
start = time.time()
outputs = await client.async_inference(frame)
durations.append(time.time() - start)
# 3. show
spinner.text = (
f"send + infer + receive: {durations[-1]:.4f} secs "
+ f"(mean: {np.mean(durations):.4f}, std: {np.std(durations):.4f}, "
+ f"min: {np.min(durations):.4f}, max: {np.max(durations):.4f})"
)
success, output = outputs[0]
if not success:
raise RuntimeError(output)
h, w, _ = frame.shape
frame = cv2.resize(frame, (w * 2, h * 2))
output = cv2.resize(output, (w * 2, h * 2))
concatenate_imgs = np.concatenate((frame, output), axis=1)
cv2.imshow("original / inference ", concatenate_imgs)
key = cv2.waitKey(1)
if key == 27:
break
cam.release()
cv2.destroyAllWindows()
asyncio.run(main())