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 resolution_width = 1280
31 resolution_height = 720
33 dispose_frames_for_stablisation = 30
41 rs_config = rs.config()
42 rs_config.enable_stream(rs.stream.depth, resolution_width, resolution_height, rs.format.z16, frame_rate)
43 rs_config.enable_stream(rs.stream.infrared, 1, resolution_width, resolution_height, rs.format.y8, frame_rate)
44 rs_config.enable_stream(rs.stream.color, resolution_width, resolution_height, rs.format.bgr8, frame_rate)
48 device_manager.enable_all_devices()
51 for frame
in range(dispose_frames_for_stablisation):
52 frames = device_manager.poll_frames()
54 assert( len(device_manager._available_devices) > 0 )
57 Calibrate all the available devices to the world co-ordinates. 58 For this purpose, a chessboard printout for use with opencv based calibration process is needed. 62 intrinsics_devices = device_manager.get_device_intrinsics(frames)
65 chessboard_params = [chessboard_height, chessboard_width, square_size]
68 calibrated_device_count = 0
69 while calibrated_device_count < len(device_manager._available_devices):
70 frames = device_manager.poll_frames()
71 pose_estimator =
PoseEstimation(frames, intrinsics_devices, chessboard_params)
72 transformation_result_kabsch = pose_estimator.perform_pose_estimation()
73 object_point = pose_estimator.get_chessboard_corners_in3d()
74 calibrated_device_count = 0
75 for device
in device_manager._available_devices:
76 if not transformation_result_kabsch[device][0]:
77 print(
"Place the chessboard on the plane where the object needs to be detected..")
79 calibrated_device_count += 1
82 transformation_devices={}
83 chessboard_points_cumulative_3d = np.array([-1,-1,-1]).transpose()
84 for device
in device_manager._available_devices:
85 transformation_devices[device] = transformation_result_kabsch[device][1].
inverse()
86 points3D = object_point[device][2][:,object_point[device][3]]
87 points3D = transformation_devices[device].apply_transformation(points3D)
88 chessboard_points_cumulative_3d = np.column_stack( (chessboard_points_cumulative_3d,points3D) )
92 chessboard_points_cumulative_3d = np.delete(chessboard_points_cumulative_3d, 0, 1)
95 print(
"Calibration completed... \nPlace the box in the field of view of the devices...")
99 2: Measurement and display 100 Measure the dimension of the object using depth maps from multiple RealSense devices 101 The information from Phase 1 will be used here 106 device_manager.enable_emitter(
True)
109 device_manager.load_settings_json(
"./HighResHighAccuracyPreset.json")
112 extrinsics_devices = device_manager.get_depth_to_color_extrinsics(frames)
115 calibration_info_devices = defaultdict(list)
116 for calibration_info
in (transformation_devices, intrinsics_devices, extrinsics_devices):
117 for key, value
in calibration_info.items():
118 calibration_info_devices[key].append(value)
123 frames_devices = device_manager.poll_frames()
134 except KeyboardInterrupt:
135 print(
"The program was interupted by the user. Closing the program...")
138 device_manager.disable_streams()
139 cv2.destroyAllWindows()
142 if __name__ ==
"__main__":
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)