def __init__(self): scene.SceneCanvas.__init__(self, keys=None) self.size = 800, 600 self.unfreeze() self.view = self.central_widget.add_view() self.radius = 2.0 self.view.camera = 'turntable' self.view.camera.elevation = 19.0 self.view.camera.center = (3.9, 3.0, 7.1) self.view.camera.azimuth = -90.0 self.view.camera.scale_factor = 48 fName = '/home/hexindong/DATASET/kittidataset/KITTI/object/training/velodyne/000169.bin' scans = np.fromfile(fName,dtype=np.float32).reshape(-1,4)[:,0:3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(scans, edge_width=0, face_color=(1, 1, 1, 1), size=0.01, scaling=True) self.view.add(scatter) self.freeze()
def addPointCloud(): ### this is the example code of vispy # first, show a point cloud given the coordinates pos = np.random.normal(size=(100000, 3), scale=0.2) # one could stop here for the data generation, the rest is just to make the # data look more interesting. Copied over from magnify.py centers = np.random.normal(size=(50, 3)) indexes = np.random.normal(size=100000, loc=centers.shape[0] / 2., scale=centers.shape[0] / 3.) indexes = np.clip(indexes, 0, centers.shape[0] - 1).astype(int) scales = 10 ** (np.linspace(-2, 0.5, centers.shape[0]))[indexes][:, np.newaxis] pos *= scales pos += centers[indexes] print 'type of pos:', type(pos) print 'shape of pos:', pos.shape # create scatter object and fill in the data scatter = visuals.Markers() scatter.set_data(pos, symbol='o', edge_color=None, face_color=(1, 1, 1, .5), size=5) view.add(scatter)
def setup_pcl_viewer(X, color=(1, 1, 1, .5), run=False): # setup a point cloud viewer using vispy and return a drawing function # make a canvas and add simple view canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) view = canvas.central_widget.add_view() # create scatter object and fill in the data # init_pc = np.random.normal(size=(100, 3), scale=0.2) init_pc = X scatter = visuals.Markers() draw_fn = partial(scatter.set_data, edge_color=None, face_color=color, size=5) draw_fn(init_pc) view.add(scatter) # set camera view.camera = 'turntable' # ['turntable','arcball'] # add a colored 3D axis for orientation axis = visuals.XYZAxis(parent=view.scene) return draw_fn
def __init__(self, data_getter_func, live=False): """ Initializes Display :param live: Whether data is live; determines whether data is updated for each draw frame. """ # Make canvas and add view self.pos = None self.canvas = PointMapCanvas(keys='interactive', show=True) self._data_getter = data_getter_func self.view = self.canvas.central_widget.add_view() # visuals constructs visual classes at run-time. # Markers class is defined then. self.points = visuals.Markers(parent=self.view.scene) # not an error self.update_data() self.view.add(self.points) self.view.camera = 'turntable' self.axis = visuals.XYZAxis(parent=self.view.scene) # not an error if live: self.canvas.draw_func = self.update_data
def visualize(pcl,label = None): canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) view = canvas.central_widget.add_view() scatter = visuals.Markers() # pcl = convert_pcl(pcl,label) if (label != None): pcl = convert_pcl(pcl,label) if (label != None): pcl = add_points(pcl,label) # pcl = get_road(pcl) if (label != None): scatter.set_data(pcl[:,:3],edge_color = color_b(pcl[:,-1]), size = 2) else: scatter.set_data(pcl[:,:3], size = 7) # scatter.set_data(, edge_color=None, face_color=(1, 1, 1, .5), size=5) view.add(scatter) view.camera = 'turntable' axis = visuals.XYZAxis(parent=view.scene) vispy.app.run()
def __init__(self, reconstruction, data_path, has_color=1, show_camera=1): self.data_path = data_path self.has_color = has_color self.show_camera = show_camera pos, color = get_points(reconstruction, has_color) # create scatter object and fill in the data self.scatter = visuals.Markers() self.scatter.set_data(pos, edge_color=None, face_color=color, size=5) # self.canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) # self.view = self.canvas.central_widget.add_view() vispy.scene.SceneCanvas.__init__(self, keys='interactive', show=True) self.unfreeze() self.view = self.central_widget.add_view() self.view.add(self.scatter) # size for plotting cameras self.length = 1 self.font_size = 12 if (show_camera): self.camera_nodes, self.text_nodes = \ attach_cameras_to_view(reconstruction, self.view, self.data_path, length=self.length, font_size=self.font_size) self.view.camera = 'arcball' # or try 'turntable' # add a colored 3D axis for orientation self.axis = visuals.XYZAxis(parent=self.view.scene) self.visible = True # self.canvas.app.run() self.app.run()
def __init__(self): self.canvas = SceneCanvas(keys='interactive', show=True) self.grid = self.canvas.central_widget.add_grid() self.view = vispy.scene.widgets.ViewBox(border_color='white', parent=self.canvas.scene) self.grid.add_widget(self.view, 0, 0) # Point Cloud Visualizer self.sem_vis = visuals.Markers() self.view.camera = vispy.scene.cameras.TurntableCamera(up='z', azimuth=90) self.view.add(self.sem_vis) visuals.XYZAxis(parent=self.view.scene) # Object Detection Visualizer self.obj_vis = visuals.Line() self.view.add(self.obj_vis) self.connect = np.asarray([[0, 1], [0, 3], [0, 4], [2, 1], [2, 3], [2, 6], [5, 1], [5, 4], [5, 6], [7, 3], [7, 4], [7, 6]]) self.data = load_data( 'data/data.p') # Change to data.p for your final submission
def reset(self): """ Reset. """ # last key press (it should have a mutex, but visualization is not # safety critical, so let's do things wrong) self.action = "no" # no, next, back, quit are the possibilities # new canvas prepared for visualizing data self.canvas = SceneCanvas(keys='interactive', show=True) # interface (n next, b back, q quit, very simple) self.canvas.events.key_press.connect(self.key_press) self.canvas.events.draw.connect(self.draw) # grid self.grid = self.canvas.central_widget.add_grid() #if self.instances: print("Using instances in visualizer") self.inst_view = vispy.scene.widgets.ViewBox(border_color='white', parent=self.canvas.scene) self.grid.add_widget(self.inst_view, 0, 0) self.inst_vis = visuals.Markers() self.inst_view.camera = 'turntable' self.inst_view.add(self.inst_vis) visuals.XYZAxis(parent=self.inst_view.scene)
def render(self, pcd=None, camera_mode='turntable', point_size=0, auto_clear=True): ''' Render the new pcd :param pcd: the new Open3D pcd data :param camera_mode: camera's mode :param point_size: each point's point size :param auto_clear: True if clear the scene before rendering, false otherwise :return: ''' if auto_clear: self.clear() point_size = self.point_size if point_size == 0 else point_size try: self.pcd = pcd self.view = self.canvas.central_widget.add_view() self.view.parent = self.canvas.scene self.view.camera = camera_mode points = np.asarray(pcd.points) colors = np.asarray(pcd.colors) self.marker = visuals.Markers() self.marker.set_gl_state('translucent', blend=True, depth_test=True) self.marker.set_data(points, edge_color=colors, face_color=colors, size=point_size) self.view.add(self.marker) return True except: raise Scene.SceneError("Unable to render")
def __init__(self, pos, mfc=[0.5, 0.5, 0.5, 0.8], mec=None, mfs=8, mes=1, bgc=[1, 1, 1], scaling_symbol=False, symbol='disc', size=(800, 600)): scene.SceneCanvas.__init__(self, keys=None, show=True, bgcolor=bgc, size=size) self.unfreeze() # allow the creation of new attribute to the class # Create the view and the scatter self.view = self.central_widget.add_view() self.scatter = visuals.Markers(parent=self.view.scene) self.scatter.set_data(pos, face_color=mfc, edge_color=mec, scaling=scaling_symbol, size=mfs, edge_width=mes, symbol=symbol) # Set the camera properties self.view.camera = scene.PanZoomCamera(aspect=1) self.view.camera.set_range() # Settings for the lines that indicate the position of the cursor self.param_tickprop = .01 self.pressxy = (0, 0) self.movexy = (0, 0) tr = self.scene.node_transform(self.scatter) win_xmin, win_ymax = tr.map([0, 0])[:2] win_xmax, win_ymin = tr.map(self.size)[:2] win_xsize, win_ysize = win_xmax - win_xmin, win_ymax - win_ymin tick_size = self.param_tickprop * win_xsize # Create the lines on the border of the viewbox self.top_line = scene.visuals.Line(pos=np.array( [[win_xmin, win_ymax], [win_xmin, win_ymax - tick_size]]), color=[.2, .2, .2, 0.5], width=1, parent=self.view.scene, method='gl') self.right_line = scene.visuals.Line(pos=np.array( [[win_xmax, win_ymin], [win_xmax - tick_size, win_ymin]]), color=[.2, .2, .2, 0.5], width=1, parent=self.view.scene, method='gl') self.bottom_line = scene.visuals.Line(pos=np.array( [[win_xmax, win_ymin], [win_xmax, win_ymin + tick_size]]), color=[.2, .2, .2, 0.5], width=1, parent=self.view.scene, method='gl') self.left_line = scene.visuals.Line(pos=np.array( [[win_xmin, win_ymax], [win_xmin + tick_size, win_ymax]]), color=[.2, .2, .2, 0.5], width=1, parent=self.view.scene, method='gl') # Create the cross on the cursor coord self.cross_hline = scene.visuals.Line(pos=np.array( [[win_xmax, win_ymin], [win_xmax, win_ymin + tick_size]]), color=[0, 0, 0, 1], width=2, parent=self.view.scene, method='gl') self.cross_vline = scene.visuals.Line(pos=np.array( [[win_xmin, win_ymax], [win_xmin + tick_size, win_ymax]]), color=[0, 0, 0, 1], width=2, parent=self.view.scene, method='gl') self.freeze() @self.events.mouse_move.connect def on_mouse_move(event): # Find the cursor position in the windows coordinate tr = self.scene.node_transform(self.scatter) x, y = tr.map(event.pos)[:2] self.movexy = (x, y) # Find the min and max for both axis in the windows coordinate win_xmin, win_ymax = tr.map([0, 0])[:2] win_xmax, win_ymin = tr.map(self.size)[:2] win_xsize, win_ysize = win_xmax - win_xmin, win_ymax - win_ymin tick_size = self.param_tickprop * win_xsize #refresh lines self.top_line.set_data( pos=np.array([[x, win_ymax], [x, win_ymax - tick_size]])) self.right_line.set_data( pos=np.array([[win_xmax, y], [win_xmax - tick_size, y]])) self.bottom_line.set_data( pos=np.array([[x, win_ymin], [x, win_ymin + tick_size]])) self.left_line.set_data( pos=np.array([[win_xmin, y], [win_xmin + tick_size, y]])) self.cross_hline.set_data( pos=np.array([[x - tick_size / 2, y], [x + tick_size / 2, y]])) self.cross_vline.set_data( pos=np.array([[x, y - tick_size / 2], [x, y + tick_size / 2]])) @self.events.mouse_press.connect def on_mouse_press(event): # Find the cursor position in the windows coordinate tr = self.scene.node_transform(self.scatter) x, y = tr.map(event.pos)[:2] self.pressxy = (x, y)
def pcd_vispy(scans=None, img=None, boxes=None, name=None, index=0, vis_size=(800, 600), save_img=False, visible=True, no_gt=False, multi_vis=False, point_size=0.02): if multi_vis: canvas = vispy.scene.SceneCanvas(title=name, keys='interactive', size=vis_size, show=True) else: canvas = vispy.scene.SceneCanvas(title=name, keys='interactive', size=vis_size, show=visible) grid = canvas.central_widget.add_grid() vb = grid.add_view(row=0, col=0, row_span=2) vb_img = grid.add_view(row=1, col=0) vb.camera = 'turntable' vb.camera.elevation = 90 # 21.0 vb.camera.center = (6.5, -0.5, 9.0) vb.camera.azimuth = -90 # -75.5 vb.camera.scale_factor = 63 # 32.7 if scans is not None: if not isinstance(scans, list): pos = scans[:, :3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(1, 1, 1, 1), size=point_size, scaling=True) vb.add(scatter) else: pos = scans[0][:, :3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(1, 1, 1, 1), size=point_size, scaling=True) vb.add(scatter) pos = scans[1][:, :3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(0, 1, 1, 1), size=point_size, scaling=True) vb.add(scatter) axis = visuals.XYZAxis() vb.add(axis) if img is None: img = np.zeros(shape=[1, 1, 3], dtype=np.float32) image = visuals.Image(data=img, method='auto') vb_img.camera = 'turntable' vb_img.camera.elevation = -90.0 vb_img.camera.center = (2100, -380, -500) vb_img.camera.azimuth = 0.0 vb_img.camera.scale_factor = 1500 vb_img.add(image) if boxes is not None: gt_indice = np.where(boxes["cls_rpn"] == 4)[0] gt_cnt = len(gt_indice) boxes_cnt = boxes["center"].shape[0] i = 0 for k in range(boxes_cnt): radio = max(boxes["score"][k] - 0.5, 0.005) * 2.0 color = (0, radio, 0, 1) # Green if boxes["cls_rpn"][k] == 4: # gt boxes i = i + 1 vsp_box = visuals.Box(depth=boxes["size"][k][0], width=boxes["size"][k][1], height=boxes["size"][k][2], color=(0.3, 0.4, 0.0, 0.06), edge_color='pink') mesh_box = vsp_box.mesh.mesh_data mesh_border_box = vsp_box.border.mesh_data vertices = mesh_box.get_vertices() center = np.array([ boxes["center"][k][0], boxes["center"][k][1], boxes["center"][k][2] ], dtype=np.float32) vertices_roa_trans = box_rot_trans(vertices, -boxes["yaw"][k][0], center) # mesh_border_box.set_vertices(vertices_roa_trans) mesh_box.set_vertices(vertices_roa_trans) vb.add(vsp_box) if True: text = visuals.Text(text='det: ({}/{})'.format(i, gt_cnt), color='white', face='OpenSans', font_size=12, pos=[ boxes["center"][k][0], boxes["center"][k][1], boxes["center"][k][2] ], anchor_x='left', anchor_y='top', font_manager=None) vb.add(text) elif (boxes["cls_rpn"][k] + boxes["cls_cube"][k] ) == 0: # True negative cls rpn divided by cube vb.add( line_box(boxes["center"][k], boxes["size"][k], -boxes["yaw"][k], color=color)) elif (boxes["cls_rpn"][k] + boxes["cls_cube"][k] ) == 1: # False negative cls rpn divided by cube vb.add( line_box(boxes["center"][k], boxes["size"][k], -boxes["yaw"][k], color="red")) elif (boxes["cls_rpn"][k] + boxes["cls_cube"][k] ) == 2: # False positive cls rpn divided by cube vb.add( line_box(boxes["center"][k], boxes["size"][k], -boxes["yaw"][k], color="blue")) elif (boxes["cls_rpn"][k] + boxes["cls_cube"][k] ) == 3: # True positive cls rpn divided by cube vb.add( line_box(boxes["center"][k], boxes["size"][k], -boxes["yaw"][k], color="yellow")) text = visuals.Text( text=str(k), color=color, face='OpenSans', font_size=12, pos=[ boxes["center"][k][0] - boxes["size"][k][0] / 2, boxes["center"][k][1] - boxes["size"][k][1] / 2, boxes["center"][k][2] - boxes["size"][k][2] / 2 ], anchor_x='left', anchor_y='top', font_manager=None) vb.add(text) if save_img: folder = path_add(cfg.TEST_RESULT, cfg.RANDOM_STR) if not os.path.exists(folder): os.makedirs(folder) fileName = path_add(folder, str(index).zfill(6) + '.png') res = canvas.render(bgcolor='black')[:, :, 0:3] vispy_file.write_png(fileName, res) @canvas.connect def on_key_press(ev): if ev.key.name in '+=': a = vb.camera.get_state() print(a) if visible: pass vispy.app.run() return canvas
def __init__(self, name, xyz, data=None, color='red', alpha=1., symbol='disc', radius_min=5., radius_max=10., edge_width=0., edge_color='black', system='mni', mask=None, mask_color='gray', text=None, text_size=3., text_color='black', text_bold=False, text_translate=(0., 2., 0.), visible=True, transform=None, parent=None, verbose=None, _z=-10., **kw): """Init.""" VisbrainObject.__init__(self, name, parent, transform, verbose, **kw) # _______________________ CHECKING _______________________ # XYZ : sh = xyz.shape assert sh[1] in [2, 3] self._n_sources = sh[0] pos = xyz if sh[1] == 3 else np.c_[xyz, np.full((len(self),), _z)] # Radius min and max : assert all([isinstance(k, (int, float)) for k in ( radius_min, radius_max)]) radius_max = max(radius_min, radius_max) self._radius_min, self._radius_max = radius_min, radius_max # Data : if data is None: data = np.ones((len(self),)) else: data = np.asarray(data).ravel() assert len(data) == len(self) self._data = vispy_array(data) # System : pos = pos if system == 'mni' else tal2mni(pos) self._xyz = vispy_array(pos) # Color : self._color = color # Edges : self._edge_color, self._edge_width = edge_color, edge_width # Mask : if mask is None: mask = [False] * len(self) self._mask = np.asarray(mask).ravel().astype(bool) assert len(self._mask) == len(self) self._mask_color = color2vb(mask_color) # Text : self._text_size = text_size self._text_color = text_color self._text_translate = text_translate # _______________________ MARKERS _______________________ self._sources = visuals.Markers(pos=self._xyz, name='Markers', edge_color=edge_color, edge_width=edge_width, symbol=symbol, parent=self._node) # _______________________ TEXT _______________________ tvisible = text is None self._text = [''] * len(self) if tvisible else text self._text = np.array(self._text) assert len(self._text) == len(self) self._sources_text = visuals.Text(self._text, pos=self._xyz, bold=text_bold, name='Text', color=color2vb(text_color), font_size=text_size, parent=self._node) self._sources_text.visible = not tvisible tr = vist.STTransform(translate=text_translate) self._sources_text.transform = tr # _______________________ UPDATE _______________________ # Radius / color : self.visible = visible self._update_radius() self._update_color() self.alpha = alpha
def __init__(self, exp_df_path, trial_df_path, rig_leds_path): exp_df = pd.read_pickle(exp_df_path) trial_df = pd.read_pickle(trial_df_path) self.df = exp_df.join(trial_df.drop('block', axis=1), on='trial_number') self.rig_leds = np.load(rig_leds_path) self.precalculate_data() verts, faces, normals, nothin = vispy.io.read_mesh( os.path.join(fh.PACKAGE_DIR, '../datafiles', 'head.obj')) verts = np.einsum('ni,ji->nj', (verts - verts.mean(axis=0)), fh.from_yawpitchroll(180, 90, 0)) #verts = verts - verts.mean(axis=0) # add SceneCanvas first to create QApplication object before widget self.vispy_canvas = vispy.scene.SceneCanvas(create_native=True, vsync=True, show=True, bgcolor=(0.2, 0.2, 0.2, 0)) super(ExperimentVisualization, self).__init__() #self.setAttribute(Qt.WA_TranslucentBackground) self.timer = vispy.app.Timer(1 / 30, start=False, connect=self.advance_frame) self.n_trials = len(self.df) self.current_trial = 0 self.i_frame = 0 self.current_row = self.df.iloc[self.current_trial] self.current_R_helmet = None self.current_gaze_normals = None self.current_ref_points = None self.vispy_view = self.vispy_canvas.central_widget.add_view() self.vispy_view.camera = 'turntable' self.vispy_view.camera.center = self.rig_leds[127, :] + ( self.rig_leds[0, :] - self.rig_leds[127, :]) + ( self.rig_leds[254, :] - self.rig_leds[127, :]) self.vispy_view.camera.fov = 40 self.vispy_view.camera.distance = 1500 self.main_layout = QtWidgets.QVBoxLayout() self.setLayout(self.main_layout) self.frame_slider = QtWidgets.QSlider(Qt.Horizontal) self.frame_slider.setMinimum(0) self.frame_slider.valueChanged.connect(self.on_slider_change) self.trial_picker = QtWidgets.QSpinBox() self.trial_picker.setMaximum(self.n_trials) self.trial_picker.valueChanged.connect(self.on_picker_change) self.trial_changed.connect(self.load_trial) self.frame_changed.connect(self.load_frame) self.picker_slider_layout = QtWidgets.QHBoxLayout() self.main_layout.addWidget(self.vispy_canvas.native) self.main_layout.addLayout(self.picker_slider_layout) self.animation_button = QtWidgets.QPushButton('Start Animation') self.picker_slider_layout.addWidget(self.animation_button) self.animation_button.clicked.connect(self.toggle_animation) self.frame_label = QtWidgets.QLabel('Frame') self.picker_slider_layout.addWidget(self.frame_label) self.picker_slider_layout.addWidget(self.frame_slider) self.picker_slider_layout.addWidget(QtWidgets.QLabel('Trial')) self.picker_slider_layout.addWidget(self.trial_picker) self.rig_vis = visuals.Markers() self.rig_vis.set_gl_state(depth_test=False) self.rig_vis.antialias = 0 self.vispy_view.add(self.rig_vis) self.helmet_vis = visuals.Markers() self.vispy_view.add(self.helmet_vis) self.gaze_vis = visuals.Line() self.vispy_view.add(self.gaze_vis) self.head_mesh = visuals.Mesh(vertices=verts, shading='smooth', faces=faces, mode='triangles', color=(0.5, 0.55, 0.7)) self.head_mesh.shininess = 0 self.head_mesh.light_dir = [0, 1, 1] # self.head_mesh.light_color = np.array((1, 1, 0.95)) * 0.8 self.head_mesh.ambient_light_color = np.array((0.98, 0.98, 1)) * 0.2 self.head_mesh.attach(Alpha(0.3)) self.head_mesh.set_gl_state(depth_test=True, cull_face=True) self.head_mesh_transform = MatrixTransform() self.head_mesh.transform = self.head_mesh_transform self.vispy_view.add(self.head_mesh) self.trial_changed.emit(0) self.frame_changed.emit(0) self.show() vispy.app.run()
def run3DVisualizationIPM(ipmPoints, centroids, violations, frame, render=False): ''' Takes all the world coordinates produced by the IPM method, and their color values and plots them in 3D space, using vispy. Also draws halo cylinders around 3D points corresponding to people centroids (from efficientdet bounding boxes). Also draws 3D lines (tubes) between the halos that represent a pair of people which are violating the 6' restriction. Parameters: ipmPoints (list): World coordinates with color, generated by the ipm method (x,y,z,[r,g,b]) centroids (list): A list of lists, where each inner list object is a 3D world point, representing the centroid of a person (found using the efficientdet bounding boxes), in format [x,y,z] violations (list): A list of lists, where each inner list is a pair of integers, which are two indices in the bBoxes list representing a pair of people violating the 6' restriction. Formatted as [[pi1,pi2],[pi3,pi4]] frame (int): frame number for the filename to save to render (bool): whether or not to render canvas to a file Returns: ''' # Create canvas to draw everything canvas = vispy.scene.SceneCanvas(keys='interactive', show=True, size=(1920, 1080)) view = canvas.central_widget.add_view() # Unpack ipm points ipmPos = [] ipmColor = [] for point in ipmPoints: ipmPos.append([point[0], point[1], point[2]]) r = point[3][0] g = point[3][1] b = point[3][2] ipmColor.append([r, g, b]) pos = np.array(ipmPos) colors = np.array(ipmColor) # 3D scatter plot to show depth map pointcloud scatter = visuals.Markers() scatter.antialias = 0 scatter.set_data(pos, edge_color=None, face_color=colors, size=5) view.add(scatter) # Draw cylinders around centroids for point in centroids: x, y, z = point cyl_mesh = vispy.geometry.create_cylinder(10, 10, radius=[500, 500], length=50) # Move cylinder to correct location in 3D space # Make sure to negate the y value, otherwise everything will be mirrored vertices = cyl_mesh.get_vertices() center = np.array([x, -y, z + 150], dtype=np.float32) vtcs = np.add(vertices, center) cyl_mesh.set_vertices(vtcs) cyl = visuals.Mesh(meshdata=cyl_mesh, color='g') view.add(cyl) # Draw lines between violating people for pair in violations: x1, y1, z1 = centroids[pair[0]] x2, y2, z2 = centroids[pair[1]] #lin = visuals.Line(pos=np.array([[x1,-y1,z1+1],[x2,-y2,z2+1]]), color='r', method='gl') #view.add(lin) tube = visuals.Tube(points=np.array([[x1, -y1, z1 + 150], [x2, -y2, z2 + 150]]), radius=50, color='red') view.add(tube) view.camera = 'turntable' # or try 'arcball' view.camera.elevation = 30.5 view.camera.azimuth = -78.5 view.camera.distance = 8250.000000000002 view.camera.fov = 60 # Add a colored 3D axis for orientation axis = visuals.XYZAxis(parent=view.scene) if (render): img = canvas.render() fname = "out_renders/IPM" + str(frame) + ".png" io.write_png(fname, img) else: vispy.app.run()
def __init__(self, xyz=None, channels=None, system='cartesian', unit='degree', title=None, title_color='black', title_size=20., line_color='black', line_width=4., chan_size=12., chan_offset=(0., 0., 0.), chan_mark_color='white', chan_mark_symbol='disc', chan_txt_color='black', bgcolor='white', cbar=True, cb_txt_size=10., margin=.05, parent=None): """Init.""" # ======================== VARIABLES ======================== self._bgcolor = color2vb(bgcolor) scale = 800. # fix GL bugs for small plots pos = np.zeros((1, 3), dtype=np.float32) # Colors : title_color = color2vb(title_color) line_color = color2vb(line_color) chan_txt_color = color2vb(chan_txt_color) self._chan_mark_color = color2vb(chan_mark_color) self._chan_mark_symbol = chan_mark_symbol # Disc interpolation : self._interp = .1 self._pix = 64 csize = int(self._pix / self._interp) if self._interp else self._pix l = csize / 2 # noqa # ======================== NODES ======================== # Main topoplot node : self.node = scene.Node(name='Topoplot', parent=parent) self.node.transform = vist.STTransform(scale=[scale] * 3) # Headset + channels : self.node_headfull = scene.Node(name='HeadChan', parent=self.node) # Headset node : self.node_head = scene.Node(name='Headset', parent=self.node_headfull) # Channel node : self.node_chan = scene.Node(name='Channels', parent=self.node_headfull) self.node_chan.transform = vist.STTransform(translate=(0., 0., -10.)) # Cbar node : self.node_cbar = scene.Node(name='Channels', parent=self.node) # Dictionaries : kw_line = { 'width': line_width, 'color': line_color, 'parent': self.node_head } # ======================== PARENT VISUALS ======================== # Main disc : self.disc = visuals.Image(pos=pos, name='Disc', parent=self.node_head, interpolation='bilinear') # Title : self.title = visuals.Text(text=title, pos=(0., .6, 0.), name='Title', parent=self.node, font_size=title_size, color=title_color, bold=True) self.title.font_size *= 1.1 # ======================== HEAD / NOSE / EAR ======================== # ------------------ HEAD ------------------ # Head visual : self.head = visuals.Line(pos=pos, name='Head', **kw_line) # Head circle : theta = np.arange(0, 2 * np.pi, 0.001) head = np.full((len(theta), 3), -1., dtype=np.float32) head[:, 0] = l * (1. + np.cos(theta)) head[:, 1] = l * (1. + np.sin(theta)) self.head.set_data(pos=head) # ------------------ NOSE ------------------ # Nose visual : self.nose = visuals.Line(pos=pos, name='Nose', **kw_line) # Nose data : wn, hn = csize * 50. / 512., csize * 30. / 512. nose = np.array([[l - wn, 2 * l - wn, 2.], [l, 2 * l + hn, 2.], [l, 2 * l + hn, 2.], [l + wn, 2 * l - wn, 2.]]) self.nose.set_data(pos=nose, connect='segments') # ------------------ EAR ------------------ we, he = csize * 10. / 512., csize * 30. / 512. ye = l + he * np.sin(theta) # Ear left data : self.earL = visuals.Line(pos=pos, name='EarLeft', **kw_line) # Ear left visual : ear_l = np.full((len(theta), 3), 3., dtype=np.float32) ear_l[:, 0] = 2 * l + we * np.cos(theta) ear_l[:, 1] = ye self.earL.set_data(pos=ear_l) # Ear right visual : self.earR = visuals.Line(pos=pos, name='EarRight', **kw_line) # Ear right data : ear_r = np.full((len(theta), 3), 3., dtype=np.float32) ear_r[:, 0] = 0. + we * np.cos(theta) ear_r[:, 1] = ye self.earR.set_data(pos=ear_r) # ================== CHANNELS ================== # Channel's markers : self.chanMarkers = visuals.Markers(pos=pos, name='ChanMarkers', parent=self.node_chan) # Channel's text : self.chanText = visuals.Text(pos=pos, name='ChanText', parent=self.node_chan, anchor_x='center', color=chan_txt_color, font_size=chan_size) # ================== CAMERA ================== self.rect = ((-scale / 2) * (1 + margin), (-scale / 2) * (1 + margin), scale * (1. + cbar * .3 + margin), scale * (1.11 + margin)) # ================== CBAR ================== if cbar: self.cbar = CbarVisual(cbtxtsz=1.2 * cb_txt_size, txtsz=cb_txt_size, txtcolor=title_color, cbtxtsh=2., parent=self.node_cbar) self.node_cbar.transform = vist.STTransform(scale=(.6, .4, 1.), translate=(.6, 0., 0.)) # ================== COORDINATES ================== auto = self._get_channel_coordinates(xyz, channels, system, unit) if auto: eucl = np.sqrt(self._xyz[:, 0]**2 + self._xyz[:, 1]**2).max() self.node_head.transform = vpnormalize(head, dist=2 * eucl) # Rescale between (-1:1, -1:1) = circle : circle = vist.STTransform(scale=(.5 / eucl, .5 / eucl, 1.)) self.node_headfull.transform = circle # Text translation : tr = np.array([0., .8, 0.]) + np.array(chan_offset) else: # Get coordinates of references along the x and y-axis : ref_x, ref_y = self._get_ref_coordinates() # Recenter the topoplot : t = vist.ChainTransform() t.prepend(vprecenter(head)) # Rescale (-ref_x:ref_x, -ref_y:ref_y) (ref_x != ref_y => ellipse) coef_x = 2 * ref_x / head[:, 0].max() coef_y = 2 * ref_y / head[:, 1].max() t.prepend(vist.STTransform(scale=(coef_x, coef_y, 1.))) self.node_head.transform = t # Rescale between (-1:1, -1:1) = circle : circle = vist.STTransform(scale=(.5 / ref_x, .5 / ref_y, 1.)) self.node_headfull.transform = circle # Text translation : tr = np.array([0., .04, 0.]) + np.array(chan_offset) self.chanText.transform = vist.STTransform(translate=tr) # ================== GRID INTERPOLATION ================== # Interpolation vectors : x = y = np.arange(0, self._pix, 1) xnew = ynew = np.arange(0, self._pix, self._interp) # Grid interpolation function : def _grid_interpolation(grid): f = interp2d(x, y, grid, kind='linear') return f(xnew, ynew) self._grid_interpolation = _grid_interpolation
def show_in_vispy(path): # Make a canvas and add simple view canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) view = canvas.central_widget.add_view() # # generate data 使用随机数据的话把这块反注释掉 # pos = np.random.normal(size=(100000, 3), scale=0.2) # print(pos) # # one could stop here for the data generation, the rest is just to make the # # data look more interesting. Copied over from magnify.py # centers = np.random.normal(size=(50, 3)) # indexes = np.random.normal(size=100000, loc=centers.shape[0]/2., # scale=centers.shape[0]/3.) # indexes = np.clip(indexes, 0, centers.shape[0]-1).astype(int) # scales = 10**(np.linspace(-2, 0.5, centers.shape[0]))[indexes][:, np.newaxis] # pos *= scales # pos += centers[indexes] # print(pos) # scatter = visuals.Markers() # scatter.set_data(pos, edge_color=None, face_color=(1, 1, 1, .5), size=5) # # # # # # # # # 使用 kitti 数据, n*3 # # img_id = 17 # 2,3 is not able for pcl; # # path = r'D:\KITTI\Object\training\velodyne\%06d.bin' % img_id ## Path ## need to be changed # # points = np.fromfile(path, dtype=np.float32).reshape(-1, 4) # # # # # # 读取点云 # with open(path,"r") as f: # lines = f.readlines() # for i in range(len(lines)): # lines[i] = list(map(float, lines[i].strip("\n").split(" "))) # lines = np.array(lines) # print(len(lines)) with open(path, "r") as f: lines = f.readlines() lines = lines[13:] points = np.ones((len(lines), 3)) colors = [] for i in range(len(lines)): points[i, :3] = list( map(float, lines[i].strip("\n").split(" ")[:3])) colors.append( tuple(list(map(float, lines[i].strip("\n").split(" ")[-3:])))) colors = np.array(colors) / 255 # colors[:,3] = 0.5 # for i in range(len(colors)): # colors[i] = tuple(colors[i]) print(colors) print(points) # print(lines) # create scatter object and fill in the data scatter = visuals.Markers() scatter.set_data(points[:, :3], edge_color=None, face_color=colors, size=4) view.add(scatter) view.camera = 'turntable' # or try 'arcball' # add a colored 3D axis for orientation axis = visuals.XYZAxis(parent=view.scene) if sys.flags.interactive != 1: vispy.app.run()
def pcd_vispy_standard(scans=None, img=None, boxes=None, name=None, index=0, vis_size=(800, 600), save_img=False, visible=True, multi_vis=False, point_size=0.02, lidar_view_set=None): if multi_vis: canvas = vispy.scene.SceneCanvas(title=name, keys='interactive', size=vis_size, show=True) else: canvas = vispy.scene.SceneCanvas(title=name, keys='interactive', size=vis_size, show=visible) grid = canvas.central_widget.add_grid() vb = grid.add_view(row=0, col=0, row_span=2) vb_img = grid.add_view(row=1, col=0) if lidar_view_set is None: vb.camera = 'turntable' vb.camera.elevation = 90 # 21.0 vb.camera.center = (6.5, -0.5, 9.0) vb.camera.azimuth = -90 # -75.5 vb.camera.scale_factor = 63 # 32.7 else: vb.camera = 'turntable' vb.camera.elevation = lidar_view_set['elevation'] # 21.0 vb.camera.center = lidar_view_set['center'] vb.camera.azimuth = lidar_view_set['azimuth'] vb.camera.scale_factor = lidar_view_set['scale_factor'] if scans is not None: if not isinstance(scans, list): pos = scans[:, :3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(1, 1, 1, 1), size=point_size, scaling=True) vb.add(scatter) else: pos = scans[0][:, :3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(1, 1, 1, 1), size=point_size, scaling=True) vb.add(scatter) pos = scans[1][:, :3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(0, 1, 1, 1), size=0.1, scaling=True) vb.add(scatter) axis = visuals.XYZAxis() vb.add(axis) if img is None: img = np.zeros(shape=[1, 1, 3], dtype=np.float32) image = visuals.Image(data=img, method='auto') vb_img.camera = 'turntable' vb_img.camera.elevation = -90.0 vb_img.camera.center = (1900, 160, -1300) vb_img.camera.azimuth = 0.0 vb_img.camera.scale_factor = 1500 vb_img.add(image) if boxes is not None: if len(boxes.shape) == 1: boxes = boxes.reshape(-1, boxes.shape[0]) # one box: type,xyz,lwh,yaw,[score,reserve1,reserve2] for box in boxes: if box[0] == 1: # type:car vb.add(line_box_stand(box, color="yellow")) elif box[0] == 2: # type:Perdestrain vb.add(line_box_stand(box, color="red")) elif box[0] == 3: # type:Cyclist vb.add(line_box_stand(box, color="blue")) elif box[0] == 4: # type:Van vb.add(line_box_stand(box, color="pink")) else: vb.add(line_box_stand(box, color="green")) if save_img: folder = path_add(cfg.TEST_RESULT, cfg.RANDOM_STR) if not os.path.exists(folder): os.makedirs(folder) fileName = path_add(folder, str(index).zfill(6) + '.png') res = canvas.render(bgcolor='black')[:, :, 0:3] vispy_file.write_png(fileName, res) @canvas.connect def on_key_press(ev): if ev.key.name in '+=': a = vb.camera.get_state() print(a) if visible: pass vispy.app.run() return canvas
def gen_skeleton(blob3d): #------------------------------------------------ # NOTE OUTDATED, keeping only for reference #------------------------------------------------ # Begin by creating a 3d array, with each element either None or the id of the pixel # Then create a second 3d array, with the distances from each internal point to the closest edge point # Find internals by doing all pixels - edge_pixels print("CALLED GEN_SKELETON!!!!") import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') xdim = blob3d.maxx - blob3d.minx + 1 ydim = blob3d.maxy - blob3d.miny + 1 zdim = blob3d.highslideheight - blob3d.lowslideheight + 1 minx = blob3d.minx miny = blob3d.miny minz = blob3d.lowslideheight def pixel_to_pos(pixel): # Returns the (x,y,z) location of the pixel in any one of the 3d arrays return (pixel.x - minx, pixel.y - miny, pixel.z - minz) def distance_from_offset(x_offset, y_offset, z_offset): return math.sqrt( math.pow(x_offset, 2) + math.pow(y_offset, 2) + math.pow(z_offset, 2)) def inBounds(x, y, z): if x >= 0 and y >= 0 and z >= 0 and x < xdim and y < ydim and z < zdim: return True return False def find_nearest_neighbor(x, y, z, arr, recur=1): """ :param x: X coordinate within given arr :param y: Y coordinate within given arr :param z: Z coordinate within given arr :param arr: An array populated by the ids of pixels :param recur: How far outwards to extend the 'search cube' :return: (x_coor, y_coor, z_coor, distance, pixel_id) """ # print("Finding nearest neighbor of: " + str((x, y, z, recur))) possible_coordinates = [] # Contains coordinate tuples (x,y,z) # Because am using cube instead of spheres, need to remember all matches and then find the best # arr should # TODO restrict the ranges so that they don't overlap # X restricts Y and Z # Y restricts Z # Z restricts nothing for x_offset in [-recur, recur]: curx = x + x_offset for y_offset in range(-recur, recur + 1): # +1 to include endcap cury = y + y_offset for z_offset in range(-recur, recur + 1): # +1 to include endcap curz = z + z_offset if inBounds(curx, cury, curz) and not np.isnan(arr[curx][cury][curz]): possible_coordinates.append( (curx, cury, curz, distance_from_offset(x_offset, y_offset, z_offset), int(arr[curx][cury][curz]))) # x, y, z, distance, pixel_id # print("Coordinates found: " + str(possible_coordinates)) if len(possible_coordinates) == 0: # print("----Making a recursive call!") return find_nearest_neighbor(x, y, z, arr, recur + 1) # TODO Y and Z else: # Find the closest coordinate possible_coordinates.sort( key=lambda x_y_z_dist_id: x_y_z_dist_id[3]) # Sort by distance # print("SORTED POSSIBLE COORDINATES: " + str(possible_coordinates)) return possible_coordinates[0] # TODO have an option to create a shell around the blob3d, by taking the highest and lowest levels, and making them all count temporarily as # edge pixels. This way, the effects of segmentation will be less dependent on the scan direction edge_pixels = blob3d.get_edge_pixels() # Actual pixels not ids all_pixels = blob3d.get_pixels() inner_pixels = [ Pixel.get(cur_pixel) for cur_pixel in (set(pixel.id for pixel in all_pixels) - set(pixel.id for pixel in edge_pixels)) ] edge_array = np.empty((xdim, ydim, zdim)) # , dtype=np.int) inner_array = np.empty((xdim, ydim, zdim)) # , dtype=np.int) distances = np.empty((xdim, ydim, zdim), dtype=np.float) edge_array[:] = np.nan inner_array[:] = np.nan distances[:] = np.nan for pixel in edge_pixels: x, y, z = pixel_to_pos(pixel) edge_array[x][y][z] = pixel.id for pixel in inner_pixels: x, y, z = pixel_to_pos(pixel) inner_array[x][y][z] = pixel.id inner_pos = np.zeros([len(inner_pixels), 3]) near_pos = np.zeros([len(inner_pixels), 3]) line_endpoints = np.zeros([2 * len(inner_pixels), 3]) index_distance_id = np.zeros([len(inner_pixels), 3]) maxdim = max([xdim, ydim, zdim]) # Adjusting z visualization pixel_nid_nx_ny_nz_dist = [] marker_colors = np.zeros((len(inner_pixels), 4)) mycolors = [ 'r', 'orange', 'yellow', 'white', 'lime', 'g', 'teal', 'blue', 'purple', 'pink' ] # HACK import vispy myrgba = list( vispy.color.ColorArray(color_str).rgba for color_str in mycolors) for index, pixel in enumerate(inner_pixels): # print("Pixel: " + str(pixel)) x, y, z = pixel_to_pos(pixel) inner_pos[index] = x / xdim, y / ydim, z / zdim x_y_z_dist_id = find_nearest_neighbor(x, y, z, edge_array) pixel_nid_nx_ny_nz_dist.append( (pixel, x_y_z_dist_id[4], x_y_z_dist_id[0], x_y_z_dist_id[1], x_y_z_dist_id[2], x_y_z_dist_id[3])) near_pos[index] = (x_y_z_dist_id[0] / xdim, x_y_z_dist_id[1] / ydim, x_y_z_dist_id[2] / zdim) marker_colors[index] = myrgba[int(x_y_z_dist_id[3]) % len(myrgba)] line_endpoints[2 * index] = (x_y_z_dist_id[0] / xdim, x_y_z_dist_id[1] / ydim, x_y_z_dist_id[2] / zdim) line_endpoints[2 * index + 1] = (x / xdim, y / ydim, z / zdim) # index_distance_id[index] = [index, x_y_z_dist_id[3], x_y_z_dist_id[4]] # Sort distances; need to maintain index for accessing id later pixel_nid_nx_ny_nz_dist = sorted(pixel_nid_nx_ny_nz_dist, key=lambda entry: entry[5], reverse=True) # TODO find ridge # Strategy: Iterative, use either the highest n or x percent to find cutoff..? ridge = [pixel_nid_nx_ny_nz_dist[0]] ridge_ends = [pixel_nid_nx_ny_nz_dist[0] ] # Preferable to keep at 2 if we can # HACK TODO for i in pixel_nid_nx_ny_nz_dist: print(' ' + str(i)) # min_threshold = int(pixel_nid_nx_ny_nz_dist[int(.1 * len(pixel_nid_nx_ny_nz_dist))]) # pixel_costs = dict() # for pnnnnd in pixel_nid_nx_ny_nz_dist: # pixel_costs[pnnnnd[0].id] = pnnnnd[5] # Mapping inner pixel's id to its distance (to the closest edge_pixel) # # # # # # n, bins, patches = plt.hist([idi[5] for idi in pixel_nid_nx_ny_nz_dist], bins=np.linspace(0,10,50)) # # hist = np.histogram( [idi[4] for idi in pixel_nid_nx_ny_nz_dist] , bins=np.arange(10)) # plt.show() import vispy.io import vispy.scene from vispy.scene import visuals from vispy.util import keys canvas = vispy.scene.SceneCanvas(size=(1200, 1200), keys='interactive', show=True) view = canvas.central_widget.add_view() view.camera = vispy.scene.cameras.FlyCamera(parent=view.scene, fov=30, name='Fly') # view.camera = vispy.scene.cameras.TurntableCamera(fov=0, azimuth=80, parent=view.scene, distance=1, # elevation=-55, name='Turntable') # view.camera = vispy.scene.cameras.ArcballCamera(parent=view.scene, fov=50, distance=1, # name='Arcball') inner_markers = visuals.Markers() near_markers = visuals.Markers() lines = visuals.Line(method=Config.linemethod) lines.set_data(pos=line_endpoints, connect='segments', color='y') inner_markers.set_data(inner_pos, face_color=marker_colors, size=10) near_markers.set_data(near_pos, face_color='g', size=10) print("Inner pos: " + str(inner_pos)) print("Near pos: " + str(near_pos)) view.add(inner_markers) view.add(near_markers) view.add(lines) vispy.app.run() print('------------')
def __init__(self, view): self.view = view Plot3D = scene.visuals.create_visual_node(visualsplot.LinePlotVisual) comp_points = {} Lx = 1 * params.arm_length * sqrt(2) / 2 cage_width = params.cage_width motors = np.array([[Lx, Lx, -Lx, -Lx], [Lx, -Lx, -Lx, Lx], [0, 0, 0, 0]]).T # Arms points_arm1 = motors[:3:2, :] arm1 = Plot3D(points_arm1, width=4.0, color='blue', face_color=(0., 0., 0, 0.), parent=view.scene) comp_points[arm1] = points_arm1 points_arm2 = motors[1:4:2, :] arm2 = Plot3D(points_arm2, width=4.0, color='blue', face_color=(0., 0., 0, 0.), parent=view.scene) comp_points[arm2] = points_arm2 # Motors motor_points = visuals.Markers() motor_points.set_data(motors, edge_color=None, face_color=(1, 1, 1, .5), size=15) view.add(motor_points) comp_points[motor_points] = motors # Front tag # Centroid scatter = visuals.Markers() pos = np.array([[Lx / 2], [0], [0]]).T scatter.set_data(pos, edge_color=None, face_color=(1, 0, 0, .5), size=10) view.add(scatter) comp_points[scatter] = pos # Cage down side l2c = cage_width / abs(Lx) / 2 # Lx to cage width cage_color = (0, 1, 0, .5) cage_d_points = np.vstack((motors, motors[0, :])) * l2c cage_d_points[:, 2] -= .3 * Lx cage_d = Plot3D(cage_d_points, width=1.0, color=cage_color, face_color=(0., 0., 0, 0.), parent=view.scene, marker_size=0) comp_points[cage_d] = cage_d_points # Cage top cage_t_points = np.vstack((motors, motors[0, :])) * l2c cage_t_points[:, 2] += .7 * Lx cage_t = Plot3D(cage_t_points, width=1.0, color=cage_color, face_color=(0., 0., 0, 0.), parent=view.scene, marker_size=0) comp_points[cage_t] = cage_t_points # lageral for i in range(4): cage_li_points = np.vstack((l2c * motors[i, :] - np.array([[0, 0, .3 * Lx], [0, 0, .3 * Lx]]), l2c * motors[i, :] + np.array([[0, 0, .7 * Lx], [0, 0, .7 * Lx]]))) cage_li = Plot3D(cage_li_points, width=1.0, color=cage_color, face_color=(0., 0., 0, 0.), parent=view.scene, marker_size=0) comp_points[cage_li] = cage_li_points self._comp_points = comp_points
view = canvas.central_widget.add_view() view.bgcolor = '#111111' view.camera = [ 'perspective', 'panzoom', 'fly', 'arcball', 'base', 'turntable', None ][2] if 1: view.camera.fov = 60 view.camera.scale_factor = 0.7 view.camera.keymap['Right'] = (1, 5) view.camera.keymap['Left'] = (-1, 5) view.camera.keymap['Up'] = (1, 4) view.camera.keymap['Down'] = (-1, 4) print(' - Camera View : ', view.camera) axis = visuals.XYZAxis(parent=view.scene) scatter = visuals.Markers(parent=view.scene) canvas.show() # Update t = min(t + 1, len(points) - 1) update([points[t], colors[t]]) @canvas.events.key_press.connect def keypress(e): global t if e._key.name == '=': print(' - File Index : ', t) t = min(t + 1, len(points) - 1) update([points[t], colors[t]])
# Project from their unit circles out to the curves. A = np.linalg.cholesky(cov) xg = np.matmul(A[None, ...], x[:, None, :, None]) xg = xg.squeeze() + mu[None, :, :] xg = xg.reshape(-1, D) for m, c in zip(mu, cov): Ellipse(mu=m, cov=c, std=.7, parent=view.scene) # And reproject with slightly different mus (so we can differentiate the balls.) # This should project all the projections into the sphere "manifold" of each gaussian. # So this will yield K^2 ellipses, where K of them are spheres (the "source" manifold). xb = obs.np_el_backproject_all(x=xg, mu=mu, Ainv=np.linalg.inv(A)).reshape(-1, D) # create scatter object and fill in the data scatter = visuals.Markers() scatter.set_data(x, edge_color=colors, face_color=colors, size=2) scatter2 = visuals.Markers() scatter2.set_data(xg, size=5, face_color='blue') scatter3 = visuals.Markers() scatter3.set_data(xb, size=10, face_color='red') view.add(scatter) view.add(scatter2) view.add(scatter3) view.camera = 'turntable' # or try 'arcball' # add a colored 3D axis for orientation axis = visuals.XYZAxis(parent=view.scene)
def main(vid): # Video capture cap = cv2.VideoCapture(vid) print("fps is ", cap.get(cv2.CAP_PROP_FPS)) # Make a canvas and add simple view canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) view = canvas.central_widget.add_view() # create scatter object scatter = visuals.Markers() # generate data or figure out how to prevent crash without data ^^ pos = np.random.normal(size=(100000, 3), scale=0.2) scatter.set_data(pos, edge_color=None, face_color=(1, 1, 1, .5), size=5) view.add(scatter) #configure view view.camera = 'turntable' # or try 'arcball' axis = visuals.XYZAxis(parent=view.scene) #load model sess = tf.Session() model = RunModel(config, sess=sess) # verts_anim = [] i = 0 while True: # Capture frame-by-frame # print(cap.read()) ret, frame = cap.read() if frame is None: break processed, proc_param, img = preprocess_image(frame) # Add batch dimension: 1 x D x D x 3 input_img = np.expand_dims(processed, 0) # Theta is the 85D vector holding [camera, pose, shape] # where camera is 3D [s, tx, ty] # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format # shape is 10D shape coefficients of SMPL start = datetime.datetime.now() joints, verts, cams, joints3d, theta = model.predict( input_img, get_theta=True) # verts_anim.append(verts.tolist()) cam_for_render, vert_shifted, joints_orig = vis_util.get_original( proc_param, verts, cams[0], joints[0], img_size=img.shape[:2]) rend_img_overlay = renderer( vert_shifted, cam=cam_for_render, img=img, do_alpha=True) cv2.imshow('processed', rend_img_overlay) end = datetime.datetime.now() delta = end -start print("took:" , delta) # Display Camera frame cv2.imshow('frame',frame) # cv2.imshow('processed',processed) if cv2.waitKey(1) & 0xFF == ord('q'): break # Display Plot # pos = np.random.normal(size=(100000, 3), scale=0.2) # scatter.set_data(verts[0], edge_color=None, face_color=(1, 1, 1, .5), size=5) i = i + 1 print(i) if i % 30 == 0: print("new second!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") # to_json(verts_anim, "verts_anim.txt") # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def main(): # Video capture cap = cv2.VideoCapture(0) # Make a canvas and add simple view canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) view = canvas.central_widget.add_view() # create scatter object scatter = visuals.Markers() # generate data or figure out how to prevent crash without data ^^ pos = np.random.normal(size=(100000, 3), scale=0.2) scatter.set_data(pos, edge_color=None, face_color=(1, 1, 1, .5), size=5) view.add(scatter) #configure view view.camera = 'turntable' # or try 'arcball' axis = visuals.XYZAxis(parent=view.scene) # BY ANDREW: add GPU growth tfcfg = tf.ConfigProto() tfcfg.gpu_options.allow_growth = True #load model sess = tf.Session(config=tfcfg) model = RunModel(config, sess=sess) while (True): # Capture frame-by-frame ret, frame = cap.read() processed = preprocess_image(frame) # Add batch dimension: 1 x D x D x 3 input_img = np.expand_dims(processed, 0) # Theta is the 85D vector holding [camera, pose, shape] # where camera is 3D [s, tx, ty] # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format # shape is 10D shape coefficients of SMPL start = datetime.datetime.now() joints, verts, cams, joints3d, theta = model.predict(input_img, get_theta=True) end = datetime.datetime.now() delta = end - start print("took:", delta) # Display Camera frame cv2.imshow('frame', frame) cv2.imshow('processed', processed) if cv2.waitKey(1) & 0xFF == ord('q'): break # Display Plot # pos = np.random.normal(size=(100000, 3), scale=0.2) scatter.set_data(verts[0], edge_color=None, face_color=(1, 1, 1, .5), size=5) # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def __init__(self, ui: MyUi, canvas, camera, use_pre_rendering=True): self.ui = ui self.canvas = canvas self.camera = camera self.ui.vispy_view.addWidget(canvas.native) self.use_pre_rendering = use_pre_rendering # member variables initialization self._ready = False self._slider_dt = 0.01 self._playback_mode = False self._current_t = 0.0 self._playback_timer = QTimer(self.ui) self.obj_file = '' self.param_file = '' self.trajec_file = '' self.evlog_file = '' self.evlog = {} # self.rocket_model = RocketMesh('bianca.obj') # デフォルトモデルを読み込み std_model_path = os.path.join(THIS_FILE_DIR, 'samples/std_scale.obj') self.rocket_model = RocketMesh(std_model_path) self.rocket_model.set_scale(np.array([1.0, 0.1, 0.1])) self.rocket_model.set_CG_pos(np.array([0.5, 0, 0])) self.rocket_model.move(np.array([0.0, 0.0, 0.0])) # set events self.ui.load_trajec_btn.clicked.connect(self.load_trajectory) self.ui.load_param_btn.clicked.connect(self.load_params) self.ui.load_obj_btn.clicked.connect(self.load_obj) self.ui.load_evlog_btn.clicked.connect(self.load_eventlog) self.ui.import_btn.clicked.connect(self.setup_rendering) self.ui.start_btn.clicked.connect(self.on_start_clicked) self.ui.pause_btn.clicked.connect(self.on_pause_clicked) self.ui.stop_btn.clicked.connect(self.on_stop_clicked) self.ui.t_slider.setMinimum(0) self.ui.t_slider.setMaximum(1) self.ui.t_slider.setSingleStep(1) self.ui.t_slider.valueChanged[int].connect(self.on_slider_changed) self.trajec_plot_model = None # event markers self.trajec_event_markers = visuals.Markers() self.trajec_event_texts = None view = self.canvas.central_widget.add_view() view.add(GridLines()) view.add(XYZAxis()) # view.add(self.trajec_event_markers) # view.add(self.trajec_plot_model) view.add(self.rocket_model.visual) view.bgcolor = 'gray' view.camera = self.camera view.padding = 12 self.view = view self.canvas.show() self.ui.show()
def reset(self): """ Reset. """ # last key press (it should have a mutex, but visualization is not # safety critical, so let's do things wrong) self.action = "no" # no, next, back, quit are the possibilities # new canvas prepared for visualizing data self.canvas = SceneCanvas(keys='interactive', show=True) # interface (n next, b back, q quit, very simple) self.canvas.events.key_press.connect(self.key_press) self.canvas.events.draw.connect(self.draw) # grid self.grid = self.canvas.central_widget.add_grid() # laserscan part self.scan_view = vispy.scene.widgets.ViewBox(border_color='white', parent=self.canvas.scene) self.grid.add_widget(self.scan_view, 0, 0) self.scan_vis = visuals.Markers() self.scan_view.camera = 'turntable' self.scan_view.add(self.scan_vis) visuals.XYZAxis(parent=self.scan_view.scene) # add semantics if self.semantics: print("Using semantics in visualizer") self.sem_view = vispy.scene.widgets.ViewBox( border_color='white', parent=self.canvas.scene) self.grid.add_widget(self.sem_view, 0, 1) self.sem_vis = visuals.Markers() self.sem_view.camera = 'turntable' self.sem_view.add(self.sem_vis) visuals.XYZAxis(parent=self.sem_view.scene) # self.sem_view.camera.link(self.scan_view.camera) if self.instances: print("Using instances in visualizer") self.inst_view = vispy.scene.widgets.ViewBox( border_color='white', parent=self.canvas.scene) self.grid.add_widget(self.inst_view, 0, 2) self.inst_vis = visuals.Markers() self.inst_view.camera = 'turntable' self.inst_view.add(self.inst_vis) visuals.XYZAxis(parent=self.inst_view.scene) # self.inst_view.camera.link(self.scan_view.camera) # img canvas size self.multiplier = 1 self.canvas_W = 1024 self.canvas_H = 64 if self.semantics: self.multiplier += 1 if self.instances: self.multiplier += 1 # new canvas for img self.img_canvas = SceneCanvas(keys='interactive', show=True, size=(self.canvas_W, self.canvas_H * self.multiplier)) # grid self.img_grid = self.img_canvas.central_widget.add_grid() # interface (n next, b back, q quit, very simple) self.img_canvas.events.key_press.connect(self.key_press) self.img_canvas.events.draw.connect(self.draw) # add a view for the depth self.img_view = vispy.scene.widgets.ViewBox( border_color='white', parent=self.img_canvas.scene) self.img_grid.add_widget(self.img_view, 0, 0) self.img_vis = visuals.Image(cmap='viridis') self.img_view.add(self.img_vis) # add semantics if self.semantics: self.sem_img_view = vispy.scene.widgets.ViewBox( border_color='white', parent=self.img_canvas.scene) self.img_grid.add_widget(self.sem_img_view, 1, 0) self.sem_img_vis = visuals.Image(cmap='viridis') self.sem_img_view.add(self.sem_img_vis) # add instances if self.instances: self.inst_img_view = vispy.scene.widgets.ViewBox( border_color='white', parent=self.img_canvas.scene) self.img_grid.add_widget(self.inst_img_view, 2, 0) self.inst_img_vis = visuals.Image(cmap='viridis') self.inst_img_view.add(self.inst_img_vis)
grid.add_widget(vb1, 1, 0) grid.add_widget(vb2, 1, 1) grid.add_widget(vb3, 1, 2) c_grid = vb4.add_grid() c_grid.add_widget(get_label('Classes:')) for i in range(len(rgb_codes)): vb = vispy.scene.widgets.ViewBox(bgcolor=[code / 255 for code in rgb_codes[i]], parent=vb4) vb.add_widget(get_label(class_names[i])) c_grid.add_widget(vb) grid.add_widget(vb4, 2, 0, col_span=3) real_scatter = visuals.Markers() mask_scatter = visuals.Markers() pred_scatter = visuals.Markers() real_scatter.set_data(data[:, :3], edge_color=None, face_color=real_colors, size=5) mask_scatter.set_data(data[:, :3], edge_color=None, face_color=mask_colors, size=5) pred_scatter.set_data(data[:, :3], edge_color=None, face_color=pred_colors, size=5) vb1.add(real_scatter) vb2.add(mask_scatter) vb3.add(pred_scatter) vb1.camera = 'turntable' vb2.camera = 'turntable' vb3.camera = 'turntable'
cov.append(np.cov(d)) cov = np.array(cov) # Plot them. for m, c in zip(mu, cov): Ellipse(mu=m, cov=c, std=.7, parent=view.scene, edge_color='blue') # Project the paths into gaussian spherical space. # This should yield three different paths, each bending around the origin in different ways. xb = obs.np_el_backproject_all(x=uu.reshape(-1, D), mu=mu, Ainv=np.linalg.inv( np.linalg.cholesky(cov))).reshape(-1, D) # create scatter object and fill in the data scatter3 = visuals.Markers() scatter3.set_data(xb, size=5, face_color='blue') view.add(scatter3) scatter4 = visuals.Markers() scatter4.set_data(uu.reshape(-1, D), size=5, face_color='green') view.add(scatter4) view.camera = 'turntable' # or try 'arcball' # add a colored 3D axis for orientation axis = visuals.XYZAxis(parent=view.scene) if __name__ == '__main__': import sys if sys.flags.interactive != 1:
def scatter(pos, mfc=[0.5, 0.5, 0.5, 0.8], mec=None, mfs=8, mes=1, bgc=[0.9, 0.9, 0.9], scaling=False, symbol='disc'): """ Display a scatter plot in 2D or 3D. Parameters ---------- pos : array The array of locations to display each symbol. mfc : Color | ColorArray The color used to draw each symbol interior. mec : Color | ColorArray The color used to draw each symbol outline. mfs : float or array The symbol size in px. mes : float | None The width of the symbol outline in pixels. bgc : Color The color used for the background. scaling : bool If set to True, marker scales when rezooming. symbol : str The style of symbol to draw ('disc', 'arrow', 'ring', 'clobber', 'square', 'diamond', 'vbar', 'hbar', 'cross', 'tailed_arrow', 'x', 'triangle_up', 'triangle_down', 'star'). """ # Create the Canvas, the Scene and the View canvas = scene.SceneCanvas(keys='interactive', show=True, bgcolor=bgc) view = canvas.central_widget.add_view() # Create the scatter plot scatter = visuals.Markers() scatter.set_data(pos, face_color=mfc, edge_color=mec, scaling=scaling, size=mfs, edge_width=mes, symbol=symbol) view.add(scatter) # 2D Shape if pos.shape[1] == 2: # Set the camera properties view.camera = scene.PanZoomCamera(aspect=1) view.camera.set_range() # Create lines to know the position of the cursor tr = canvas.scene.node_transform(scatter) win_xmin, win_ymax = tr.map([0, 0])[:2] win_xmax, win_ymin = tr.map(canvas.size)[:2] win_xsize, win_ysize = win_xmax - win_xmin, win_ymax - win_ymin prop = .015 tick_size = prop * win_xsize top_line = scene.visuals.Line(pos=np.array( [[win_xmin, win_ymax], [win_xmin, win_ymax - tick_size]]), color=[.2, .2, .2, 0.5], width=1, parent=view.scene, method='gl') right_line = scene.visuals.Line(pos=np.array( [[win_xmax, win_ymin], [win_xmax - tick_size, win_ymin]]), color=[.2, .2, .2, 0.5], width=1, parent=view.scene, method='gl') bottom_line = scene.visuals.Line(pos=np.array( [[win_xmax, win_ymin], [win_xmax, win_ymin + tick_size]]), color=[.2, .2, .2, 0.5], width=1, parent=view.scene, method='gl') left_line = scene.visuals.Line(pos=np.array( [[win_xmin, win_ymax], [win_xmin + tick_size, win_ymax]]), color=[.2, .2, .2, 0.5], width=1, parent=view.scene, method='gl') cross_hline = scene.visuals.Line(pos=np.array( [[win_xmax, win_ymin], [win_xmax, win_ymin + tick_size]]), color=[0, 0, 0, 1], width=2, parent=view.scene, method='gl') cross_vline = scene.visuals.Line(pos=np.array( [[win_xmin, win_ymax], [win_xmin + tick_size, win_ymax]]), color=[0, 0, 0, 1], width=2, parent=view.scene, method='gl') # TODO: create rectangle around the text # Create text to give cursor position text_xline = visuals.Text('', bold=False, font_size=12, color=[0, 0, 0, 1], pos=[50, 50], anchor_x='left', anchor_y='baseline') text_yline = visuals.Text('', bold=False, font_size=12, color=[0, 0, 0, 1], pos=[50, 50], anchor_x='left', anchor_y='baseline') view.add(text_xline) view.add(text_yline) # When the mouse move, refresh the cursor position @canvas.events.mouse_move.connect def on_mouse_move(event): # Find the cursor position in the windows coordinate tr = canvas.scene.node_transform(scatter) x, y = tr.map(event.pos)[:2] # Find the min and max for both axis in the windows coordinate win_xmin, win_ymax = tr.map([0, 0])[:2] win_xmax, win_ymin = tr.map(canvas.size)[:2] win_xsize, win_ysize = win_xmax - win_xmin, win_ymax - win_ymin tick_size = prop * win_xsize #refresh xtext, ytext = str('%.2e' % x), str('%.2e' % y) text_xline.text = xtext text_xline.pos = [x, win_ymin] text_yline.text = ytext text_yline.pos = [win_xmin, y] top_line.set_data( pos=np.array([[x, win_ymax], [x, win_ymax - tick_size]])) right_line.set_data( pos=np.array([[win_xmax, y], [win_xmax - tick_size, y]])) bottom_line.set_data( pos=np.array([[x, win_ymin], [x, win_ymin + tick_size]])) left_line.set_data( pos=np.array([[win_xmin, y], [win_xmin + tick_size, y]])) cross_hline.set_data( pos=np.array([[x - tick_size / 2, y], [x + tick_size / 2, y]])) cross_vline.set_data( pos=np.array([[x, y - tick_size / 2], [x, y + tick_size / 2]])) # 3D Shape elif pos.shape[1] == 3: view.camera = 'turntable' app.run()
canvas = vispy.scene.SceneCanvas(keys='interactive', show=True) view = canvas.central_widget.add_view() view.camera = 'turntable' # generate data def solver(t): pos = np.array([[0.5 + t / 10000, 0.5, 0], [0, 0, 0.5], [0, 0.5, 0], [0.5, 0, 0]]) return pos # These are the data that need to be updated each frame --^ scatter = visuals.Markers() view.add(scatter) #view.camera = scene.TurntableCamera(up='z') # just makes the axes axis = visuals.XYZAxis(parent=view.scene) t = 0.0 def update(ev): global scatter global t t += 1.0 scatter.set_data(solver(t),
def input_data(self, scans=None, img=None, boxes=None, index=0, save_img=False, no_gt=False): self.canvas = vispy.scene.SceneCanvas(show=True) self.grid = self.canvas.central_widget.add_grid() self.vb = self.grid.add_view(row=0, col=0, row_span=2) self.vb_img = self.grid.add_view(row=1, col=0) self.vb.camera = 'turntable' self.vb.camera.elevation = 90 #21.0 self.vb.camera.center = (6.5, -0.5, 9.0) self.vb.camera.azimuth = -90 #-75.5 self.vb.camera.scale_factor = 63 #32.7 self.vb_img.camera = 'turntable' self.vb_img.camera.elevation = -90.0 self.vb_img.camera.center = (2100, -380, -500) self.vb_img.camera.azimuth = 0.0 self.vb_img.camera.scale_factor = 1500 pos = scans[:, 0:3] scatter = visuals.Markers() scatter.set_gl_state('translucent', depth_test=False) scatter.set_data(pos, edge_width=0, face_color=(1, 1, 1, 1), size=0.01, scaling=True) self.vb.add(scatter) if img is None: img = np.zeros(shape=[1, 1, 3], dtype=np.float32) image = visuals.Image(data=img, method='auto') self.vb_img.add(image) if boxes is not None: if len(boxes.shape) == 1: boxes = boxes.reshape(1, -1) gt_indice = np.where(boxes[:, -1] == 2)[0] gt_cnt = len(gt_indice) i = 0 for box in boxes: radio = max(box[0] - 0.5, 0.005) * 2.0 color = (0, radio, 0, 1) # Green if box[-1] == 4: # gt boxes i = i + 1 vsp_box = visuals.Box(width=box[4], depth=box[5], height=box[6], color=(0.6, 0.8, 0.0, 0.3)) #edge_color='yellow') mesh_box = vsp_box.mesh.mesh_data mesh_border_box = vsp_box.border.mesh_data vertices = mesh_box.get_vertices() center = np.array([box[1], box[2], box[3]], dtype=np.float32) vtcs = np.add(vertices, center) mesh_border_box.set_vertices(vtcs) mesh_box.set_vertices(vtcs) self.vb.add(vsp_box) if False: text = visuals.Text(text='gt: ({}/{})'.format( i, gt_cnt), color='white', face='OpenSans', font_size=12, pos=[box[1], box[2], box[3]], anchor_x='left', anchor_y='top', font_manager=None) self.vb.add(text) if (box[-1] + box[-2]) == 0: # True negative cls rpn divided by cube self.vb.add(line_box(box, color=color)) if (box[-1] + box[-2] ) == 1: # False negative cls rpn divided by cube self.vb.add(line_box(box, color='red')) if (box[-1] + box[-2] ) == 2: # False positive cls rpn divided by cube if no_gt: self.vb.add(line_box(box, color='yellow')) else: self.vb.add(line_box(box, color='blue')) if (box[-1] + box[-2]) == 3: # True positive cls rpn divided by cube self.vb.add(line_box(box, color='yellow')) if save_img: if not os.path.exists(folder): os.makedirs(folder) fileName = path_add(folder, str(index).zfill(6) + '.png') res = self.canvas.render(bgcolor='black')[:, :, 0:3] vispy_file.write_png(fileName, res) @self.canvas.connect def on_key_press(ev): if ev.key.name in '+=': a = self.vb.camera.get_state() print(a)