Intel RealSense深度相机项目应用——生产消费模型
近期因为项目需要,使用了intel的深度相机,此处重点记录一下如何使用Python语言完成生产消费者模型来调用相机图像,此处使用的相机型号为SR305(该相机使用的是intel实感SDK2.0,据intel介绍是通用于多数型号的)因为是第一次使用,如有理解不到位或者错误的情况,还请大家批评指正!
""" 生产消费模型(基于线程) """ import pyrealsense2 as rs import cv2 from threading import Thread from collections import deque import numpy as np import time def producer(cap, q): while True: print('producer execuation') if cap.isOpened(): start = time.time() ret, img = cap.read() q.append(img) end = time.time() print("生产图片的时间为", (end - start)) def consumer(q): while True: if len(q) == 0: pass else: img = q.pop() print('consumer execuation') cv2.imshow('img_multi_process', img) cv2.waitKey(1) if __name__ == '__main__': 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) profile = pipeline.start(config) # 表示构建的管道使用上述配置开始流传输 # 获取深度比例 depth_sensor = profile.get_device().first_depth_sensor() depth_scale = depth_sensor.get_depth_scale() align_to = rs.stream.color # 深度对齐彩色,彩色图不变,深度图变化 # align_to = rs.stream.depth # 彩色图对齐深度图,深度图不变,彩色图变化 align = rs.align(align_to) # rs.align允许我们执行深度帧与其他帧的对齐 frames = pipeline.wait_for_frames() # 等待所有配置的流生成一个帧 aligned_frames = align.process(frames) # 将深度框与颜色框对齐,aligned_frames是对齐后的帧集合 aligned_depth_frame = aligned_frames.get_depth_frame() # 从对齐后的帧集合中调取深度帧 color_frame = frames.get_color_frame() # 从帧集合中调取彩色帧 color_image = np.asanyarray(color_frame.get_data()) # 从彩色帧中取图像 depth_image = np.asanyarray(aligned_depth_frame.get_data()) # 从深度帧中取图像 # 获取相机颜色帧内参 color_profile = color_frame.get_profile() cvsprofile = rs.video_stream_profile(color_profile) color_intrin = cvsprofile.get_intrinsics() color_intrin_part = [color_intrin.ppx, color_intrin.ppy, color_intrin.fx, color_intrin.fy] # [ppx, ppy, fx, fy]fx与fy代表焦距, # ppx,ppy 相机内参矩阵主坐标 目标点实际位置换算到像素图上后的x、y方向的误差补偿(或偏置) camera = cv2.VideoCapture(0) frame_deque = deque(maxlen=10) cap = cv2.VideoCapture(0) p1 = Thread(target=producer, args=(cap, frame_deque)) p2 = Thread(target=consumer, args=(frame_deque,)) p1.start() p2.start() p1.join() p2.join() """ 生产消费模型(基于进程) """ from threading import Thread from multiprocessing import Queue, Process import cv2 import numpy as np import time import pyrealsense2 as rs def producer(q): 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) profile = pipeline.start(config) # 表示构建的管道使用上述配置开始流传输 # 获取深度比例 depth_sensor = profile.get_device().first_depth_sensor() depth_scale = depth_sensor.get_depth_scale() align_to = rs.stream.color # 深度对齐彩色,彩色图不变,深度图变化 # align_to = rs.stream.depth # 彩色图对齐深度图,深度图不变,彩色图变化 align = rs.align(align_to) # rs.align允许我们执行深度帧与其他帧的对齐 frames = pipeline.wait_for_frames() # 等待所有配置的流生成一个帧 aligned_frames = align.process(frames) # 将深度框与颜色框对齐,aligned_frames是对齐后的帧集合 aligned_depth_frame = aligned_frames.get_depth_frame() # 从对齐后的帧集合中调取深度帧 color_frame = frames.get_color_frame() # 从帧集合中调取彩色帧 color_image = np.asanyarray(color_frame.get_data()) # 从彩色帧中取图像 depth_image = np.asanyarray(aligned_depth_frame.get_data()) # 从深度帧中取图像 # 获取相机颜色帧内参 color_profile = color_frame.get_profile() cvsprofile = rs.video_stream_profile(color_profile) color_intrin = cvsprofile.get_intrinsics() color_intrin_part = [color_intrin.ppx, color_intrin.ppy, color_intrin.fx, color_intrin.fy] # [ppx, ppy, fx, fy]fx与fy代表焦距, # ppx,ppy 相机内参矩阵主坐标 目标点实际位置换算到像素图上后的x、y方向的误差补偿(或偏置) camera = cv2.VideoCapture(0) while True: print("producer execuation") res, frame = camera.read() q.put(frame) def consumer(q): while True: print("consumer execuation") img = q.get() if img is None: print("there is no image!") cv2.imshow("consumer", img) cv2.waitKey(1) if __name__ == '__main__': q = Queue(maxsize=100) p1 = Process(target=producer, args=(q, )) p2 = Process(target=consumer, args=(q, )) p1.start() p2.start() p1.join() p2.join()感谢三位大佬的文章,给予了很大的帮助,大家也可以参考 添加链接描述
添加链接描述
添加链接描述
