def main(): global pause_btn, next_btn # Create curves curves.append(Curve(LED_NUM, RED, milliseconds)) curves.append(Curve(LED_NUM, GREEN, milliseconds)) curves.append(Curve(LED_NUM, BLUE, milliseconds)) curves.append(Curve(LED_NUM, GREEN, milliseconds)) # Setup plots fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.2) setup_chart(ax) # X values xs = list(range(LED_NUM)) # Add buttons axpause = plt.axes([0.7, 0.05, 0.1, 0.075]) axnext = plt.axes([0.81, 0.05, 0.1, 0.075]) pause_btn = Button(axpause, 'Play') pause_btn.on_clicked(toggle_pause) next_btn = Button(axnext, 'Next') next_btn.set_active(True) next_btn.on_clicked(next_step) # Start animation ani = animation.FuncAnimation(fig, loop, fargs=(ax, xs), interval=SPEED) plt.show()
def _set_buttons(self, enable_reset, enable_save, enable_io): reset_button = Button(plt.axes([0.51, 0.01, 0.1, 0.035]), 'Reset') # [0.88, 0.01, 0.1, 0.035] reset_button.label.set_color('r') reset_button.label.set_fontweight('bold') reset_button.on_clicked(self._reset) if enable_reset: reset_button.set_active(True) else: reset_button.set_active(False) reset_button.ax.set_visible(False) textbox = TextBox( plt.axes([0.2, 0.06, 0.6, 0.095]), 'I/O', initial='' ) # [0.12, 0.01, 0.65, 0.06] [0.05,0.92,0.8,0.07] [0.025, 0.885, 0.75, 0.05] textbox.label.set_fontweight('bold') textbox.on_submit(self._evaluate_message) if enable_io: textbox.set_active(True) else: textbox.set_active(False) textbox.ax.set_visible(False) save_button = Button(plt.axes([0.39, 0.01, 0.1, 0.035]), 'Save') # [0.77, 0.01, 0.1, 0.035] save_button.label.set_fontweight('bold') save_button.on_clicked(self._quicksave) if enable_save: save_button.set_active(True) else: save_button.set_active(False) save_button.ax.set_visible(False) self._widgetlist[self.fignum.index(plt.gcf().number)] = (reset_button, textbox, save_button) self._textbox = textbox
class BatchVisualization(object): def __init__(self, images, true_masks=None, pred_masks=None, true_labels=None, pred_labels=None, legends=None, **fig_kwargs): self.images = images self.n_images = self.images.shape[0] self.true_masks = true_masks self.pred_masks = pred_masks self.true_labels = true_labels self.pred_labels = pred_labels self.legends = legends if true_labels is not None: self.ncols = fig_kwargs.get('ncols', 3) self.nrows = fig_kwargs.get('nrows', 3) else: # Mask predictions self.ncols = fig_kwargs.get('ncols', 1) self.nrows = fig_kwargs.get('nrows', 3) self.batch_size = self.ncols * self.nrows self.start_idx = 0 self.num_plots_per_image = 1 if self.true_masks is not None: self.num_plots_per_image += 1 if self.pred_masks is not None: self.num_plots_per_image += 1 if self.true_masks is not None and len(self.true_masks.shape) == 3: self.true_masks = np.expand_dims(self.true_masks, axis=3) if self.pred_masks is not None and len(self.pred_masks.shape) == 3: self.pred_masks = np.expand_dims(self.pred_masks, axis=3) self.mask_type = fig_kwargs.get('mask_type', 'contour') def __call__(self, *args, **kwargs): self.fig, self.ax = plt.subplots(figsize=(10, 8), nrows=self.nrows, ncols=self.num_plots_per_image * self.ncols, sharex='all', sharey='all', gridspec_kw={'hspace': 0., 'wspace': 0. } ) self.ax = np.ravel(self.ax) for ax in self.ax: ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') ax.axis('off') self.fig.subplots_adjust(bottom=0.2, hspace=0, wspace=0) self.axprev = plt.axes([0.7, 0.05, 0.1, 0.075]) self.axnext = plt.axes([0.81, 0.05, 0.1, 0.075]) self.bnext = Button(self.axnext, 'Next') self.bnext.on_clicked(self.next) self.bprev = Button(self.axprev, 'Previous') self.bprev.on_clicked(self.prev) self.start_idx = 0 self.update_batch() self.update_buttons() plt.show(block=True) def next(self, event): self.start_idx += self.batch_size self.update_batch() self.update_buttons() plt.show(block=True) def prev(self, event): self.start_idx -= self.batch_size self.update_batch() self.update_buttons() plt.show(block=True) def update_buttons(self): if self.start_idx + self.batch_size < self.n_images: self.axnext.set_visible(True) self.bnext.set_active(True) else: self.axnext.set_visible(False) self.bnext.set_active(False) if self.start_idx - self.batch_size >= 0: self.bprev.set_active(True) self.axprev.set_visible(True) else: self.bprev.set_active(False) self.axprev.set_visible(False) def update_batch(self): for ax_idx, image_idx in enumerate(range(self.start_idx, min(self.start_idx + self.batch_size, self.n_images))): img_ax_idx = ax_idx * self.num_plots_per_image self.ax[img_ax_idx].clear() self.ax[img_ax_idx].imshow(self.images[image_idx]) if self.true_masks is not None: img_ax_idx += 1 plot_mask(axis_in=self.ax[img_ax_idx], img_in=self.images[image_idx], mask_in=self.true_masks[image_idx], title_in='GT mask') if self.true_labels is not None or self.pred_labels is not None: true_label = None if self.true_labels is None else class_names[np.argmax(self.true_labels[image_idx])] pred_label = None if self.pred_labels is None else class_names[np.argmax(self.pred_labels[image_idx])] if true_label is not None and pred_label is not None: label = '%s -> %s' % (true_label, pred_label) font_color = 'darkgreen' if true_label == pred_label else 'darkred' else: label = '%s' % true_label if pred_label is None else pred_label font_color = 'k' # these are matplotlib.patch.Patch properties props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) font_weight = 'bold' fontdict = {'family': 'serif', 'color': font_color, 'weight': font_weight, 'size': 14, } # place a text box in upper left in axes coords ax = self.ax[img_ax_idx] ax.text(0.35, 0.95, label, transform=ax.transAxes, fontdict=fontdict, verticalalignment='top', bbox=props) if self.pred_masks is not None: img_ax_idx += 1 plot_mask(axis_in=self.ax[img_ax_idx], img_in=self.images[image_idx], mask_in=self.pred_masks[image_idx], title_in='Pred mask') if self.legends: plt.figlegend() for ax in self.ax: ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') ax.axis('off') plt.draw()
except: pass ax.figure.canvas.draw() del df1 callback = Index() axpause = plt.axes([0.92, 0.01, 0.075, 0.075]) bpause = Button(axpause, 'stop') bpause.on_clicked(callback.pause) axfull = plt.axes([0.92, 0.1, 0.075, 0.075]) bfull = Button(axfull, 'full') bfull.on_clicked(callback.full) bfull.set_active(False) y_count = 0 def get_iw(address, lower, upper): global timer global callback global y_count y_count += 1 if y_count > 80: y_count = 0 tmp = (5000,) elif y_count > 40:
class guiMenu: fig = None gauss_params = [33, 7] logTextLabels = [] logText = [] axMisc = None axAlgorithm = None axSaveOptions = None axGauss = None axLog = None axRadio = None axLogLabels = None line_gaussian = None ax_window_size = None slider_window_size = None ax_sigma = None slider_sigma = None originalStdOut = None #------------------------------------------------------------------------------ # Class initialization # def __init__(self): self.strTitle = 'Gesture Analysis Configuration - ' + settings.appVersion self.fig = plt.figure() self.fig.canvas.set_window_title(self.strTitle) self.fig.set_size_inches((settings.screen_cx * 0.49) / self.fig.dpi, (settings.screen_cy * 0.465) / self.fig.dpi) self.fig.canvas.mpl_connect('resize_event', self.onresize) self.fig.canvas.mpl_connect('close_event', self.onclose) self.fig.edgecolor = 'blue' self.fig.set_facecolor('0.90') left = 0.03 # the left side of the subplots of the figure right = 0.97 # the right side of the subplots of the figure bottom = 0.04 # the bottom of the subplots of the figure top = 0.92 # the top of the subplots of the figure wspace = 0.3 # the amount of width reserved for blank space between subplots hspace = 0.7 # the amount of height reserved for white space between subplots plt.subplots_adjust(left=left, top=top, right=right, bottom=bottom, wspace=wspace, hspace=hspace) self.axMisc = plt.subplot2grid((6, 4), (0, 0), rowspan=1, colspan=1, aspect='auto', anchor='NW') self.axAlgorithm = plt.subplot2grid((6, 4), (1, 0), rowspan=2, colspan=1, aspect='auto', anchor='NW') self.axSaveOptions = plt.subplot2grid((6, 4), (3, 0), rowspan=3, colspan=1, aspect='equal', anchor='NW') self.axGauss = plt.subplot2grid((6, 4), (0, 1), rowspan=4, colspan=3) self.axLog = plt.subplot2grid((6, 4), (5, 1), rowspan=1, colspan=3, aspect='auto', anchor='NW') # create the various groups of UI controls self.createUIMiscellaneous() self.createUILog() self.createUIAlgorithm() self.createUIGaussianFilterControls() self.createUIMiscellaneousControls() # set settings.tkGuiCanvas for use for modal dialogs try: if (self.fig is not None) and (self.fig.canvas is not None) and ( self.fig.canvas._tkcanvas is not None): settings.tkGuiCanvas = self.fig.canvas._tkcanvas except Exception as e: pass #------------------------------------------------------------------------------ # def get_aspect(self, ax): # Total figure size figW, figH = ax.get_figure().get_size_inches() # Axis size on figure _, _, w, h = ax.get_position().bounds # Ratio of display units disp_ratio = (figH * h) / (figW * w) # Ratio of data units # Negative over negative because of the order of subtraction data_ratio = sub(*ax.get_ylim()) / sub(*ax.get_xlim()) return disp_ratio / data_ratio #------------------------------------------------------------------------------ # def createUIAlgorithm(self): algorithm = settings.application.algorithm.lower() defaultAlgoritmIdx = 0 if (algorithm == 'total'): defaultAlgoritmIdx = 0 elif (algorithm == 'parallel'): defaultAlgoritmIdx = 1 elif (algorithm == 'naive'): defaultAlgoritmIdx = 2 self.axAlgorithm.set_title('Algorithm', x=0, horizontalalignment='left') # create an axis to host to radio buttons. make its aspect ratio # equal so the radiobuttons stay round aspect = self.get_aspect(self.axAlgorithm) rect = [0, 0, 1.0 * aspect, 1.0] ip = InsetPosition(self.axAlgorithm, rect) self.axRadio = plt.axes(rect) self.axRadio.set_axes_locator(ip) self.axRadio.axis('off') self.radioAlgorithm = RadioButtons( self.axRadio, ('Total Energy', 'Parallel Energy', 'Naive (no filter)'), active=defaultAlgoritmIdx) self.radioAlgorithm.on_clicked(self.onClickAlgorithm) #------------------------------------------------------------------------------ # def createUIGaussianFilterControls(self): axcolor = 'lightgoldenrodyellow' rect = [0.82, -0.158, 0.14, 0.07] ax_btnapply = plt.axes(rect) ip = InsetPosition(self.axGauss, rect) #posx, posy, width, height ax_btnapply.set_axes_locator(ip) self.btnApply = Button(ax_btnapply, 'Apply') self.btnApply.on_clicked(self.onclickApply) rect = [0.82, -0.245, 0.14, 0.07] ax_btnreset = plt.axes(rect) ip = InsetPosition(self.axGauss, rect) #posx, posy, width, height ax_btnreset.set_axes_locator(ip) self.btnReset = Button(ax_btnreset, 'Reset', color='0.950', hovercolor='0.975') self.btnReset.on_clicked(self.onclickReset) rect = [0.1, -0.155, 0.55, 0.04] self.ax_window_size = plt.axes(rect, facecolor=axcolor) ip = InsetPosition(self.axGauss, rect) #posx, posy, width, height self.ax_window_size.set_axes_locator(ip) self.slider_window_size = Slider(self.ax_window_size, 'Window Size', 1, (self.gauss_params[0] + 1) * 2 + 1, valinit=self.gauss_params[0], valstep=2) self.slider_window_size.on_changed(self.updateGaussianFilter) rect = [0.1, -0.235, 0.55, 0.04] self.ax_sigma = plt.axes(rect, facecolor=axcolor) ip = InsetPosition(self.axGauss, rect) #posx, posy, width, height self.ax_sigma.set_axes_locator(ip) self.slider_sigma = Slider(self.ax_sigma, 'Sigma', 1, (self.gauss_params[1] + 1) * 2, valinit=self.gauss_params[1], valstep=1) self.slider_sigma.on_changed(self.updateGaussianFilter) self.updateGaussianFilter() #------------------------------------------------------------------------------ # def createUIMiscellaneous(self): self.axMisc.set_title('', x=0, horizontalalignment='left') # removing top and right borders self.axMisc.xaxis.set_visible(False) self.axMisc.yaxis.set_visible(False) # remove ticks self.axSaveOptions.set_xticks([]) self.axSaveOptions.set_yticks([]) # remove ticks self.axAlgorithm.set_xticks([]) self.axAlgorithm.set_yticks([]) bbox = self.axMisc.get_window_extent() self.axMisc.set_xlim(0, bbox.width) self.axMisc.set_ylim(0, bbox.height) #------------------------------------------------------------------------------ # def createUILog(self): self.axLog.set_title('Console Log', x=0, horizontalalignment='left') # removing top and right borders self.axLog.xaxis.set_visible(False) self.axLog.yaxis.set_visible(False) self.resetLogLabels() # redirect console messages to gui's log self.originalStdOut = sys.stdout sys.stdout = CaptureOutput() # ----------------------------------------------------------------------------- # def resetLogLabels(self): cnt = len(self.logTextLabels) for i in range(0, cnt): self.logTextLabels[i].remove() self.logTextLabels = [] bbox = self.axLog.get_window_extent() self.axLog.set_xlim(0, bbox.width) self.axLog.set_ylim(0, bbox.height) aspect = self.get_aspect(self.axLog) rect = [0, 0, 1.0 * aspect, 1.0] ip = InsetPosition(self.axLog, rect) if (self.axLogLabels is None): self.axLogLabels = plt.axes(rect) else: self.axLogLabels.set_position(rect) self.axLogLabels.set_axes_locator(ip) self.axLogLabels.axis('off') aspectLog = 1.0 / self.get_aspect(self.axLog) strText = 'Tyg' tmp, self.logTextHeight = settings.getTextExtent(self.axLog, strText) self.logTextHeight = self.logTextHeight * aspectLog # * self.fig.dpi # pre-create empty log label placeholders self.logTextLabels = [] y = (self.logTextHeight / 4.0) cy = bbox.height idx = len(self.logText) - 1 while (y < cy): str = self.logText[idx] if (idx >= 0) else '' idx = idx - 1 lbl = self.axLogLabels.text( 8.0, y, str, horizontalalignment='left', verticalalignment='bottom', color='dimgray', clip_on=True, transform=self.axLog.transData ) #, bbox={'facecolor':'lightgray', 'alpha':0.7, 'pad':0.0}) self.logTextLabels.append(lbl) y += self.logTextHeight # ----------------------------------------------------------------------------- # def createUIMiscellaneousControls(self): rect = [0.06, 0.30, 0.70, 0.40] ip = InsetPosition(self.axMisc, rect) #posx, posy, width, height ax_btnbrowse = plt.axes(rect) ax_btnbrowse.set_axes_locator(ip) self.btnBrowse = Button(ax_btnbrowse, 'Input File') self.btnBrowse.on_clicked(self.onclickBrowse) self.axSaveOptions.set_title('Output Files', x=0, horizontalalignment='left') x = 0.06 dx = 0.70 # 0.80 dy = 0.10 # 0.17 cy = 0.14 # 0.24 y = 0.80 rect = [x, y, dx, dy] ip = InsetPosition(self.axSaveOptions, rect) #posx, posy, width, height ax_btn = plt.axes(rect) ax_btn.set_axes_locator(ip) self.btnSaveJSON = Button(ax_btn, 'JSON') self.btnSaveJSON.on_clicked(self.onclickSaveJSON) rect = [x, y - 1 * cy, dx, dy] ip = InsetPosition(self.axSaveOptions, rect) #posx, posy, width, height ax_btn = plt.axes(rect) ax_btn.set_axes_locator(ip) self.btnSaveTXT = Button(ax_btn, 'Text') self.btnSaveTXT.on_clicked(self.onclickSaveTXT) rect = [x, y - 2 * cy, dx, dy] ip = InsetPosition(self.axSaveOptions, rect) #posx, posy, width, height ax_btn = plt.axes(rect) ax_btn.set_axes_locator(ip) self.btnSaveFilterView = Button(ax_btn, 'Filter Graph') self.btnSaveFilterView.on_clicked(self.onclickSaveFilterView) rect = [x, y - 3 * cy, dx, dy] ip = InsetPosition(self.axSaveOptions, rect) #posx, posy, width, height ax_btn = plt.axes(rect) ax_btn.set_axes_locator(ip) self.btnSaveSkeletonView = Button(ax_btn, '3D Joint Data') self.btnSaveSkeletonView.on_clicked(self.onclickSaveSkeletonView) rect = [x, y - 4 * cy, dx, dy] ip = InsetPosition(self.axSaveOptions, rect) #posx, posy, width, height ax_btn = plt.axes(rect) ax_btn.set_axes_locator(ip) self.btnSaveScoreView = Button(ax_btn, 'Score View') self.btnSaveScoreView.on_clicked(self.onclickSaveScoreView) rect = [x, y - 5 * cy, dx, dy] ip = InsetPosition(self.axSaveOptions, rect) #posx, posy, width, height ax_btn = plt.axes(rect) ax_btn.set_axes_locator(ip) self.btnSavePNG = Button(ax_btn, 'Full Score') self.btnSavePNG.on_clicked(self.onclickSaveImage) #------------------------------------------------------------------------------ # canvas resize event # def onresize(self, event): # plt.tight_layout() # keep tha radio buttons round... if (self.axRadio is not None) and (self.axAlgorithm is not None): aspect = self.get_aspect(self.axAlgorithm) rect = [0, 0, 1.0 * aspect, 1.0] ip = InsetPosition(self.axAlgorithm, rect) self.axRadio.set_axes_locator(ip) self.axRadio.set_position(rect) self.resetLogLabels() # ----------------------------------------------------------------------------- # canvas close event # def onclose(self, event): self.fig = None # if user closes this figure, let the main application know and to exit settings.application.close() # ----------------------------------------------------------------------------- # def updateUIControls(self, algorithm): algorithm = algorithm.lower() fEnable = False if (algorithm == 'naive') else True alpha = 0.2 if (algorithm == 'naive') else 1.0 self.btnApply.set_active(fEnable) self.btnReset.set_active(fEnable) self.btnApply.label.set_alpha(alpha) self.btnReset.label.set_alpha(alpha) fUpdateGaussianPlot = False if (self.gauss_params[0] != self.slider_window_size.val): self.ax_window_size.clear() self.slider_window_size.__init__( self.ax_window_size, 'Window Size', valmin=1, valmax=(self.gauss_params[0] + 1) * 2 + 1, valinit=self.gauss_params[0], valstep=2) self.slider_window_size.on_changed(self.updateGaussianFilter) fUpdateGaussianPlot = True if (self.gauss_params[1] != self.slider_sigma.val): self.ax_sigma.clear() self.slider_sigma.__init__(self.ax_sigma, 'Sigma', valmin=1, valmax=(self.gauss_params[1] + 1) * 2, valinit=self.gauss_params[1], valstep=1) self.slider_sigma.on_changed(self.updateGaussianFilter) fUpdateGaussianPlot = True if (fUpdateGaussianPlot): self.updateGaussianFilter() self.line_gaussian.set_alpha(alpha) self.ax_window_size.patch.set_alpha(alpha) if (self.slider_window_size.poly): self.slider_window_size.poly.set_alpha(alpha) self.slider_window_size.set_active(fEnable) for r in self.slider_window_size.ax.texts: r.set_alpha(alpha) self.ax_sigma.patch.set_alpha(alpha) if (self.slider_sigma.poly): self.slider_sigma.poly.set_alpha(alpha) self.slider_sigma.set_active(fEnable) for r in self.slider_sigma.ax.texts: r.set_alpha(alpha) self.fig.canvas.draw_idle() # ----------------------------------------------------------------------------- # def updateInputName(self): self.fig.canvas.set_window_title( self.strTitle + ' - [' + settings.application.strBeautifiedInputFile + ']') # ----------------------------------------------------------------------------- # def getAlgorithmSelection(self): if (self.radioAlgorithm.value_selected == 'Total Energy'): return 'Total' elif (self.radioAlgorithm.value_selected == 'Parallel Energy'): return 'Parallel' return 'Naive' # ----------------------------------------------------------------------------- # def onClickAlgorithm(self, label): algorithm = self.getAlgorithmSelection() if (settings.application.labanotation is not None): self.gauss_params = settings.application.labanotation.getGaussianParameters( algorithm) self.updateUIControls(algorithm) settings.application.applyAlgoritm(algorithm) #------------------------------------------------------------------------------ # updateGaussianFilter() has an unused parameter, though needs it because the # sliders use this function as their update callback... def updateGaussianFilter(self, val=0): # remove current gaussian lines if (self.line_gaussian is not None): self.line_gaussian.remove() del self.line_gaussian self.line_gaussian = None gauss_params = (int(self.slider_window_size.val), int(self.slider_sigma.val)) self._t = np.arange(-gauss_params[0] / 2.0 + 0.5, gauss_params[0] / 2.0 + 0.5, 1.0) s = wf.gaussFilter(gauss_params[0], gauss_params[1]) self.line_gaussian, = self.axGauss.plot(self._t, s, marker="o", linestyle='-', color='red', lw=1) # for i, txt in enumerate(s): # self.axGauss.annotate("{:0.2f}".format(txt), (self._t[i], s[i])) wnd = int(gauss_params[0] / 2) + 1 self.axGauss.set_xlim(-wnd, wnd) self.axGauss.set_ylim(0, 0.42) # np.max(s)) self.fig.canvas.draw_idle() #------------------------------------------------------------------------------ # def onclickApply(self, event): algorithm = self.getAlgorithmSelection() self.gauss_params = (int(self.slider_window_size.val), int(self.slider_sigma.val)) if (settings.application.labanotation is not None): settings.application.labanotation.setGaussianParameters( algorithm, self.gauss_params) settings.application.applyAlgoritm(algorithm) #------------------------------------------------------------------------------ # def onclickSaveJSON(self, event): settings.application.saveJSON() #------------------------------------------------------------------------------ # def onclickSaveTXT(self, event): settings.application.saveTXT() #------------------------------------------------------------------------------ # def onclickSaveImage(self, event): settings.application.saveImage() #------------------------------------------------------------------------------ # def onclickSaveFilterView(self, event): settings.application.saveFilterView() #------------------------------------------------------------------------------ # def onclickSaveSkeletonView(self, event): settings.application.saveSkeletonView() #------------------------------------------------------------------------------ # def onclickSaveScoreView(self, event): settings.application.saveScoreView() #------------------------------------------------------------------------------ # def onclickReset(self, event): self.slider_window_size.reset() self.slider_sigma.reset() #------------------------------------------------------------------------------ # def onclickBrowse(self, event): file = self.selectInputFile() if (file is None): return settings.application.openAndProcessInputfile(file) #------------------------------------------------------------------------------ # def selectInputFile(self): fTyp = [("Kinect Joint Data File", "*.csv")] splitInput = os.path.split( os.path.abspath(settings.application.inputFilePath)) options = {} options['filetypes'] = fTyp options['initialdir'] = splitInput[0].replace('/', os.sep) if (settings.tkGuiCanvas is not None): options['parent'] = settings.tkGuiCanvas file = tkFileDialog.askopenfilename(**options) if not file: return None return file #------------------------------------------------------------------------------ # def logMessage(self, str, ioRedirect=False): # also write message to console if (self.originalStdOut is not None): extra = "\r\n" if (ioRedirect is False) else "" self.originalStdOut.write(str + extra) self.logText.append(str) cnt = len(self.logTextLabels) if (cnt > 0): for i in range(cnt - 1, 0, -1): self.logTextLabels[i].set_text( self.logTextLabels[i - 1].get_text()) self.logTextLabels[0].set_text(str) self.fig.canvas.draw_idle()