This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
realsense.py
79 lines (57 loc) · 2.59 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
import pyrealsense2 as rs
import numpy as np
class RSCamera:
def __init__(self):
self.pipeline = rs.pipeline()
# Dimensions for any camera of the Realsense series
self.width = 640
self.height = 480
config = rs.config()
config.enable_stream(rs.stream.color, self.width,
self.height, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, self.width,
self.height, rs.format.z16, 30)
profile = self.pipeline.start(config)
depth_sensor = profile.get_device().first_depth_sensor()
image_sensor = profile.get_device().query_sensors()[1]
# Need this to compute depth
self.depth_scale = depth_sensor.get_depth_scale()
# Enable automatic change of expose for high contrast scenes
image_sensor.set_option(rs.option.enable_auto_exposure, True)
# Get the raw frames as tensors
def get_raw_frames(self):
frames = self.pipeline.wait_for_frames()
depth = np.asanyarray(frames.get_depth_frame().get_data())
color = np.asanyarray(frames.get_color_frame().get_data())
return (color, depth)
# Get the frames as rs2.Frame objects
def get_rs_frames(self):
frames = self.pipeline.wait_for_frames()
depth = frames.get_depth_frame()
color = frames.get_color_frame()
# Get frames aligned to color as tensors
def get_raw_color_aligned_frames(self):
frames = self.pipeline.wait_for_frames()
align_to = rs.stream.color
align = rs.align(align_to)
aligned_frames = align.process(frames)
depth = np.asanyarray(aligned_frames.get_depth_frame().get_data())
color = np.asanyarray(aligned_frames.get_color_frame().get_data())
return (color, depth)
# Get frames aligned to color as rs2 frame objects
def get_rs_color_aligned_frames(self):
frames = self.pipeline.wait_for_frames()
align_to = rs.stream.color
align = rs.align(align_to)
aligned_frames = align.process(frames)
return (aligned_frames.get_color_frame(), aligned_frames.get_depth_frame())
def colorize_frame(self, depth_frame):
colorizer = rs.colorizer(color_scheme=0)
return np.asanyarray(
colorizer.colorize(depth_frame).get_data())
def deproject(self, intrinsics, x, y, depth):
return rs.rs2_deproject_pixel_to_point(intrinsics, [x, y], depth)
def project(self, intrinsics, point):
return rs.rs2_project_point_to_pixel(intrinsics, point)
def release(self):
self.pipeline.stop()