pyqt5显示realsense d435i

pyqt5显示realsense d435i

界面:

vc_ui.py

复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.12.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1102, 847)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")

        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout")
        
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setMinimumSize(QtCore.QSize(1280, 720))
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.verticalLayout.addLayout(self.horizontalLayout)


        
        
        
        self.horizontalLayout_2 = QtWidgets.QVBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        #self.label_3.setMinimumSize(QtCore.QSize(0, 240))
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_2.addWidget(self.label_3)
        
        
        

        self.label_signal = QtWidgets.QLabel(self.centralwidget)
        self.label_signal.setMinimumSize(QtCore.QSize(0, 32))
        self.label_signal.setAlignment(QtCore.Qt.AlignCenter)
        self.label_signal.setObjectName("label_signal")
        self.horizontalLayout_2.addWidget(self.label_signal)
        
        
        self.horizontalLayout.addLayout(self.horizontalLayout_2)

        
        
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.horizontalLayout_3.addLayout(self.verticalLayout)

        
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1102, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.toolBar = QtWidgets.QToolBar(MainWindow)
        self.toolBar.setObjectName("toolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.actionConnect = QtWidgets.QAction(MainWindow)
        self.actionConnect.setObjectName("actionConnect")
        self.actionCheck = QtWidgets.QAction(MainWindow)
        self.actionCheck.setObjectName("actionCheck")

        
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setObjectName("actionClose")
        self.toolBar.addAction(self.actionConnect)
        
        
        self.toolBar.addAction(self.actionClose)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_2.setText(_translate("MainWindow", ""))
        self.label_3.setText(_translate("MainWindow", "深度图"))
        
        
        self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
        self.actionConnect.setText(_translate("MainWindow", "连接"))
        self.actionClose.setText(_translate("MainWindow", "关闭"))
复制代码

 

主函数

vc_main.py

复制代码
from vc_check_realsense_2022052502 import *


from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

if __name__ == '__main__':
    app = QApplication( sys.argv )
    dlg = VCRealsense('Realsense数据显示')
    dlg.showMaximized()
    sys.exit(app.exec_())
    pass
复制代码

 

realsense操作函数

复制代码
from vc_ui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QImage, QPixmap


#################################################
import time
import numpy as np
import os
import cv2
import pyrealsense2 as rs




#相机数据操作类
class VCRealsense(QtWidgets.QMainWindow):
    def __init__(self, name="Relaense"):
        # 构造函数
        super().__init__()
        self.initUI(name)
        self.cnt_tmp = 0

        self.warning_count = 0
        
        #init QTimer
        self.timer_camera = QtCore.QTimer()
        #init QTimer 
        self.timer_ygkj = QtCore.QTimer()
        
        self.initConnect()

        
        self.pipeline = rs.pipeline()
        self.config = rs.config()
        
        self.align_to = rs.stream.color  #与color流对齐
        self.align = rs.align(self.align_to)
        
        self.config.enable_stream(rs.stream.depth,  1280, 720, rs.format.z16, 30)
        self.config.enable_stream(rs.stream.color,  1280, 720, rs.format.bgr8, 30)
        
        

    def initUI(self, name):
        # 初始化函数
        self.vc_ui = Ui_MainWindow()
        self.vc_ui.setupUi(self)
        self.setWindowTitle(name)

        pass
        


    #绑定槽函数
    def initConnect(self):
        
        #连接相机
        self.vc_ui.actionConnect.triggered.connect(self.slot_connect_action)
        
        #关闭
        self.vc_ui.actionClose.triggered.connect(self.slot_close_action)
        #
        self.timer_camera.timeout.connect(self.show_camera_action)
        pass


    
    #连接
    def slot_connect_action(self):
        print("+++")
        #print(self.CAM_ID)
        #self.timer_camera = QtCore.QTimer()
        if self.timer_camera.isActive() == False:
            #flag = self.camera.open(self.CAM_ID)
            self.pipeline.start(self.config)
            #刷新频率
            self.timer_camera.start(30)
            
        pass




    

    #关闭
    def slot_close_action(self):
        print("close")
        
        pass

        

    
    

    #把图像数据显示在控件中
    def checkImgShow(self):
        # 提取图像的通道和尺寸,用于将OpenCV下的image转换成Qimage
        height, width, channel = self.img2.shape
        bytesPerline = 3 * width
        #img_show = cv2.resize(self.img2, (720, 480))
        #self.qImg2 = QImage(img_show.data, 720, 480, bytesPerline, QImage.Format_RGB888).rgbSwapped()
        
        self.qImg2 = QImage(self.img2.data, width, height, bytesPerline, QImage.Format_RGB888).rgbSwapped()
        
        # 将QImage显示出来
        self.vc_ui.label_2.setPixmap(QPixmap.fromImage(self.qImg2))    

    #
    
    
    ######
    #刷新显示
    def show_camera_action(self):
        
        color_intrin, depth_intrin, color_image, depth_image, aligned_depth_frame = self.get_aligned_images()

        
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        
        images = np.hstack((color_image, depth_colormap))
        
        self.img_test2 = cv2.resize(depth_colormap, (640, 480))
        
        
        self.image = color_image.copy()
        
        self.cameraImg =self.image
        self.im0 = self.cameraImg.copy()
        self.cameraImg = cv2.cvtColor(self.cameraImg,cv2.COLOR_BGR2RGB)
        self.img_test2 = cv2.cvtColor(self.img_test2,cv2.COLOR_BGR2RGB) 
        
        
        self.showImage = QtGui.QImage(self.cameraImg.data, self.cameraImg.shape[1], self.cameraImg.shape[0], QtGui.QImage.Format_RGB888)
        self.showImage2 = QtGui.QImage(self.img_test2.data, self.img_test2.shape[1], self.img_test2.shape[0], QtGui.QImage.Format_RGB888)

        
        

        self.vc_ui.label_2.setPixmap(QtGui.QPixmap.fromImage(self.showImage))
        self.vc_ui.label_3.setPixmap(QtGui.QPixmap.fromImage(self.showImage2))

        
        pass
            


    


    
    

    
    
    


    

    

    #
    #获取相机数据
    def get_aligned_images(self):
        
        frames = self.pipeline.wait_for_frames()  #等待获取图像帧
        aligned_frames = self.align.process(frames)  #获取对齐帧
        aligned_depth_frame = aligned_frames.get_depth_frame()  #获取对齐帧中的depth帧
        color_frame = aligned_frames.get_color_frame()   #获取对齐帧中的color帧

        ############### 相机参数的获取 #######################
        intr = color_frame.profile.as_video_stream_profile().intrinsics   #获取相机内参
        depth_intrin = aligned_depth_frame.profile.as_video_stream_profile().intrinsics  #获取深度参数(像素坐标系转相机坐标系会用到)
        
        
        depth_image = np.asanyarray(aligned_depth_frame.get_data())  #深度图(默认16位)
        depth_image_8bit = cv2.convertScaleAbs(depth_image, alpha=0.03)  #深度图(8位)
        depth_image_3d = np.dstack((depth_image_8bit,depth_image_8bit,depth_image_8bit))  #3通道深度图
        color_image = np.asanyarray(color_frame.get_data())  # RGB图
        
        #返回相机内参、深度参数、彩色图、深度图、齐帧中的depth帧
        return intr, depth_intrin, color_image, depth_image, aligned_depth_frame


#######################
复制代码

 

 

 

依赖库:

requirements.txt

# pip install -r requirements.txt

# base ----------------------------------------
numpy>=1.18.5
opencv-python>=4.1.2
pyrealsense2

 

运行:

(wind_2021) F:\RealsenseProjecty\pyrealsense>python vc_main.py

 

 

运行效果:

 

 

 

####################

posted @   西北逍遥  阅读(236)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2021-07-07 IfcStructuralLoadTemperature
2020-07-07 IfcTrimmedCurve
2019-07-07 osg fbx遍历模型节点名称
2019-07-07 osg fbx模型点击节点,对应节点染色
2019-07-07 osg fbx模型中任何一个节点染色(着色)
2019-07-07 osg 在fbx模型中添加自定义节点
2019-07-07 osg fbx模型删除模型中的某几个节点,实现编辑模型的功能
点击右上角即可分享
微信分享提示