def visualization(image, polygons, char_polygons, words, resize_ratio, colors): draw = ImageDraw.Draw(image, 'RGBA') for polygon in polygons: draw.polygon(polygon, fill=None, outline=(0, 255, 0, 255)) for i, char_polygon in enumerate(char_polygons): for j, polygon in enumerate(char_polygon): polygon = [int(x * resize_ratio) for x in polygon] char = words[i][j] color = colors[char2num(char)] draw.polygon(polygon, fill=color, outline=color)
def convert(self, mode): width, height = self.size if mode == "char_mask": if not self.use_char_ann: char_map = -np.ones((height, width)) char_map_weight = np.zeros((self.char_num_classes,)) else: char_map = np.zeros((height, width)) char_map_weight = np.ones((self.char_num_classes,)) for i, p in enumerate(self.char_boxes): poly = p.numpy().reshape(4, 2) poly = shrink_poly(poly, 0.25) cv2.fillPoly( char_map, [poly.astype(np.int32)], int(self.char_classes[i]) ) pos_index = np.where(char_map > 0) pos_num = pos_index[0].size if pos_num > 0: pos_weight = 1.0 * (height * width - pos_num) / pos_num char_map_weight[1:] = pos_weight return torch.from_numpy(char_map), torch.from_numpy(char_map_weight) elif mode == "seq_char_mask": decoder_target = self.char_num_classes * np.ones((32,)) word_target = -np.ones((32,)) if not self.use_char_ann: char_map = -np.ones((height, width)) char_map_weight = np.zeros((self.char_num_classes,)) for i, char in enumerate(self.word): if i > 31: break decoder_target[i] = char2num(char) word_target[i] = char2num(char) end_point = min(max(1, len(self.word)), 31) word_target[end_point] = self.char_num_classes else: char_map = np.zeros((height, width)) char_map_weight = np.ones((self.char_num_classes,)) word_length = 0 for i, p in enumerate(self.char_boxes): poly = p.numpy().reshape(4, 2) if i < 32: decoder_target[i] = int(self.char_classes[i]) word_target[i] = int(self.char_classes[i]) word_length += 1 poly = shrink_poly(poly, 0.25) cv2.fillPoly( char_map, [poly.astype(np.int32)], int(self.char_classes[i]) ) end_point = min(max(1, word_length), 31) word_target[end_point] = self.char_num_classes pos_index = np.where(char_map > 0) pos_num = pos_index[0].size if pos_num > 0: pos_weight = 1.0 * (height * width - pos_num) / pos_num char_map_weight[1:] = pos_weight return ( torch.from_numpy(char_map), torch.from_numpy(char_map_weight), torch.from_numpy(decoder_target), torch.from_numpy(word_target), )
def convert(self, mode): width, height = self.size if mode == "char_mask": if not self.use_char_ann: char_map = -np.ones((height, width)) char_map_weight = np.zeros((37,)) else: char_map = np.zeros((height, width)) char_map_weight = np.ones((37,)) for i, p in enumerate(self.char_boxes): poly=p.numpy().reshape(4,2) # x_center = np.mean(poly[:,0], axis = 0).astype(np.int32) # y_center = np.mean(poly[:,1], axis = 0).astype(np.int32) poly=shrink_poly(poly,0.25) cv2.fillPoly(char_map,[poly.astype(np.int32)],int(self.char_classes[i])) # if is_poly_inbox(poly, height, width) and x_center>=0 and x_center<width and y_center>=0 and y_center<height: # spoly=shrink_rect(poly,0.25) # spoly = spoly.astype(np.int32) # sbox_xmin_shrink = max(0, min(spoly[:,0])) # sbox_xmax_shrink = min(width - 1, max(spoly[:,0])) # sbox_ymin_shrink = max(0, min(spoly[:,1])) # sbox_ymax_shrink = min(height - 1, max(spoly[:,1])) # ## very small char box # if sbox_xmax_shrink == sbox_xmin_shrink: # sbox_xmax_shrink = sbox_xmin_shrink + 1 # if sbox_ymax_shrink == sbox_ymin_shrink: # sbox_ymax_shrink = sbox_ymin_shrink + 1 # char_map[sbox_ymin_shrink:sbox_ymax_shrink, sbox_xmin_shrink:sbox_xmax_shrink] = int(self.char_classes[i]) pos_index = np.where(char_map > 0) pos_num = pos_index[0].size if pos_num > 0: pos_weight = 1.0 * (height*width - pos_num)/pos_num char_map_weight[1:] = pos_weight return torch.from_numpy(char_map), torch.from_numpy(char_map_weight) elif mode == "seq_char_mask": decoder_target = (38 - 1) * np.ones((32, )) word_target = -np.ones((32, )) if not self.use_char_ann: char_map = -np.ones((height, width)) char_map_weight = np.zeros((37,)) for i, char in enumerate(self.word): if i > 31: break decoder_target[i] = char2num(char) word_target[i] = char2num(char) end_point = min(max(1, len(self.word)), 31) word_target[end_point] = 37 else: char_map = np.zeros((height, width)) char_map_weight = np.ones((37,)) word_length = 0 for i, p in enumerate(self.char_boxes): poly=p.numpy().reshape(4,2) # x_center = np.mean(poly[:,0], axis = 0).astype(np.int32) # y_center = np.mean(poly[:,1], axis = 0).astype(np.int32) # if is_poly_inbox(poly, height, width): if i < 32: decoder_target[i] = int(self.char_classes[i]) word_target[i] = int(self.char_classes[i]) word_length += 1 poly=shrink_poly(poly,0.25) cv2.fillPoly(char_map,[poly.astype(np.int32)],int(self.char_classes[i])) # if x_center>=0 and x_center<width and y_center>=0 and y_center<height: # spoly=shrink_rect(poly,0.25) # spoly = spoly.astype(np.int32) # sbox_xmin_shrink = max(0, min(spoly[:,0])) # sbox_xmax_shrink = min(width - 1, max(spoly[:,0])) # sbox_ymin_shrink = max(0, min(spoly[:,1])) # sbox_ymax_shrink = min(height - 1, max(spoly[:,1])) # ## very small char box # if sbox_xmax_shrink == sbox_xmin_shrink: # sbox_xmax_shrink = sbox_xmin_shrink + 1 # if sbox_ymax_shrink == sbox_ymin_shrink: # sbox_ymax_shrink = sbox_ymin_shrink + 1 # char_map[sbox_ymin_shrink:sbox_ymax_shrink, sbox_xmin_shrink:sbox_xmax_shrink] = int(self.char_classes[i]) end_point = min(max(1, word_length), 31) word_target[end_point] = 37 pos_index = np.where(char_map > 0) pos_num = pos_index[0].size if pos_num > 0: pos_weight = 1.0 * (height*width - pos_num)/pos_num char_map_weight[1:] = pos_weight return torch.from_numpy(char_map), torch.from_numpy(char_map_weight), torch.from_numpy(decoder_target), torch.from_numpy(word_target)