forked from StoneT2000/lerobot-sim2real
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestrealsense.py
More file actions
56 lines (44 loc) · 1.9 KB
/
testrealsense.py
File metadata and controls
56 lines (44 loc) · 1.9 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
## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
#####################################################
## librealsense tutorial #1 - Accessing depth data ##
#####################################################
# First import the library
import pyrealsense2 as rs
import numpy as np
import cv2
try:
# Create a context object. This object owns the handles to all connected realsense devices
pipeline = rs.pipeline()
# Configure streams
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
while True:
# This call waits until a new coherent set of frames is available on a device
# Calls to get_frame_data(...) and get_frame_timestamp(...) on a device will return stable values until wait_for_frames(...) is called
frames = pipeline.wait_for_frames()
depth = frames.get_depth_frame()
color = frames.get_color_frame()
if not depth or not color: continue
# Convert frames to numpy arrays
depth_image = np.asanyarray(depth.get_data())
color_image = np.asanyarray(color.get_data())
# Apply colormap to depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
# Display both streams in one window
cv2.imshow('Color and Depth Streams', images)
# Break loop with 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Clean up
pipeline.stop()
cv2.destroyAllWindows()
exit(0)
except Exception as e:
print(e)
pass