Browse Source

增加自动打开cc

zx 10 months ago
parent
commit
18c0bd4392
3 changed files with 86 additions and 30 deletions
  1. 58 24
      Viewer/CustomFrame.py
  2. 19 0
      Viewer/GrpcClient.py
  3. 9 6
      Viewer/MainWnd.py

+ 58 - 24
Viewer/CustomFrame.py

@@ -1,3 +1,5 @@
+import os
+import subprocess
 
 from PyQt5.QtWidgets import (QWidget,QVBoxLayout,QTabWidget,QSplitter, QFileDialog,QMessageBox,QComboBox,
                              QLineEdit,QTextEdit,QCheckBox,QLabel,QFrame,QPushButton,QMenu,QAction)
@@ -10,12 +12,13 @@ import GrpcClient as rpc
 import vtk
 import cv2
 import time
+import google.protobuf.text_format as tf
 from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
 #from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
 
 import threading
 class ControlFrame(QFrame):
-    cameraMap={"相机1":1,"相机2":2,"相机3":3,"相机4":4}
+    cameraMap={"相机1":0,"相机2":1,"相机3":2,"相机4":3}
     def __init__(self):
         QFrame.__init__(self)
         self.setGeometry(0, 0, 500, 500)
@@ -96,37 +99,33 @@ class ControlFrame(QFrame):
         self.btnSaveWheelSampleCloud.setGeometry(150, 170, 100, 30)
         self.btnSaveWheelSampleCloud.setText("保存轮胎样本")
 
+        self.ParameterEdit = QTextEdit(self)
+        self.ParameterEdit.setText("")
+        self.ParameterEdit.setGeometry(20, 300, 130, 150)
+
         self.MatrixEdit = QTextEdit(self)
+        self.MatrixEdit.setAlignment(Qt.AlignTop)
         self.MatrixEdit.setText("输入4x4变换矩阵")
         self.MatrixEdit.setGeometry(20, 205, 500, 90)
-        #self.MatrixEdit.setLineWrapMode()
-        self.MatrixEdit.setAlignment(Qt.AlignTop)
-        self.btnMatrix = QPushButton(self)
-        self.btnMatrix.setGeometry(20, 300, 100, 30)
-        self.btnMatrix.setText("转换rpy,xyz")
-        self.btnMatrix.clicked.connect(self.matrix2rpy)
-
-        self.ParameterEdit = QTextEdit(self)
-        self.ParameterEdit.setText("")
-        self.ParameterEdit.setGeometry(20, 340, 130, 150)
+        self.MatrixEdit.textChanged.connect(self.matrix2rpy)
 
         self.CameraListEdit=QComboBox(self)
         self.CameraListEdit.addItems(self.cameraMap.keys())
-        self.CameraListEdit.setGeometry(155, 340, 100, 30)
+        self.CameraListEdit.setGeometry(155, 300, 100, 30)
 
 
         self.btnChange = QPushButton(self)
-        self.btnChange.setGeometry(155, 390, 100, 40)
+        self.btnChange.setGeometry(155, 350, 100, 40)
         self.btnChange.setText("修改参数")
         self.btnChange.clicked.connect(self.ChangeCameraParameter)
 
         self.btnSave = QPushButton(self)
-        self.btnSave.setGeometry(155, 450, 100, 40)
+        self.btnSave.setGeometry(155, 410, 100, 40)
         self.btnSave.setText("写入参数")
         self.btnSave.clicked.connect(self.SaveCameraParameter)
 
         self.ErrorInfo = QLabel(self)
-        self.ErrorInfo.setGeometry(20, 495, 250, 400)
+        self.ErrorInfo.setGeometry(20, 455, 250, 400)
         self.ErrorInfo.setWordWrap(True)
         self.ErrorInfo.setAlignment(Qt.AlignTop)
 
@@ -236,17 +235,48 @@ class ControlFrame(QFrame):
             z = 0
         return np.array([x, y, z])
     def SaveCameraParameter(self):
+        self.btnSave.setEnabled(False)
         camera=self.CameraListEdit.currentText()
         id=self.cameraMap[camera]
         text=self.ParameterEdit.toPlainText()
-
-        print(" 保存相机%d参数:%s"%(id,text))
+        transform=pb.CoordinateTransformation3D()
+        try:
+            tf.Parse(text, transform)
+        except:
+            QMessageBox.information(self, '格式错误',text,QMessageBox.Ok,QMessageBox.Ok)
+            self.btnSave.setEnabled(True)
+        print(" 保存相机%d参数:%s"%(id,transform))
+        client=rpc.GrpcStream()
+        ip=self.IPEdit.text()
+        port=int(self.PortEdit.text())
+        try:
+            ret=client.SaveCameraParameter(ip,port,id,transform)
+            QMessageBox.information(self, 'Info',ret.info,QMessageBox.Ok,QMessageBox.Ok)
+        except Exception as e:
+            QMessageBox.information(self, 'Info',str(e),QMessageBox.Ok,QMessageBox.Ok)
+        self.btnSave.setEnabled(True)
     def ChangeCameraParameter(self):
+        self.btnChange.setEnabled(False)
         camera=self.CameraListEdit.currentText()
         id=self.cameraMap[camera]
         text=self.ParameterEdit.toPlainText()
-
-        print(" 修改相机%d参数:%s"%(id,text))
+        transform=pb.CoordinateTransformation3D()
+        try:
+            tf.Parse(text, transform)
+        except:
+            QMessageBox.information(self, '格式错误',text,QMessageBox.Ok,QMessageBox.Ok)
+            self.btnChange.setEnabled(True)
+            return
+        print(" 修改相机%d参数:%s"%(id,transform))
+        client=rpc.GrpcStream()
+        ip=self.IPEdit.text()
+        port=int(self.PortEdit.text())
+        try:
+            ret=client.ChangeCameraParameter(ip,port,id,transform)
+            QMessageBox.information(self, 'Info',ret.info,QMessageBox.Ok,QMessageBox.Ok)
+        except Exception as e:
+            QMessageBox.information(self, 'Info',str(e),QMessageBox.Ok,QMessageBox.Ok)
+        self.btnChange.setEnabled(True)
     def matrix2rpy(self):
         text=""
         str=self.MatrixEdit.toPlainText()
@@ -266,8 +296,9 @@ class ControlFrame(QFrame):
                 count+=1
 
             rpy=self.rotationMatrixToEulerAngles(matrix)
-            text="\"roll\" : %.3f,\n\"pitch\": %.3f,\n\"yaw\": %.3f,\n\"x\":%.3f,\n\"y\":%.3f,\n\"z\":%.3f,"%(math.degrees(rpy[0]),math.degrees(rpy[1]),math.degrees(rpy[2]),
+            text="roll : %.3f,\npitch: %.3f,\nyaw: %.3f,\nx:%.3f,\ny:%.3f,\nz:%.3f,"%(math.degrees(rpy[0]),math.degrees(rpy[1]),math.degrees(rpy[2]),
                                             matrix[0,3],matrix[1,3],matrix[2,3])
+
         self.ParameterEdit.setText(text)
         #QMessageBox.information(self, '旋转变换转rpy-t',text,QMessageBox.Ok,QMessageBox.Ok)
     def btnCancelClick(self):
@@ -427,17 +458,20 @@ class PointCLViwer(QSplitter):
         self.addWidget(self.pclViewer)
         self.setStretchFactor(0,4)
     def save(self):
-        fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "C:/","All Files (*);;Text Files (*.txt)")
-        print(fileName2 ,ok2)
+        #fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "C:/","All Files (*);;Text Files (*.txt)")
+        #print(fileName2 ,ok2)
+        fileName2="./cloud/%d.txt"%(self.id)
         with open(fileName2,"w+") as f:
             for point in self.pointCloud:
                 prob=point[3]
                 f.write("%f %f %f %d %d %d\n"%(point[0],point[1],point[2],int(255*prob),int(255-prob*255),255-int(prob*255)))
 
         print("Save Cloud to %s"%(fileName2))
+        subprocess.Popen(["cloudcompare.CloudCompare",fileName2])
+        #os.system("cloudcompare.CloudCompare %s"%(fileName2))
     def saveWheel(self):
-        fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "C:/","All Files (*);;Text Files (*.txt)")
-        print(fileName2 ,ok2)
+        #fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "C:/","All Files (*);;Text Files (*.txt)")
+        fileName2="./cloud/%d-wheel.txt"%(self.id)
         with open(fileName2,"w+") as f:
             for point in self.pointCloud:
                 prob=point[3]

+ 19 - 0
Viewer/GrpcClient.py

@@ -48,6 +48,25 @@ class GrpcStream(threading.Thread):
         resCloud=self.stub.GetCloud(cmd)
         if self.cloudCallBack is not None:
             self.cloudCallBack(resCloud)
+    def ChangeCameraParameter(self,ip,port,id,parameter:pb.CoordinateTransformation3D):
+        connectstr='%s:%d'%(ip,port)
+        self.channel=grpc.insecure_channel(connectstr)
+        self.stub = mrpc.StreamServerStub(self.channel)
+        cmd=pb.RequestCmd()
+        cmd.Id=id
+        cmd.params.CopyFrom(parameter)
+        res=self.stub.setTofTransformation(cmd)
+        return res
+    def SaveCameraParameter(self,ip,port,id,parameter:pb.CoordinateTransformation3D):
+        connectstr='%s:%d'%(ip,port)
+        self.channel=grpc.insecure_channel(connectstr)
+        self.stub = mrpc.StreamServerStub(self.channel)
+        cmd=pb.RequestCmd()
+        cmd.Id=id
+        cmd.params.CopyFrom(parameter)
+        res=self.stub.saveTofTransformation(cmd)
+        return res
+
     def OpenDataStream(self,ip,port):
         connectstr='%s:%d'%(ip,port)
         channel=grpc.insecure_channel(connectstr)

+ 9 - 6
Viewer/MainWnd.py

@@ -19,9 +19,10 @@ class MainWindow(QMainWindow):
         splitter= self.split_()
         self.setCentralWidget(splitter)
     def saveAllCloud(self):
-        fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "C:/","All Files (*);;Text Files (*.txt)")
-        if fileName2=="":
-            return
+        # fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "C:/","All Files (*);;Text Files (*.txt)")
+        # if fileName2=="":
+        #     return
+        fileName2="./cloud/all.txt"
 
         with open(fileName2,"w+") as f:
             for point in self.viewerFrame.pcViewer1.pointCloud:
@@ -39,7 +40,7 @@ class MainWindow(QMainWindow):
     #窗口基础属性
     def saveWheelSampleCloud(self):
         print(" save wheel sample")
-        path="./wheelSamples"
+        path="./cloud"
         folder=os.path.exists(path)
         if not folder:
             os.mkdir(path)
@@ -72,6 +73,8 @@ class MainWindow(QMainWindow):
                     f.write("%f %f %f\n"%(point[0],point[1],point[2]))
         text="样本已保存到:%s 文件夹下"%(sampleFolder)
         QMessageBox.information(self, '保存轮胎样本',text,QMessageBox.Ok,QMessageBox.Ok)
+        subprocess.Popen(["cloudcompare.CloudCompare",sampleFolder+"/1.txt",sampleFolder+"/2.txt",
+                          sampleFolder+"/3.txt",sampleFolder+"/4.txt"])
 
     def basic(self):
         #设置标题,大小,图标
@@ -95,8 +98,8 @@ class MainWindow(QMainWindow):
         splitter.addWidget(self.viewerFrame)
         splitter.addWidget(self.Controller)
 
-        splitter.setStretchFactor(0,3)
-        splitter.setStretchFactor(1,1)
+        splitter.setStretchFactor(0,11)
+        splitter.setStretchFactor(1,4)
         return splitter
     def OnImagesHandle(self,images:pb.ResImage):
         self.viewerFrame.DisplayImage(images)