目录
安装:
示例代码:
生成3d检测框:
安装:
pip install pyqt5 mayavi traits traitsui
示例代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from tvtk.pyface.scene_editor import SceneEditor
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.mayavi_scene import MayaviScene
class Visualization(HasTraits):
scene = Instance(MlabSceneModel, ())
def __init__(self):
HasTraits.__init__(self)
# the layout of the dialog created
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=250, width=300, show_label=False), )
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('Mayavi embedded in PyQt5')
# Create the main widget and layout
main_widget = QWidget()
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
# Create the Mayavi visualization
self.visualization = Visualization()
self.mayavi_scene = self.visualization.edit_traits(parent=main_widget, kind='subpanel').control
layout.addWidget(self.mayavi_scene)
# Create a button and connect its event
self.button = QPushButton('Generate 3D Mesh')
layout.addWidget(self.button)
self.button.clicked.connect(self.button_event)
def button_event(self):
# Create the data.
from numpy import pi, sin, cos, mgrid
dphi, dtheta = pi / 250.0, pi / 250.0
[phi, theta] = mgrid[0:pi + dphi * 1.5:dphi, 0:2 * pi + dtheta * 1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7
x = r * sin(phi) * cos(theta)
y = r * cos(phi)
z = r * sin(phi) * sin(theta)
# View it.
self.visualization.scene.mlab.mesh(x, y, z)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
生成3d检测框:
import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from tvtk.pyface.scene_editor import SceneEditor
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.mayavi_scene import MayaviScene
from mayavi import mlab
class Visualization(HasTraits):
scene = Instance(MlabSceneModel, ())
def __init__(self):
HasTraits.__init__(self)
# the layout of the dialog created
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=600, width=800, show_label=False), )
def draw_3d_box(self, center, size, orientation):
c, s = np.cos(orientation), np.sin(orientation)
R = np.array([
[c, -s, 0],
[s, c, 0],
[0, 0, 1]
])
l, w, h = size / 2.0
corners = np.array([
[ l, w, h],
[ l, -w, h],
[-l, -w, h],
[-l, w, h],
[ l, w, -h],
[ l, -w, -h],
[-l, -w, -h],
[-l, w, -h]
])
corners = np.dot(corners, R.T) + center
edges = [
[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]
]
for edge in edges:
self.scene.mlab.plot3d(
[corners[edge[0], 0], corners[edge[1], 0]],
[corners[edge[0], 1], corners[edge[1], 1]],
[corners[edge[0], 2], corners[edge[1], 2]],
color=(1, 0, 0), tube_radius=None, line_width=1, figure=self.scene.mayavi_scene
)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('Mayavi embedded in PyQt5')
# Create the main widget and layout
main_widget = QWidget()
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
# Create the Mayavi visualization
self.visualization = Visualization()
self.mayavi_scene = self.visualization.edit_traits(parent=main_widget, kind='subpanel').control
layout.addWidget(self.mayavi_scene)
# Create a button and connect its event
self.button = QPushButton('Generate 3D Detection Box')
layout.addWidget(self.button)
self.button.clicked.connect(self.button_event)
def button_event(self):
# Example data for a 3D box
center = np.array([0, 0, 0])
size = np.array([2, 1, 1])
orientation = np.pi / 4 # 45 degrees
# Draw the 3D box
self.visualization.draw_3d_box(center, size, orientation)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())