-
Notifications
You must be signed in to change notification settings - Fork 0
/
realsense.py
132 lines (111 loc) · 5.05 KB
/
realsense.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import numpy as np
import cv2 as cv
import pyrealsense2 as rs
import sys
class RealSense():
'''Intel RealSense D435 image grabber'''
def __init__(self):
'''Instantiate the pipeline'''
self.pipeline = rs.pipeline()
self.profile = None
self.depth_scale = 1.
self.is_opened = False
self.frame = None
self.enable_align, self.align, self.colorizer = False, None, None
def open(self, width=1280, height=720, fps=30, use_bgr=True, enable_color=True, enable_color_align=True, enable_depth=True, enable_infrared=True, config=rs.config()):
'''Start the camera'''
config.disable_all_streams()
if enable_color:
if use_bgr:
config.enable_stream(rs.stream.color, width, height, rs.format.bgr8, fps)
else:
config.enable_stream(rs.stream.color, width, height, rs.format.rgb8, fps)
if enable_color_align:
self.align = rs.align(rs.stream.color)
self.colorizer = rs.colorizer()
self.enable_align = True
if enable_depth:
config.enable_stream(rs.stream.depth, width, height, rs.format.z16, fps)
if enable_infrared:
config.enable_stream(rs.stream.infrared, width, height, rs.format.y8, fps)
try:
self.profile = self.pipeline.start(config)
depth_sensor = self.profile.get_device().first_depth_sensor()
self.depth_scale = depth_sensor.get_depth_scale()
self.is_opened = True
return True
except RuntimeError:
return False
def close(self):
'''Stop the camera'''
if self.is_opened:
self.is_opened = False
return self.pipeline.stop()
return False
def is_open(self):
'''Check whether the camera is openend or not'''
return self.is_opened
def grab(self, timeout_sec=5):
'''Grab a set of sensor data'''
success, self.frame = self.pipeline.try_wait_for_frames(timeout_sec*1000)
return success
def get_images(self, aligning=True):
'''Retrieve all images'''
# aligning color and depth image
if aligning and self.enable_align:
frameset = self.align.process(self.frame)
color = np.asanyarray(frameset.get_color_frame().get_data())
infra = np.asanyarray(frameset.get_infrared_frame().get_data())
depth = np.asanyarray(frameset.get_depth_frame().get_data())
# Not aligning
else:
color = np.asanyarray(self.frame.get_color_frame().get_data())
infra = np.asanyarray(self.frame.get_infrared_frame().get_data())
depth = np.asanyarray(self.frame.get_depth_frame().get_data())
return color, depth, infra
def print_realsense_info(realsense:RealSense):
'''Print camera information of the RealSense camera'''
active_device = realsense.profile.get_device()
color_profile = realsense.profile.get_stream(rs.stream.color).as_video_stream_profile()
depth_profile = realsense.profile.get_stream(rs.stream.depth).as_video_stream_profile()
infra_profile = realsense.profile.get_stream(rs.stream.infrared).as_video_stream_profile()
print('* Device information')
print(f' * Model : {active_device.get_info(rs.camera_info.name)}')
print(f' * Serial : {active_device.get_info(rs.camera_info.serial_number)}')
print(f' * Firmware: {active_device.get_info(rs.camera_info.firmware_version)}')
print('')
print('* Intrinsic parameters')
print(f' * Color : {color_profile.get_intrinsics()}')
print(f' * Depth : {depth_profile.get_intrinsics()}')
print(f' * Infrared: {infra_profile.get_intrinsics()}')
print('')
print('* Extrinsic parameters')
print(f' * Depth-to-Color : {depth_profile.get_extrinsics_to(color_profile)}')
print(f' * Depth-to-Infrared: {depth_profile.get_extrinsics_to(infra_profile)}')
print('')
def test_realsense(width=1280, height=720, fps=30, max_depth = 5000, zoom=0.5):
'''Show live images of the RealSense camera'''
realsense = RealSense()
realsense.open(width, height, fps)
print_realsense_info(realsense)
sys.stdout.flush()
cv.namedWindow('RealSense Live: Color, Depth, and Infrared')
while True:
if realsense.grab():
# Get all images
color, depth, infra = realsense.get_images()
# Show the images
_, depth_color = cv.threshold(depth / max_depth * 255., 255, 255, cv.THRESH_TRUNC)
depth_color = cv.applyColorMap(depth_color.astype('uint8'), cv.COLORMAP_JET)
infra_color = cv.applyColorMap(infra, cv.COLORMAP_JET)
merge = cv.resize(np.vstack((color, depth_color, infra_color)), (0, 0), fx=zoom, fy=zoom)
cv.imshow('RealSense Live: Color, Depth, and Infrared', merge)
key = cv.waitKey(1)
if key == ord(' '): # Space
key = cv.waitKey(0)
if key == 27: # ESC
break
cv.destroyAllWindows()
realsense.close()
if __name__ == '__main__':
test_realsense()