def render_image(self, img, status_dict, task_name, wait=5, pt_color=(0, 255, 0), render_layers={ "selected": [0.5, 2], "background": [0.3, 1] }): if img.shape[2] == 3: cv2.setMouseCallback(self.window_name, self.__mouse_handler, cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)) elif img.shape[2] == 1: cv2.setMouseCallback(self.window_name, self.__mouse_handler, img) else: raise NotImplementedError # render part for _c, _rgs in self.region_cache.items(): if _c == self.class_selected: transparency = render_layers["selected"][0] thicky = np.ceil(self.adsorb_thresh).astype(int) radius = 1 + np.ceil(self.adsorb_thresh).astype(int) img = polygon_marker(img, self.point_cache, pt_color, thicky, transparency, True, radius, False) else: transparency = render_layers["background"][0] thicky = render_layers["background"][1] radius = render_layers["background"][1] for _j, _rg in enumerate(_rgs): img = polygon_marker(img, _rg, self.render_dict[_c], thicky, transparency, True, radius, True) text = "{}:{}\npt:{} rg:{}".format( task_name, self.class_dict[self.class_selected], len(self.point_cache), len(self.region_cache[self.class_selected])) text_marker(img, text, (0, 0), (0, 0), FONT, FONTSCALE, THICKNESS, self.render_dict[self.class_selected], background=(255, 255, 255), bgthick=0.3) img = self.__patch_legend(img) cv2.imshow(self.window_name, img) pressed_key = cv2.waitKey(wait) & 0xFF for k, v in status_dict.items(): if pressed_key == ord(k): flag, rst = self.__status_handler(v) if flag: print("recorded: {}".format(rst)) return flag, rst for k, v in self.class_dict.items(): if pressed_key == ord(str(k)): self.__class_handler(k) return False, None return False, None
def __legend_panel(self, cubic_shink=1, background=(255, 255, 255), text_color=(0, 0, 0), spacing=10, status_dict=None): width = 0 height = 0 for k in self.class_list: (label_width, label_height), baseline = cv2.getTextSize( " " + str(k) + " " + self.class_dict[k], FONT, FONTSCALE, THICKNESS) width = max(width, label_width) height = max(height, label_height + baseline) if status_dict is None: status_dict = {} operation_instruction = "" for k, v in status_dict.items(): opt_text = str(k) + ": " + str(v) operation_instruction += (opt_text + "\n") (label_width, label_height), baseline = cv2.getTextSize(opt_text, FONT, FONTSCALE, THICKNESS) width = max(width, label_width) height = max(height, label_height + baseline) cubic_size = height - 2 * cubic_shink cubic_shink = cubic_shink if cubic_size > 0 else 0 cubic_size = cubic_size if cubic_size > 0 else height width = 2 * spacing + width + cubic_size total_height = height * (len(self.class_list) + len(status_dict) ) + spacing * (len(self.class_list) + 2) channels = len(background) panel = (np.ones([total_height, width, channels]) * np.array(background)).astype(np.uint8) for _i, k in enumerate(self.class_list): row_start = _i * height + (_i + 1) * spacing panel = rectangle_marker( panel, [[cubic_shink + spacing, row_start], [cubic_shink + spacing + cubic_size, row_start + cubic_size]], self.render_dict[k], 1, 0.8, True, 1) text_marker(panel, " " + str(k) + " " + self.class_dict[k], (spacing + height, row_start), (0, 0), FONT, FONTSCALE, THICKNESS, text_color) row_start = len( self.class_list) * height + (len(self.class_list) + 1) * spacing panel = rectangle_marker( panel, [[spacing, row_start], [ width - spacing, row_start + (label_height + baseline) * len(status_dict) ]], (64, 64, 64), 1, 0.8, True, 1) text_marker(panel, operation_instruction, (spacing, row_start), (0, 0), FONT, FONTSCALE, THICKNESS, (255, 255, 255)) self.legend = panel
def renderImage(self, img, wait=30, pt_color=(0, 255, 0)): if img.shape[2] == 3: cv2.setMouseCallback(self.window_name, self.handlePoint, cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)) else: cv2.setMouseCallback(self.window_name, self.handlePoint, img) for _i, _pt in enumerate(self.point_cache): cv2.circle(img, _pt, np.ceil(self.adsorb_thresh).astype(int), pt_color, -1) text_marker(img, str(len(self.point_cache)), (0, 0), (0, 0), cv2.FONT_HERSHEY_TRIPLEX, 2, 3, (0, 255, 255)) cv2.imshow(self.window_name, img) cv2.waitKey(wait)
def __patch_legend(self, img, background=(255, 255, 255)): height = img.shape[0] legend_height = self.legend.shape[0] if height < legend_height: patch_bottom = ( np.ones([legend_height - height, img.shape[1], img.shape[2]]) * np.array(background)).astype(np.uint8) if self.prediction_status: text_marker(patch_bottom, "INTERPOLATION", (0, 0), (0, 0), FONT, FONTSCALE, THICKNESS, (0, 0, 255)) else: text_marker(patch_bottom, "LABELING", (0, 0), (0, 0), FONT, FONTSCALE, THICKNESS, (0, 255, 0)) img = np.concatenate((img, patch_bottom), axis=0) else: patch_bottom = (np.ones([ height - legend_height, self.legend.shape[1], self.legend.shape[2] ]) * np.array(background)).astype(np.uint8) if self.prediction_status: text_marker(patch_bottom, "INTERPOLATION", (0, 0), (0, 0), FONT, FONTSCALE, THICKNESS, (0, 0, 255)) else: text_marker(patch_bottom, "LABELING", (0, 0), (0, 0), FONT, FONTSCALE, THICKNESS, (0, 255, 0)) legend = np.concatenate((self.legend, patch_bottom), axis=0) img = np.concatenate((img, legend), axis=1) return img
def render_image(self, img, status_dict, wait=5, pt_color=(0, 255, 0), plg_color=(255, 0, 0), text_color=(0, 255, 255)): if img.shape[2] == 3: cv2.setMouseCallback(self.window_name, self.__mouse_handler, cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)) elif img.shape[2] == 1: cv2.setMouseCallback(self.window_name, self.__mouse_handler, img) else: raise NotImplementedError # render part for _j, _rg in enumerate(self.region_cache): img = polygon_marker(img, _rg, plg_color, np.ceil(self.adsorb_thresh).astype(int), 0.5, True, 1 + np.ceil(self.adsorb_thresh).astype(int), True) img = polygon_marker(img, self.point_cache, pt_color, np.ceil(self.adsorb_thresh).astype(int), 0.5, True, 1 + np.ceil(self.adsorb_thresh).astype(int), False) text = "pt:{} rg:{}".format(len(self.point_cache), len(self.region_cache)) text_marker(img, text, (0, 0), (0, 0), cv2.FONT_HERSHEY_COMPLEX, 1.0, 2, text_color, background=(255, 255, 255), bgthick=0.3) cv2.imshow(self.window_name, img) pressed_key = cv2.waitKey(wait) & 0xFF for k, v in status_dict.items(): if pressed_key == ord(k): flag, rst = self.__status_handler(v) if flag: print("recorded: {}".format(rst)) return flag, rst return False, None