realsense python 操作摄像头
目录
安装
读图:
获取内参:
同时读取2个摄像头,深度图和rgb图:
同时读取两个摄像头,彩色图:
3维重建:
安装
pip install pyrealsense2
读图:
import pyrealsense2 as rs
import numpy as np
import cv2if __name__ == "__main__":# Configure depth and color streamspipeline = rs.pipeline()config = rs.config()config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)# Start streamingpipeline.start(config)try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_frames()depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not depth_frame or not color_frame:continue# Convert images to numpy arraysdepth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())# Apply colormap on depth image (image must be converted to 8-bit per pixel first)depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)# Stack both images horizontallyimages = np.hstack((color_image, depth_colormap))# Show imagescv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images)key = cv2.waitKey(1)# Press esc or 'q' to close the image windowif key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()breakfinally:# Stop streamingpipeline.stop()
获取内参:
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)pipeline.start(config)# 创建对齐对象(深度对齐颜色)
align = rs.align(rs.stream.color)try:while True:frames = pipeline.wait_for_frames()# 获取对齐帧集aligned_frames = align.process(frames)# 获取对齐后的深度帧和彩色帧aligned_depth_frame = aligned_frames.get_depth_frame()color_frame = aligned_frames.get_color_frame()# 获取颜色帧内参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]print(color_intrin_part)# [318.48199462890625, 241.16720581054688, 616.5906372070312, 616.7650146484375]if not aligned_depth_frame or not color_frame:continuefinally:pipeline.stop()
fx、fy为xy轴的焦距。ppx、ppy为xy轴的原理点
开始报错:
Traceback (most recent call last):
File "F:\biadu_down\yolov5-face-master\data\aaa.py", line 17, in <module>
frames = pipeline.wait_for_frames()
RuntimeError: Frame didn't arrive within 5000
后来就好了,结果:
[318.36590576171875, 240.8314208984375, 382.5411682128906, 382.3366394042969]
[318.36590576171875, 240.8314208984375, 382.5411682128906, 382.3366394042969]
[318.36590576171875, 240.8314208984375, 382.5411682128906, 382.3366394042969]
[318.36590576171875, 240.8314208984375, 382.5411682128906, 382.3366394042969]
同时读取2个摄像头,深度图和rgb图:
import pyrealsense2 as rs
import cv2
import numpy as np
import time
import os
import sysif __name__ == '__main__':# 确定图像的输入分辨率与帧率resolution_width = 640 # pixelsresolution_height = 480 # pixelsframe_rate = 30 # fps# 注册数据流,并对其图像align = rs.align(rs.stream.color)rs_config = rs.config()rs_config.enable_stream(rs.stream.depth, resolution_width, resolution_height, rs.format.z16, frame_rate)rs_config.enable_stream(rs.stream.color, resolution_width, resolution_height, rs.format.bgr8, frame_rate)### d435i#rs_config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, frame_rate)rs_config.enable_stream(rs.stream.infrared, 2, 640, 480, rs.format.y8, frame_rate)# check相机是不是进来了connect_device = []for d in rs.context().devices:print('Found device: ',d.get_info(rs.camera_info.name), ' ',d.get_info(rs.camera_info.serial_number))if d.get_info(rs.camera_info.name).lower() != 'platform camera':connect_device.append(d.get_info(rs.camera_info.serial_number))if len(connect_device) < 2:print('Registrition needs two camera connected.But got one.')exit()# 确认相机并获取相机的内部参数pipeline1 = rs.pipeline()rs_config.enable_device(connect_device[0])# pipeline_profile1 = pipeline1.start(rs_config)pipeline1.start(rs_config)pipeline2 = rs.pipeline()rs_config.enable_device(connect_device[1])# pipeline_profile2 = pipeline2.start(rs_config)pipeline2.start(rs_config)try:while True:# 等待数据进来frames1 = pipeline1.wait_for_frames()frames2 = pipeline2.wait_for_frames()# 将进来的RGBD数据对齐aligned_frames1 = align.process(frames1)aligned_frames2 = align.process(frames2)# 将对其的RGB—D图取出来color_frame1 = aligned_frames1.get_color_frame()depth_frame1 = aligned_frames1.get_depth_frame()color_frame2 = aligned_frames2.get_color_frame()depth_frame2 = aligned_frames2.get_depth_frame()# --------------------------------------depth_frame1 = frames1.get_depth_frame()color_frame1 = frames1.get_color_frame()depth_frame2 = frames2.get_depth_frame()color_frame2 = frames2.get_color_frame()# 数组化数据便于处理ir_frame_left1 = frames1.get_infrared_frame(1)ir_frame_right1 = frames1.get_infrared_frame(2)if not depth_frame1 or not color_frame1:continueir_frame_left2 = frames2.get_infrared_frame(1)ir_frame_right2 = frames2.get_infrared_frame(2)if not depth_frame2 or not color_frame2:continuecolor_image1 = np.asanyarray(color_frame1.get_data())depth_image1 = np.asanyarray(depth_frame1.get_data())ir_left_image1 = np.asanyarray(ir_frame_left1.get_data())ir_right_image1 = np.asanyarray(ir_frame_right1.get_data())color_image2 = np.asanyarray(color_frame2.get_data())depth_image2 = np.asanyarray(depth_frame2.get_data())ir_left_image2 = np.asanyarray(ir_frame_left2.get_data())ir_right_image2 = np.asanyarray(ir_frame_right2.get_data())depth_colormap1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image1, alpha=0.03), cv2.COLORMAP_JET)images1 = np.hstack((color_image1, depth_colormap1))depth_colormap2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image2, alpha=0.03), cv2.COLORMAP_JET)images2 = np.hstack((color_image2, depth_colormap2))cv2.imshow('RealSense1', images1)cv2.imshow('RealSense2', images2)key = cv2.waitKey(1)if key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()breakfinally:pipeline1.stop()pipeline2.stop()
同时读取两个摄像头,彩色图:
import pyrealsense2 as rs
import cv2
import numpy as np
import time
import os
import sysclass Realsense2:def __init__(self, camera_id_list=[0], camera_width=1280, camera_height=720, camera_fps=30):self.camera_width = camera_widthself.camera_height = camera_heightself.camera_fps = camera_fpsself.camera_id_list = camera_id_listdef camera_config(self):self.connect_device = []# get all realsense serial numberfor d in rs.context().devices:print('Found device: ',d.get_info(rs.camera_info.name), ' ',d.get_info(rs.camera_info.serial_number))if d.get_info(rs.camera_info.name).lower() != 'platform camera':self.connect_device.append(d.get_info(rs.camera_info.serial_number))# config realsenseself.pipeline_list = [rs.pipeline() for i in range(len(self.camera_id_list))]self.config_list = [rs.config() for i in range(len(self.camera_id_list))]if len(self.camera_id_list) == 1: # one realsenseself.config_list[0].enable_device(self.connect_device[0])self.config_list[0].enable_stream(rs.stream.depth, self.camera_width, self.camera_height, rs.format.z16,self.camera_fps)self.config_list[0].enable_stream(rs.stream.color, self.camera_width, self.camera_height, rs.format.bgr8,self.camera_fps)self.pipeline_list[0].start(self.config_list[0])elif len(self.camera_id_list) >= 2: # two realsenseif len(self.connect_device) < 2:print('Registrition needs two camera connected.But got one.Please run realsense-viewer to check your camera status.')exit()# enable configfor n, config in enumerate(self.config_list):config.enable_device(self.connect_device[n])config.enable_stream(rs.stream.depth, self.camera_width, self.camera_height, rs.format.z16,self.camera_fps)config.enable_stream(rs.stream.color, self.camera_width, self.camera_height, rs.format.bgr8,self.camera_fps)# self.config_list[n] = config# start pipelinefor n, pipeline in enumerate(self.pipeline_list):pipeline.start(self.config_list[n])def wait_frames(self, frame_id=None):'''camera_id:@ = camera number , get all frame@ = id , get specific id camera frame'''self.frame_list = [None for i in range(len(self.camera_id_list))]if frame_id != None: # give a frame idfor n, camera_id in enumerate(self.camera_id_list):self.frame_list[n] = self.pipeline_list[frame_id].wait_for_frames()else: # get all frameif len(self.camera_id_list) == 1:self.frame_list.append(self.pipeline_list[0].wait_for_frames())else:for n, camera_id in enumerate(self.camera_id_list):self.frame_list[n] = self.pipeline_list[n].wait_for_frames()def rgb_image(self, camera_id=0):color_frame = self.frame_list[camera_id].get_color_frame()color_image = np.asanyarray(color_frame.get_data())return color_imagedef depth_frame(self, frames):depth_frame = frames.get_depth_frame()return depth_framedef stop(self):for pipeline in self.pipeline_list:pipeline.stop()print("camera exit sucessfully.")if __name__ == '__main__':cap = Realsense2(camera_id_list=[0, 1], camera_width=640, camera_height=480) #cap.camera_config()while True:start = time.time()cap.wait_frames()img0 = cap.rgb_image(0)img1 = cap.rgb_image(1)cv2.imshow("img0", img0)cv2.imshow("img1", img1)# print("FPS:", 1 / (time.time() - start), "f/s")if cv2.waitKey(1) == ord("q"):breakcap.stop()
3维重建:
GitHub - AoLyu/Some-implementions-with-RGBD-camera-RealSense-D435: some personal implementation of 3d object reconstruction, recognition and pose estimation
realsense python 操作摄像头相关推荐
- python操作摄像头
python操作摄像头 前言 一.直接展示代码 总结 前言 一篇文章让你能用python来调用你笔记本的摄像头,进行拍照保存指定位置,进行更多操作. 一.直接展示代码 import os import ...
- 写字机器人开发之:python opencv linux下合作操作摄像头
2019独角兽企业重金招聘Python工程师标准>>> 设想 之前使用C#控制摄像头的,现在厌烦了windows,决定转移到linux下玩耍.感觉能够python语言特有的好入手,正 ...
- Intel Realsense D435 python multiprocessing 摄像头多进程流传输
参考文章1:python 测试multiprocessing多进程 参考文章2:Intel Realsense D435 多摄像头目标识别架构
- Intel英特尔RealSense实感深度摄像头 自校准(Self-Calibration)操作步骤讲解 D400系列适用
Viewer工具与校准纹理目标纸下载:https://download.csdn.net/download/weixin_43042683/86242590 1.下载最新版本的Viewer工具和固件 ...
- python控制摄像头拍照_python+opencv+pyqt5控制摄像头在Qlabel上显示
import cv2 import numpy as numpy from PIL import * import sys from PyQt5.QtWidgets import * from PyQ ...
- python捕获摄像头帧_Xuggler教程:帧捕获和视频创建
python捕获摄像头帧 注意:这是我们的" Xuggler开发教程 "系列的一部分. 到目前为止,在我们的Xuggler教程系列中,我们已经对视频处理的Xuggler进行了介绍, ...
- opencv启动摄像头并且操作摄像头拍照
提示:代码由pycharm实现 文章目录 前言 一.引入库 二.步骤 1.开启摄像头 2.设置一个变量方便后面保存图片 3.设置循环检测 总结 前言 本次我们记录OpenCV的启用电脑的摄像头以及对摄 ...
- python图色检测_利用python打开摄像头及颜色检测方法
最近两周由于忙于个人项目,一直未发言了,实在是太荒凉了....,上周由于项目,见到Python的应用极为广泛,用起来也特别顺手,于是小编也开始着手学习Python,-下面我就汇报下今天的学习成果吧 小 ...
- 使用python控制摄像头
前言 当今,随着计算机技术的发展,摄像头已经成为了人们生活中不可或缺的一部分.而Python作为一种流行的编程语言,也可以轻松地控制和操作摄像头.无论你是想用Python写一个简单的摄像头应用程序,还 ...
最新文章
- [Android]你不知道的Android进程化(4)--进程通信AIDL框架
- 8、泛型程序设计与c++标准模板库2.4列表容器
- 机器学习-降维之主成分分析PCA算法原理及实战
- netstat命令总结
- linux切换任务命令,Linux top详解之交互命令、命令行选项
- hash table(开放寻址法-线性探查实现的哈希表)
- MVVM 架构解析及 Jetpack 架构组件的使用
- 计算机应用基础253页答案,计算机应用基础作业一(答案)
- 交叉编译mpg123
- Java面试智力题逻辑题汇总2021
- 电脑上安装的matlab软件打不开怎么办,电脑软件打不开没反应怎么办?
- bat操作ftp上传下载命令
- window上装python,pip
- kali获取同局域网设备的图片信息
- 最短路径(信息学奥赛一本通 - T1378)
- Combining Deep Learning with Information Retrieval to Localize Buggy Files for Bug Reports
- win10dnf服务器未响应,win101903玩DNF卡顿 ,掉线,死机的解决办法
- tomcat启动项目时一直卡住无反应的解决方案
- html手机下拉菜单样式,MUI下拉菜单样式
- kerberos简介