英特尔Realsense学习笔记二:pyqt5 实时显示 Realsense D415 深度图像和彩色图像
生活随笔
收集整理的這篇文章主要介紹了
英特尔Realsense学习笔记二:pyqt5 实时显示 Realsense D415 深度图像和彩色图像
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
要做一個D415相機(jī)的最高分辨率的拍攝程序,深度相機(jī)最高分辨率為1280*720,普通屏幕是不能同時顯示兩個1280*720的,由于拍攝的時候由于是拍攝人體面部,所以深度圖和彩色圖像都截取中間的640*720,然后放到一起顯示,結(jié)果如下:
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# 在對應(yīng)的頁面類的內(nèi)部,與def定義的函數(shù)同級dis_update = pyqtSignal(QPixmap)# 添加一個退出的提示事件def closeEvent(self, event):"""我們創(chuàng)建了一個消息框,上面有倆按鈕:Yes和No.第一個字符串顯示在消息框的標(biāo)題欄,第二個字符串顯示在對話框,第三個參數(shù)是消息框的倆按鈕,最后一個參數(shù)是默認(rèn)按鈕,這個按鈕是默認(rèn)選中的。返回值在變量reply里。"""reply = QMessageBox.question(self, 'Message', "Are you sure to quit?",QMessageBox.Yes | QMessageBox.No, QMessageBox.No)# 判斷返回值,如果點擊的是Yes按鈕,我們就關(guān)閉組件和應(yīng)用,否則就忽略關(guān)閉事件if reply == QMessageBox.Yes:self.stop_thread(self.thread_camera)event.accept()else:event.ignore()def open_camera(self):# target選擇開啟攝像頭的函數(shù)self.thread_camera = th.Thread(target=self.open_realsense)self.thread_camera.start()print('Open Camera')def camera_view(self, c):# 調(diào)用setPixmap函數(shù)設(shè)置顯示Pixmapself.label_show.setPixmap(c)# 調(diào)用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_())?
?
總結(jié)
以上是生活随笔為你收集整理的英特尔Realsense学习笔记二:pyqt5 实时显示 Realsense D415 深度图像和彩色图像的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7休眠 计算机管理,win7休眠设
- 下一篇: 导航定位(仿多玩)