16 import pyrealsense2
as rs
21 from collections
import defaultdict
22 from realsense_device_manager
import DeviceManager
23 from calibration_kabsch
import PoseEstimation
24 from helper_functions
import get_boundary_corners_2D
25 from measurement_task
import calculate_boundingbox_points, calculate_cumulative_pointcloud, visualise_measurements
30 L515_resolution_width = 1024
31 L515_resolution_height = 768
34 resolution_width = 1280
35 resolution_height = 720
38 dispose_frames_for_stablisation = 30
46 L515_rs_config = rs.config()
47 L515_rs_config.enable_stream(rs.stream.depth, L515_resolution_width, L515_resolution_height, rs.format.z16, L515_frame_rate)
48 L515_rs_config.enable_stream(rs.stream.infrared, 0, L515_resolution_width, L515_resolution_height, rs.format.y8, L515_frame_rate)
49 L515_rs_config.enable_stream(rs.stream.color, resolution_width, resolution_height, rs.format.bgr8, frame_rate)
51 rs_config = rs.config()
52 rs_config.enable_stream(rs.stream.depth, resolution_width, resolution_height, rs.format.z16, frame_rate)
53 rs_config.enable_stream(rs.stream.infrared, 1, resolution_width, resolution_height, rs.format.y8, frame_rate)
54 rs_config.enable_stream(rs.stream.color, resolution_width, resolution_height, rs.format.bgr8, frame_rate)
57 device_manager =
DeviceManager(rs.context(), rs_config, L515_rs_config)
58 device_manager.enable_all_devices()
61 for frame
in range(dispose_frames_for_stablisation):
62 frames = device_manager.poll_frames()
64 assert( len(device_manager._available_devices) > 0 )
67 Calibrate all the available devices to the world co-ordinates. 68 For this purpose, a chessboard printout for use with opencv based calibration process is needed. 72 intrinsics_devices = device_manager.get_device_intrinsics(frames)
75 chessboard_params = [chessboard_height, chessboard_width, square_size]
78 calibrated_device_count = 0
79 while calibrated_device_count < len(device_manager._available_devices):
80 frames = device_manager.poll_frames()
81 pose_estimator =
PoseEstimation(frames, intrinsics_devices, chessboard_params)
82 transformation_result_kabsch = pose_estimator.perform_pose_estimation()
83 object_point = pose_estimator.get_chessboard_corners_in3d()
84 calibrated_device_count = 0
85 for device_info
in device_manager._available_devices:
86 device = device_info[0]
87 if not transformation_result_kabsch[device][0]:
88 print(
"Place the chessboard on the plane where the object needs to be detected..")
90 calibrated_device_count += 1
93 transformation_devices={}
94 chessboard_points_cumulative_3d = np.array([-1,-1,-1]).transpose()
95 for device_info
in device_manager._available_devices:
96 device = device_info[0]
97 transformation_devices[device] = transformation_result_kabsch[device][1].
inverse()
98 points3D = object_point[device][2][:,object_point[device][3]]
99 points3D = transformation_devices[device].apply_transformation(points3D)
100 chessboard_points_cumulative_3d = np.column_stack( (chessboard_points_cumulative_3d,points3D) )
104 chessboard_points_cumulative_3d = np.delete(chessboard_points_cumulative_3d, 0, 1)
107 print(
"Calibration completed... \nPlace the box in the field of view of the devices...")
111 2: Measurement and display 112 Measure the dimension of the object using depth maps from multiple RealSense devices 113 The information from Phase 1 will be used here 118 device_manager.enable_emitter(
True)
121 device_manager.load_settings_json(
"./HighResHighAccuracyPreset.json")
124 extrinsics_devices = device_manager.get_depth_to_color_extrinsics(frames)
127 calibration_info_devices = defaultdict(list)
128 for calibration_info
in (transformation_devices, intrinsics_devices, extrinsics_devices):
129 for key, value
in calibration_info.items():
130 calibration_info_devices[key].append(value)
135 frames_devices = device_manager.poll_frames()
146 except KeyboardInterrupt:
147 print(
"The program was interupted by the user. Closing the program...")
150 device_manager.disable_streams()
151 cv2.destroyAllWindows()
154 if __name__ ==
"__main__":
pose inverse(const pose &a)
def calculate_boundingbox_points(point_cloud, calibration_info_devices, depth_threshold=0.01)
static std::string print(const transformation &tf)
def calculate_cumulative_pointcloud(frames_devices, calibration_info_devices, roi_2d, depth_threshold=0.01)
def visualise_measurements(frames_devices, bounding_box_points_devices, length, width, height)
def get_boundary_corners_2D(points)