CustomFrame.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import os
  2. import subprocess
  3. from PyQt5.QtWidgets import (QWidget,QVBoxLayout,QTabWidget,QSplitter, QFileDialog,QMessageBox,QComboBox,
  4. QLineEdit,QTextEdit,QCheckBox,QLabel,QFrame,QPushButton,QMenu,QAction)
  5. from PyQt5.QtGui import QPixmap,QImage,QPainter,QResizeEvent,QCloseEvent,QPaintEvent
  6. from PyQt5.QtCore import QRect,Qt
  7. import numpy as np
  8. import def_pb2 as pb
  9. import GrpcClient as rpc
  10. import vtk
  11. from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
  12. #from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
  13. import threading
  14. import json
  15. class ControlFrame(QFrame):
  16. def __init__(self):
  17. QFrame.__init__(self)
  18. with open("./ui_config.json",'r',encoding='utf-8') as fp:
  19. self.config=json.load(fp)
  20. self.setGeometry(0, 0, 500, 500)
  21. self.InitUI()
  22. self.setFrameShape(self.StyledPanel)
  23. def OnAction(self,dtype,id):
  24. client=rpc.GrpcStream()
  25. ip=self.config[self.IPcombox.currentText()]
  26. port=int(self.PortEdit.text())
  27. try:
  28. if dtype==1:
  29. client.GrabImage(ip,port,id)
  30. if dtype==2:
  31. client.GrabCloud(ip,port,id)
  32. except Exception as e:
  33. QMessageBox.information(self, 'ERROR',str(e),QMessageBox.Ok,QMessageBox.Ok)
  34. def InitUI(self):
  35. self.begstatic = QLabel(self)
  36. self.begstatic.setText(" IP:")
  37. self.begstatic.setGeometry(20, 5, 80, 25)
  38. self.IPcombox = QComboBox(self)
  39. self.IPcombox.addItems(self.config.keys())
  40. self.IPcombox.setGeometry(90, 5, 150, 25)
  41. self.endstatic = QLabel(self)
  42. self.endstatic.setText("Port:")
  43. self.endstatic.setGeometry(20, 35, 80, 25)
  44. self.PortEdit = QLineEdit(self)
  45. self.PortEdit.setText("9876")
  46. self.PortEdit.setGeometry(90, 35, 150, 25)
  47. self.btnDataStreamCheck = QCheckBox("实时显示", self)
  48. self.btnDataStreamCheck.setGeometry(150, 65, 120, 30)
  49. self.btnDataStreamCheck.clicked.connect(self.RealTimeDatacb)
  50. self.staticError = QLabel(self)
  51. self.staticError.setText("信息:")
  52. self.staticError.setGeometry(20, 100, 230, 400)
  53. self.staticError.setWordWrap(True)
  54. self.staticError.setAlignment(Qt.AlignTop)
  55. def DisplayErrorInfo(self,info):
  56. self.staticError.setText("信息:"+info)
  57. def RealTimeDatacb(self):
  58. self.staticError.setText("信息:")
  59. try:
  60. if self.btnDataStreamCheck.checkState()==Qt.Checked:
  61. client=rpc.GrpcStream()
  62. ip=self.config[self.IPcombox.currentText()]
  63. port=int(self.PortEdit.text())
  64. client.OpenDataStream(ip,port)
  65. else:
  66. client=rpc.GrpcStream()
  67. ip=self.config[self.IPcombox.currentText()]
  68. port=int(self.PortEdit.text())
  69. client.CloseDataStream(ip,port)
  70. except Exception as e:
  71. QMessageBox.information(self, 'ERROR',str(e),QMessageBox.Ok,QMessageBox.Ok)
  72. def closeEvent(self, a0: QCloseEvent) -> None:
  73. if self.btnDataStreamCheck.checkState()==Qt.Checked:
  74. client=rpc.GrpcStream()
  75. ip=self.config[self.IPcombox.currentText()]
  76. port=int(self.PortEdit.text())
  77. client.CloseDataStream(ip,port)
  78. rpc.GrpcStream().close()
  79. class VtkPointCloud:
  80. def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e7):
  81. self.maxNumPoints = maxNumPoints
  82. self.vtkPolyData = vtk.vtkPolyData()
  83. self.clearPoints()
  84. mapper = vtk.vtkPolyDataMapper()
  85. mapper.SetInputData(self.vtkPolyData)
  86. mapper.SetColorModeToDefault()
  87. mapper.SetScalarRange(zMin, zMax)
  88. mapper.SetScalarVisibility(1)
  89. self.vtkActor = vtk.vtkActor()
  90. self.vtkActor.SetMapper(mapper)
  91. self.vtkActor.GetProperty().SetPointSize(1)
  92. self.vtkActor.GetProperty().SetColor(1, 1, 1)
  93. def addPoint(self, point):
  94. if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
  95. pointId = self.vtkPoints.InsertNextPoint(point)
  96. self.vtkDepth.InsertNextValue(point[2])
  97. self.vtkCells.InsertNextCell(1)
  98. self.vtkCells.InsertCellPoint(pointId)
  99. self.vtkCells.Modified()
  100. self.vtkPoints.Modified()
  101. self.vtkDepth.Modified()
  102. def clearPoints(self):
  103. self.vtkPoints = vtk.vtkPoints()
  104. self.vtkCells = vtk.vtkCellArray()
  105. self.vtkDepth = vtk.vtkDoubleArray()
  106. self.vtkDepth.SetName('DepthArray')
  107. self.vtkPolyData.SetPoints(self.vtkPoints)
  108. self.vtkPolyData.SetVerts(self.vtkCells)
  109. #self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
  110. self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')
  111. class pclViewer(QVTKRenderWindowInteractor):
  112. def __init__(self,parent=None):
  113. super(pclViewer,self).__init__(parent=parent)
  114. def mousePressEvent(self, ev):
  115. btn = ev.button()
  116. if btn == Qt.RightButton:
  117. print(" r btn")
  118. else:
  119. pass
  120. #QVTKRenderWindowInteractor.mousePressEvent(self,ev)
  121. class VtkPointCloudCanvas(QWidget):
  122. def __init__(self, *args, **kwargs):
  123. super(VtkPointCloudCanvas, self).__init__(*args, **kwargs)
  124. self.lock_=threading.RLock()
  125. self._layout = QVBoxLayout()
  126. self.setLayout(self._layout)
  127. self._vtk_widget = pclViewer(self)
  128. self._vtk_widget.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera()) # 设置交互方式==常用的方式 移动摄像机
  129. self._layout.addWidget(self._vtk_widget)
  130. self._render = vtk.vtkRenderer()
  131. self._vtk_widget.GetRenderWindow().AddRenderer(self._render)
  132. self._iren = self._vtk_widget.GetRenderWindow().GetInteractor()
  133. self._point_cloud = VtkPointCloud()
  134. self._render.AddActor(self._point_cloud.vtkActor)
  135. transform = vtk.vtkTransform()
  136. transform.Translate(0, -0.1, 0.0)
  137. axes = vtk.vtkAxesActor()
  138. axes.SetUserTransform(transform)
  139. axes.SetTotalLength([0.1,0.1,0])
  140. axes.SetXAxisLabelText('x')
  141. axes.SetYAxisLabelText('y')
  142. axes.SetZAxisLabelText('')
  143. colors = vtk.vtkNamedColors()
  144. axes.GetXAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d("Red"))
  145. axes.GetYAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d("Green"))
  146. axes.GetZAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d("Blue"))
  147. self._render.AddActor(axes)
  148. self.camera=self._render.GetActiveCamera()
  149. self.show()
  150. self._iren.Initialize()
  151. def close(self) -> bool:
  152. print(" tk close")
  153. self._point_cloud.clearPoints()
  154. def displayPCL(self,cloud:np.array):
  155. colors = vtk.vtkUnsignedCharArray()
  156. colors.SetNumberOfComponents(3)
  157. colors.SetName("Colors")
  158. self._point_cloud.clearPoints()
  159. for point in cloud:
  160. self._point_cloud.addPoint(point)
  161. colors.InsertNextTypedTuple((255,255,255))
  162. self._point_cloud.vtkPolyData.GetPointData().SetScalars(colors)
  163. self._vtk_widget.update()
  164. def resetViewer(self):
  165. self.update()
  166. class PointCLViwer(QSplitter):
  167. def __init__(self,OnAction,id):
  168. super(PointCLViwer, self).__init__()
  169. self.OnAction=OnAction
  170. self.dtype=2
  171. self.id=id
  172. self.pointCloud=[]
  173. self.InitUI()
  174. def InitUI(self):
  175. self.pclViewer=VtkPointCloudCanvas(self)
  176. self.addWidget(self.pclViewer)
  177. self.setStretchFactor(0,4)
  178. def displayCloud(self,points:np.array):
  179. self.pointCloud=points
  180. self.pclViewer.displayPCL(points)
  181. def resetViewer(self):
  182. self.pclViewer.resetViewer()
  183. def updataData(self):
  184. self.OnAction(self.dtype,self.id-1)
  185. def displayAllPoints(self):
  186. self.pclViewer.displayPCL(self.pointCloud)
  187. def contextMenuEvent(self, a0):
  188. pass
  189. class ViewerFrame(QFrame):
  190. def __init__(self,OnAction):
  191. super(ViewerFrame, self).__init__()
  192. self.OnAction=OnAction
  193. self.InitUI()
  194. def InitUI(self):
  195. self.table1=QTabWidget(self)
  196. self.table2=QTabWidget(self)
  197. self.table3=QTabWidget(self)
  198. self.table4=QTabWidget(self)
  199. self.table1.setTabPosition(QTabWidget.TabPosition.North)
  200. self.table2.setTabPosition(QTabWidget.TabPosition.North)
  201. self.table3.setTabPosition(QTabWidget.TabPosition.North)
  202. self.table4.setTabPosition(QTabWidget.TabPosition.North)
  203. self.pcViewer1=PointCLViwer(self.OnAction,1)
  204. self.pcViewer2=PointCLViwer(self.OnAction,2)
  205. self.pcViewer3=PointCLViwer(self.OnAction,3)
  206. self.pcViewer4=PointCLViwer(self.OnAction,4)
  207. self.table1.addTab(self.pcViewer1,"C3")
  208. self.table2.addTab(self.pcViewer2,"C4")
  209. self.table3.addTab(self.pcViewer3,"C5")
  210. self.table4.addTab(self.pcViewer4,"C6")
  211. def closeEvent(self, a0: QCloseEvent) -> None:
  212. pass
  213. def resizeEvent(self, a0: QResizeEvent) -> None:
  214. w, h = self.size().width(), self.size().height()
  215. w=w-15
  216. h=h-15
  217. self.table1.setGeometry(5, 5, w/2, h/2)
  218. self.table2.setGeometry(10+w/2, 5, w/2, h/2)
  219. self.table3.setGeometry(5, 10+h/2, w/2, h/2)
  220. self.table4.setGeometry(10+w/2, 10+h/2, w/2, h/2)
  221. def PbCloud2Pts(self,cloud):
  222. size=len(cloud)
  223. npdata=np.zeros(shape=[size,3],dtype=np.float32)
  224. i=0
  225. for point in cloud:
  226. x=point.x
  227. y=point.y
  228. npdata[i,:]=[x,y-0.1,0]
  229. i=i+1
  230. return npdata
  231. def DisplayCloud(self,clouds:pb.clouds):
  232. self.pcViewer1.displayCloud(self.PbCloud2Pts(clouds.clamp_1))
  233. self.pcViewer2.displayCloud(self.PbCloud2Pts(clouds.clamp_2))
  234. self.pcViewer3.displayCloud(self.PbCloud2Pts(clouds.clamp_3))
  235. self.pcViewer4.displayCloud(self.PbCloud2Pts(clouds.clamp_4))