def get_rescaled_chars(components, args=None, separate_lines=False): ''' ... Args: components: args: separate_lines: Returns: ''' if args is None: args = Arguments() char_res = args.input_shape angle, labels = correction_angle(components, args, False) lines = get_rotation_corrected_blobs(components, angle, labels, args) rescaled_lines = [] for line in lines: n_chars = len(line) chars = np.empty((n_chars, char_res, char_res), dtype=np.float32) for i, char in enumerate(line): chars[i] = processing.rescale(char, args) rescaled_lines.append(chars) if separate_lines: return rescaled_lines else: return np.concatenate(rescaled_lines, axis=0)
def extract_lines(self, args=None, use_line_heights=True): ''' extracts images for lines of text found in the image. Args: args: Arguments instance use_line_heights: Returns: a list of image of lines ''' assert self.lines is not None if args is None: return [[self.chars[i].extract() for i in line] for line in self.lines] if use_line_heights: line_heights = [ max([self.chars[i].w for i in line]) for line in self.lines ] else: line_heights = [0] * len(self.lines) return [[ processing.rescale( self.chars[i].extract(min_height=line_heights[line_id]), args) for i in line ] for line_id, line in enumerate(self.lines)]
def _generate_plot(self): if self.plot_type not in self.cached_data or self.replot: x, y, z = self.plots[self.plot_type].prepare_data(self.datasets) z = rescale(z, method=self.scale) self.cached_data[self.plot_type] = x, y, z self.replot = False x, y, z = self.cached_data[self.plot_type] return self.plots[self.plot_type].plot(x, y, z, scale=self.scale)
def plot_datasets(self, datasets, scale='linear', reset_view=True): if self.plots: self.plot.delplot(*self.plot.plots.keys()) self.plots = {} active = filter(lambda d: d.metadata['ui'].active, datasets) hilite = filter(lambda d: d.metadata['ui'].markers, active) if len(active)==0: return None for dataset in active: ui = dataset.metadata['ui'] data = dataset.data name = ui.name or dataset.name x, y = np.transpose(data[:, [0,1]]) self.plot_data.set_data(name + '_x', x) self.plot_data.set_data(name + '_y', rescale(y, method=scale)) color = ui.color if color is None: color = 'auto' plot = self.plot.plot((name + '_x', name + '_y'), name=name, type='line', color=color, line_width=ui.line_width) if color == 'auto': ui.color = tuple( (np.array(plot[0].color_) * 255).astype('uint8').tolist()) self.plots[name] = plot for dataset in hilite: ui = dataset.metadata['ui'] data = dataset.data name = ui.name or dataset.name # Overlay a scatter plot on the original line plot to highlight # data points as circles. plot = self.plot.plot((name + '_x', name + '_y'), name=name + '_selection', type='scatter', color=ui.color, outline_color=ui.color, marker_size=ui.marker_size, line_width=ui.line_width) self.plots[name] = plot if len(datasets) > 0: self.plot0renderer = plot[0] self.range_selection_tool = RangeSelection(self.plot0renderer, left_button_selects=True) self.range_selection_overlay = RangeSelectionOverlay(component=self.plot0renderer) if reset_view: self.reset_view() # Since highlighted datasets are plotted twice, both plots show up in # the legend. This fixes that. self.plot.legend.plots = self.plots self.show_legend('Overlay') self._set_scale(scale)
def extract_blobs(self): ''' function to perform blob extraction on raw images. Data is only used if blob extraction finds the right number of characters in an image. ''' for truth, img in zip(self.truth, self.raws): chars = find_blobs(img) if len(chars) == len(truth): for j in range(len(chars)): char = processing.rescale(chars[j], self.size) self.blobs.append(char) self.blob_truth.append(truth[j]) return
def extract(self, args=None, use_line_heights=True): '''returns a list of rescaled images of the extracted components in the image. Args: args: Arguments instance use_line_heights (bool): whether to use line height as height. if flase the images are rescaled to args.input_shape Returns: a list of reshaped images of all components ''' if args is None: return [c.extract() for c in self.chars] else: if not use_line_heights: return [ processing.rescale(c.extract(), args) for c in self.chars ] else: return [ c for line in self.extract_lines(args, use_line_heights) for c in line ]