-
Notifications
You must be signed in to change notification settings - Fork 15
/
cam_preview.py
103 lines (83 loc) · 4.35 KB
/
cam_preview.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""Show OAK camera livestream.
Source: https://github.com/maxsitt/insect-detect
License: GNU GPLv3 (https://choosealicense.com/licenses/gpl-3.0/)
Author: Maximilian Sittinger (https://github.com/maxsitt)
Docs: https://maxsitt.github.io/insect-detect-docs/
- show downscaled LQ frames + fps in a new window (e.g. via X11 forwarding)
- optional arguments:
'-fov' default: stretch frames to square for visualization ('-fov stretch')
-> full FOV is preserved, only aspect ratio is changed (adds distortion)
optional: crop frames to square for visualization ('-fov crop')
-> FOV is reduced due to cropping of left and right side (no distortion)
'-af' set auto focus range in cm (min - max distance to camera)
-> e.g. '-af 14 20' to restrict auto focus range to 14-20 cm
'-mf' set manual focus position in cm (distance to camera)
-> e.g. '-mf 14' to set manual focus position to 14 cm
'-big' show a bigger preview window with 640x640 px size (default: 320x320 px)
-> decreases frame rate to ~3 fps (default: ~11 fps)
based on open source scripts available at https://github.com/luxonis
"""
import argparse
import time
import cv2
import depthai as dai
from utils.oak_cam import convert_cm_lens_position
# Define optional arguments
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
parser.add_argument("-fov", "--adjust_fov", choices=["stretch", "crop"], default="stretch", type=str,
help="Stretch frames to square ('stretch') and preserve full FOV or "
"crop frames to square ('crop') and reduce FOV.")
group.add_argument("-af", "--af_range", nargs=2, type=int,
help="Set auto focus range in cm (min - max distance to camera).", metavar=("CM_MIN", "CM_MAX"))
group.add_argument("-mf", "--manual_focus", type=int,
help="Set manual focus position in cm (distance to camera).", metavar="CM")
parser.add_argument("-big", "--big_preview", action="store_true",
help="Show a bigger preview window with 640x640 px size (default: 320x320 px).")
args = parser.parse_args()
# Create depthai pipeline
pipeline = dai.Pipeline()
# Create and configure color camera node and define output
cam_rgb = pipeline.create(dai.node.ColorCamera)
#cam_rgb.setImageOrientation(dai.CameraImageOrientation.ROTATE_180_DEG) # rotate image 180°
cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
if not args.big_preview:
cam_rgb.setPreviewSize(320, 320) # downscale frames -> LQ frames
else:
cam_rgb.setPreviewSize(640, 640)
if args.adjust_fov == "stretch":
cam_rgb.setPreviewKeepAspectRatio(False) # stretch frames (16:9) to square (1:1)
cam_rgb.setInterleaved(False) # planar layout
cam_rgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
cam_rgb.setFps(25) # frames per second available for auto focus/exposure
xout_rgb = pipeline.create(dai.node.XLinkOut)
xout_rgb.setStreamName("frame")
cam_rgb.preview.link(xout_rgb.input)
if args.af_range:
# Convert cm to lens position values and set auto focus range
lens_pos_min, lens_pos_max = convert_cm_lens_position((args.af_range[1], args.af_range[0]))
cam_rgb.initialControl.setAutoFocusLensRange(lens_pos_min, lens_pos_max)
if args.manual_focus:
# Convert cm to lens position value and set manual focus position
lens_pos = convert_cm_lens_position(args.manual_focus)
cam_rgb.initialControl.setManualFocus(lens_pos)
# Connect to OAK device and start pipeline in USB2 mode
with dai.Device(pipeline, maxUsbSpeed=dai.UsbSpeed.HIGH) as device:
# Create output queue to get the frames from the output defined above
q_frame = device.getOutputQueue(name="frame", maxSize=4, blocking=False)
# Set start time of recording and create counter to measure fps
start_time = time.monotonic()
counter = 0
while True:
# Get LQ frames and show in new window together with fps
if q_frame.has():
frame_lq = q_frame.get().getCvFrame()
counter += 1
fps = round(counter / (time.monotonic() - start_time), 2)
cv2.putText(frame_lq, f"fps: {fps}", (4, frame_lq.shape[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.imshow("cam_preview", frame_lq)
# Stop script and close window by pressing "Q"
if cv2.waitKey(1) == ord("q"):
break