realsense相机图像获取。我现在在使用Realsense D405相机,操作系统Ubuntu 20.04,Python 3.10,我希望能实时预览RGB和depth图像(这个可以直接使用opencv预览,有其他更好的也是可以的),同时又不和计算任务冲突,我希望通过本机socket通信实现,server程序实时读取图像并显示,同时能够接受socket连接请求,可以把最新的数据发送给client,client每当想要使用 realsense D405实时数据的时候,就向server请求就好了。这样设计程序独立比较好调试,相比使用多线程。
realsense_server.py
# realsense_server.py (Match RealSense Viewer Settings)
import socket
import pickle
import cv2
import numpy as np
import pyrealsense2 as rs
from threading import Lock
from datetime import datetime
latest_data = None
data_lock = Lock()
HOST = '127.0.0.1'
PORT = 9999
# === 初始化滤波器链 ===
decimation = rs.decimation_filter()
decimation.set_option(rs.option.filter_magnitude, 2)
spatial = rs.spatial_filter()
spatial.set_option(rs.option.filter_magnitude, 2)
spatial.set_option(rs.option.filter_smooth_alpha, 0.5)
spatial.set_option(rs.option.filter_smooth_delta, 20)
spatial.set_option(rs.option.holes_fill, 0) # Disabled in Spatial Filter
temporal = rs.temporal_filter()
temporal.set_option(rs.option.filter_smooth_alpha, 0.4)
temporal.set_option(rs.option.filter_smooth_delta, 20)
#temporal.set_option(rs.option.persistence_control, 8) # Valid in 2/1as
hole_filling = rs.hole_filling_filter()
hole_filling.set_option(rs.option.holes_fill, 2) # Farthest from all
# === Colorizer ===
colorizer = rs.colorizer()
colorizer.set_option(rs.option.visual_preset, 0) # Dynamic (Jet colormap)
def apply_denoise(depth_frame):
"""应用与 RealSense Viewer 完全一致的后处理流程"""
# filtered = decimation.process(depth_frame)
filtered = spatial.process(depth_frame)
filtered = temporal.process(filtered)
filtered = hole_filling.process(filtered)
return filtered
def start_realsense():
global latest_data
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
pipeline.start(config)
try:
while True:
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
depth_frame = frames.get_depth_frame()
if not color_frame or not depth_frame:
continue
color_image = np.asanyarray(color_frame.get_data())
depth_raw = np.asanyarray(depth_frame.get_data())
# 应用完整去噪链
depth_denoised_frame = apply_denoise(depth_frame)
depth_denoised = np.asanyarray(depth_denoised_frame.get_data())
# 渲染用于显示
depth_colormap = np.asanyarray(colorizer.colorize(depth_denoised_frame).get_data())
# 显示 RGB + Denoised Depth
images = np.hstack((color_image, depth_colormap))
cv2.imshow('Realsense Server (RGB | Denoised Depth)', images)
if cv2.waitKey(1) == ord('q'):
break
# 存储数据供 client 使用
with data_lock:
latest_data = {
'rgb': color_image,
'depth': depth_denoised,
'timestamp': datetime.now().isoformat()
}
finally:
pipeline.stop()
cv2.destroyAllWindows()
# === Socket Server ===
def handle_client(conn):
try:
with data_lock:
if latest_data is not None:
payload = {
'rgb': latest_data['rgb'],
'depth': latest_data['depth'],
'timestamp': latest_data['timestamp']
}
data = pickle.dumps(payload)
conn.sendall(len(data).to_bytes(4, 'big'))
conn.sendall(data)
except Exception as e:
print(f"[Server] Client error: {e}")
finally:
conn.close()
def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print(f"[Server] Listening on {HOST}:{PORT}")
import threading
cam_thread = threading.Thread(target=start_realsense, daemon=True)
cam_thread.start()
try:
while True:
conn, addr = server_socket.accept()
client_thread = threading.Thread(target=handle_client, args=(conn,))
client_thread.start()
except KeyboardInterrupt:
print("\n[Server] Shutting down...")
finally:
server_socket.close()
if __name__ == '__main__':
start_server()
realsense_client.py
# realsense_client.py (Live Preview Mode)
import socket
import pickle
import cv2
import numpy as np
def get_latest_frame(host='127.0.0.1', port=9999):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
raw_len = s.recv(4)
data_len = int.from_bytes(raw_len, 'big')
data = b''
while len(data) < data_len:
data += s.recv(data_len - len(data))
return pickle.loads(data)
if __name__ == '__main__':
print("[Client] Press 'q' to quit")
while True:
try:
data = get_latest_frame()
rgb = data['rgb']
depth = data['depth']
depth_gray = (depth / 16).clip(0, 255).astype(np.uint8)
depth_gray_bgr = cv2.cvtColor(depth_gray, cv2.COLOR_GRAY2BGR)
images = np.hstack((rgb, depth_gray_bgr))
cv2.imshow('Client Live: RGB | Depth (Gray)', images)
if cv2.waitKey(1) == ord('q'):
break
except Exception as e:
print(f"[Client] Error: {e}")
break
cv2.destroyAllWindows()
print("[Client] Exited.")

被折叠的 条评论
为什么被折叠?



