英特尔Realsense学习笔记二:pyqt5 实时显示 Realsense D415 深度图像和彩色图像
要做一个D415相机的最高分辨率的拍摄程序,深度相机最高分辨率为1280*720,普通屏幕是不能同时显示两个1280*720的,由于拍摄的时候由于是拍摄人体面部,所以深度图和彩色图像都截取中间的640*720,然后放到一起显示,结果如下:
1.face_mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>1300</width><height>860</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><widget class="QLabel" name="label_show"><property name="geometry"><rect><x>10</x><y>0</y><width>1280</width><height>720</height></rect></property><property name="text"><string>Depth</string></property></widget><widget class="QPushButton" name="pushButton_takephoto"><property name="geometry"><rect><x>330</x><y>750</y><width>101</width><height>41</height></rect></property><property name="text"><string>拍摄</string></property></widget><widget class="QLineEdit" name="lineEdit_id"><property name="geometry"><rect><x>140</x><y>750</y><width>151</width><height>41</height></rect></property></widget><widget class="QLabel" name="label"><property name="geometry"><rect><x>50</x><y>760</y><width>61</width><height>21</height></rect></property><property name="text"><string>ID:</string></property></widget></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>1300</width><height>26</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>
2.face_mainwindow.py
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'face_mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(1300, 860)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.label_show = QtWidgets.QLabel(self.centralwidget)self.label_show.setGeometry(QtCore.QRect(10, 0, 1280, 720))self.label_show.setObjectName("label_show")self.pushButton_takephoto = QtWidgets.QPushButton(self.centralwidget)self.pushButton_takephoto.setGeometry(QtCore.QRect(330, 750, 101, 41))self.pushButton_takephoto.setObjectName("pushButton_takephoto")self.lineEdit_id = QtWidgets.QLineEdit(self.centralwidget)self.lineEdit_id.setGeometry(QtCore.QRect(140, 750, 151, 41))self.lineEdit_id.setObjectName("lineEdit_id")self.label = QtWidgets.QLabel(self.centralwidget)self.label.setGeometry(QtCore.QRect(50, 760, 61, 21))self.label.setObjectName("label")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 1300, 26))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.label_show.setText(_translate("MainWindow", "Depth"))self.pushButton_takephoto.setText(_translate("MainWindow", "拍摄"))self.label.setText(_translate("MainWindow", "ID:"))
3.face.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QObject, pyqtSignal
from face_mainwindow import Ui_MainWindowimport time
import numpy as np
import threading as th
import ctypes
import inspect# First import the library
import pyrealsense2 as rs
import cv2DELAY = 0class MainWindow(QMainWindow, Ui_MainWindow):def __init__(self):super(MainWindow, self).__init__()# Set up the user interface from Designer.self.setupUi(self)self.dis_update.connect(self.camera_view)self.thread_camera = None# 在对应的页面类的内部,与def定义的函数同级dis_update = pyqtSignal(QPixmap)# 添加一个退出的提示事件def closeEvent(self, event):"""我们创建了一个消息框,上面有俩按钮:Yes和No.第一个字符串显示在消息框的标题栏,第二个字符串显示在对话框,第三个参数是消息框的俩按钮,最后一个参数是默认按钮,这个按钮是默认选中的。返回值在变量reply里。"""reply = QMessageBox.question(self, 'Message', "Are you sure to quit?",QMessageBox.Yes | QMessageBox.No, QMessageBox.No)# 判断返回值,如果点击的是Yes按钮,我们就关闭组件和应用,否则就忽略关闭事件if reply == QMessageBox.Yes:self.stop_thread(self.thread_camera)event.accept()else:event.ignore()def open_camera(self):# target选择开启摄像头的函数self.thread_camera = th.Thread(target=self.open_realsense)self.thread_camera.start()print('Open Camera')def camera_view(self, c):# 调用setPixmap函数设置显示Pixmapself.label_show.setPixmap(c)# 调用setScaledContents将图像比例化显示在QLabel上self.label_show.setScaledContents(True)def _async_raise(self, tid, exctype):"""raises the exception, performs cleanup if needed"""tid = ctypes.c_long(tid)if not inspect.isclass(exctype):exctype = type(exctype)res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))if res == 0:raise ValueError("invalid thread id")elif res != 1:# """if it returns a number greater than one, you're in trouble,# and you should call it again with exc=NULL to revert the effect"""ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)raise SystemError("PyThreadState_SetAsyncExc failed")def stop_thread(self, thread):self._async_raise(thread.ident, SystemExit)def open_realsense(self):print('open_realsense')# Create a pipelinepipeline = rs.pipeline()# Create a config and configure the pipeline to stream# different resolutions of color and depth streamsconfig = rs.config()config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)# Start streamingprofile = pipeline.start(config)# Getting the depth sensor's depth scale (see rs-align example for explanation)depth_sensor = profile.get_device().first_depth_sensor()depth_scale = depth_sensor.get_depth_scale()print("Depth Scale is: ", depth_scale)# We will be removing the background of objects more than# clipping_distance_in_meters meters awayclipping_distance_in_meters = 1 # 1 meterclipping_distance = clipping_distance_in_meters / depth_scale# Color Intrinsics# intr = color_frame.profile.as_video_stream_profile().intrinsics# Create an align object# rs.align allows us to perform alignment of depth frames to others frames# The "align_to" is the stream type to which we plan to align depth frames.align_to = rs.stream.coloralign = rs.align(align_to)# Streaming looptry:while True:# Get frameset of color and depthframes = pipeline.wait_for_frames()# frames.get_depth_frame() is a 640x360 depth image# Align the depth frame to color framealigned_frames = align.process(frames)# Get aligned framesaligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth imagecolor_frame = aligned_frames.get_color_frame()# Validate that both frames are validif not aligned_depth_frame or not color_frame:continuedepth_image = np.asanyarray(aligned_depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())# Remove background - Set pixels further than clipping_distance to greygrey_color = 153depth_image_3d = np.dstack((depth_image, depth_image, depth_image)) # depth image is 1 channel, color is 3 channelsbg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color,color_image)# Render imagesdepth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)images = np.hstack((bg_removed[0:720, 320:960], depth_colormap[0:720, 320:960]))qimage = QImage(images, 1280, 720, QImage.Format_BGR888)pixmap = QPixmap.fromImage(qimage)self.dis_update.emit(pixmap)time.sleep(DELAY)finally:pipeline.stop()if __name__ == "__main__":app = QApplication(sys.argv)w = MainWindow()w.show()w.open_camera()print('Hello World!')sys.exit(app.exec_())
英特尔Realsense学习笔记二:pyqt5 实时显示 Realsense D415 深度图像和彩色图像相关推荐
- 深度强化学习笔记(二)——Q-learning学习与二维寻路demo实现
深度强化学习笔记(二)--Q-learning学习与二维寻路demo实现 文章目录 深度强化学习笔记(二)--Q-learning学习与二维寻路demo实现 前言 理论 什么是Q-Learning 算 ...
- qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)
原博主博客地址:http://blog.csdn.net/qq21497936 本文章博客地址:http://blog.csdn.net/qq21497936/article/details/7851 ...
- [转载]dorado学习笔记(二)
原文地址:dorado学习笔记(二)作者:傻掛 ·isFirst, isLast在什么情况下使用?在遍历dataset的时候会用到 ·dorado执行的顺序,首先由jsp发送请求,调用相关的ViewM ...
- PyTorch学习笔记(二)——回归
PyTorch学习笔记(二)--回归 本文主要是用PyTorch来实现一个简单的回归任务. 编辑器:spyder 1.引入相应的包及生成伪数据 import torch import torch.nn ...
- tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数
tensorflow学习笔记二--建立一个简单的神经网络 2016-09-23 16:04 2973人阅读 评论(2) 收藏 举报 分类: tensorflow(4) 目录(?)[+] 本笔记目的 ...
- Scapy学习笔记二
Scapy学习笔记二 Scapy Sniffer的用法: http://blog.csdn.net/qwertyupoiuytr/article/details/54670489 Scapy Snif ...
- Ethernet/IP 学习笔记二
Ethernet/IP 学习笔记二 原文链接:http://wiki.mbalib.com/wiki/Ethernet/IP 1.通信模式 不同于源/目的通信模式,EtherNet/IP 采用生产/消 ...
- Java学习笔记二:数据类型
Java学习笔记二:数据类型 1. 整型:没有小数部分,允许为负数,Java整型分4种:int short long byte 1.1 Int最为常用,一个Int类型变量在内存中占用4个字节,取值范围 ...
- 吴恩达《机器学习》学习笔记二——单变量线性回归
吴恩达<机器学习>学习笔记二--单变量线性回归 一. 模型描述 二. 代价函数 1.代价函数和目标函数的引出 2.代价函数的理解(单变量) 3.代价函数的理解(两个参数) 三. 梯度下降- ...
最新文章
- 创建细分客户的无监督学习项目
- JQuery ajax请求一直返回Error(parsererror)
- Android 网络操作常用的两个类
- centos7源码安装mysql报错_centos7.3源码安装mysql
- 将MathType公式转换为Word自带公式
- 【转载】谁记录了mysql error log中的超长信息
- 黑苹果使用Karabiner-Elements改Windows快捷键教程
- I.Algorithm Choosing Mushrooms
- PHP 格式化字节大小
- 浅淡 Apache Kylin 与 ClickHouse 的对比
- sympy 求微分方程_Sympy常用函数总结
- (三)lvs负载均衡详解--lvs-DR+keepalived配置
- 漫步数学分析三十九——隐函数定理
- html5 制作书架展示 PHP,简单做出HTML5翻页效果文字特效
- 优雅编程之这样使用Map,你就“正常”了(三十六)
- 超松弛迭代法(SOR)的Python实现
- opencv摄像头 vmware虚拟机无法打开摄像头的解决方法
- 去阿里,是不可能的,这辈子都不可能的
- Android之仿美拍主要菜单滑动反弹效果
- 美女博士天天让我帮她下载SCI文献,今天终于给她发过去了