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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| from pyorbbecsdk import * import numpy as np import cv2 import os import open3d as o3d
class Camera: def __init__(self, width=1280, height=720,fps=15): self.im_width = width self.im_height = height self.fps = fps self.intrinsic = None self.scale = None
def connect(self): """用于连接相机""" self.pipeline = Pipeline() config = Config()
profile_list = self.pipeline.get_stream_profile_list(OBSensorType.COLOR_SENSOR) color_profile = profile_list.get_default_video_stream_profile() config.enable_stream(color_profile) profile_list = self.pipeline.get_stream_profile_list(OBSensorType.DEPTH_SENSOR) assert profile_list is not None depth_profile = profile_list.get_default_video_stream_profile() assert depth_profile is not None print("color profile : {}x{}@{}_{}".format(color_profile.get_width(), color_profile.get_height(), color_profile.get_fps(), color_profile.get_format())) print("depth profile : {}x{}@{}_{}".format(depth_profile.get_width(), depth_profile.get_height(), depth_profile.get_fps(), depth_profile.get_format())) config.enable_stream(depth_profile) config.set_align_mode(OBAlignMode.SW_MODE) self.pipeline.enable_frame_sync() self.pipeline.start(config)
self.intrinsic = self.get_intrinsic() def disconnect(self): """用于断开相机""" self.pipeline.stop()
def get_frame(self): """通过流来获取color frame和depth frame""" while True: frames: FrameSet = self.pipeline.wait_for_frames(200) if frames is None: continue color_frame = frames.get_color_frame() if color_frame is None: continue depth_frame = frames.get_depth_frame() if depth_frame is None: continue if color_frame != None and depth_frame != None: break return color_frame, depth_frame
def frame2data(self, color_frame, depth_frame): """暂时没用""" width = depth_frame.get_width() height = depth_frame.get_height() scale = depth_frame.get_depth_scale() depth_data = np.frombuffer(depth_frame.get_data(), dtype=np.uint16) depth_data = depth_data.reshape((height, width))
width = color_frame.get_width() height = color_frame.get_height() color_data = np.asanyarray(color_frame.get_data(), dtype=np.uint16) return color_data.astype(np.float32), depth_data.astype(np.float32) def get_data(self): """通过流来获取color data和depth data"""
self.connect() color_frame, depth_frame = self.get_frame()
width = color_frame.get_width() height = color_frame.get_height() color_format = color_frame.get_format() data = np.asanyarray(color_frame.get_data()) color_data = cv2.imdecode(data, cv2.IMREAD_COLOR) color_data.astype(np.float32)
print('color_image.shape: ', color_data.shape) width = depth_frame.get_width() height = depth_frame.get_height() scale = depth_frame.get_depth_scale() print("===width: {}===".format(width)) print("===height: {}===".format(height)) print("===scale: {}===".format(scale)) save_dir = os.path.join(os.getcwd(), "real/intrinsic") if not os.path.exists(save_dir): os.mkdir(save_dir) filename = save_dir + "/camera_depth_scale.txt" save = np.array([scale]) np.savetxt(filename, save, delimiter=' ')
depth_data = np.frombuffer(depth_frame.get_data(), dtype=np.uint16) depth_data = depth_data.reshape((height, width)) depth_data = depth_data.astype(np.float32) * scale print('depth_image.shape: ', depth_data.shape)
depth_data = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_16U) self.disconnect()
return color_data, depth_data
def get_data_saved(self): color_data, depth_data = self.get_data()
save_image_dir = os.path.join(os.getcwd(), "real/images") if not os.path.exists(save_image_dir): os.mkdir(save_image_dir) depth_filename = save_image_dir + "/depth_{}x{}.png".format(depth_data.shape[0], depth_data.shape[1]) color_filename = save_image_dir + "/color_{}x{}.png".format(color_data.shape[0], color_data.shape[1]) cv2.imwrite(color_filename, color_data) cv2.imwrite(depth_filename, depth_data)
return color_data, depth_data
def get_intrinsic(self): """获取内参""" itsc = self.pipeline.get_camera_param() raw_intrinsic = itsc.depth_intrinsic
intrinsic = np.array([raw_intrinsic.fx, 0, raw_intrinsic.cx, 0, raw_intrinsic.fy, raw_intrinsic.cy, 0, 0, 1]).reshape(3,3) print("intrinsic: ", itsc) print('depth intrinsic: ', raw_intrinsic) print("intrinsic matrix", intrinsic) save_dir = os.path.join(os.getcwd(), "real/intrinsic") if not os.path.exists(save_dir): os.mkdir(save_dir) filename = save_dir + "/camera_itcs.txt"
np.savetxt(filename, intrinsic, delimiter=' ')
return intrinsic def visualize(self): """显示rgbd图像""" color_data, depth_data = self.get_data() depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U) depth_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET) depth_image = cv2.addWeighted(color_data, 0.5, depth_image, 0.5, 0) cv2.imshow("Depth with Color", depth_image) cv2.waitKey(500)
if __name__ == '__main__': camera = Camera() camera.visualize() color, depth = camera.get_data() print("depth.shape: ", depth.shape)
|