def __airbus_ship_parse__(self, label_file, image_file): """ (xmin, ymin, xmax, ymax) """ image_file_name = os.path.splitext(os.path.basename(image_file))[0] save_image = wwtool.generate_image(800, 800, color=(0, 0, 0)) img = cv2.imread(image_file) img_height, img_width, _ = img.shape image_objects = airbus_ship_parse.airbus_ship_parse(os.path.basename(image_file)) objects = [] objects_small_save = [] total_object_num = len(image_objects) small_object_num = 0 large_object_num = 0 total_object_num = 0 for image_object in image_objects: object_struct = {} object_struct_small = {} xmin, ymin, xmax, ymax = image_object['bbox'] bbox_w = xmax - xmin bbox_h = ymax - ymin if bbox_w * bbox_h <= self.small_object_area: continue total_object_num += 1 if bbox_h * bbox_w <= small_size: small_object_num += 1 if bbox_h * bbox_w >= large_object_size: large_object_num += 1 object_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h] object_struct['segmentation'] = wwtool.bbox2pointobb([xmin, ymin, xmax, ymax]) object_struct['label'] = 1 object_struct_small = object_struct.copy() object_struct_small['bbox'] = [xmin, ymin, xmax, ymax] object_struct_small['label'] = 'ship' objects_small_save.append(object_struct_small) objects.append(object_struct) if total_object_num > self.max_object_num_per_image: self.max_object_num_per_image = total_object_num if just_keep_small or generate_small_dataset: if small_object_num >= total_object_num * small_object_rate and large_object_num < 1 and len(objects_small_save) > 0: # print(os.path.join(dst_image_path, image_file_name + '.png')) save_image[0:img_height, 0:img_width, :] = img cv2.imwrite(os.path.join(dst_image_path, image_file_name + '.png'), save_image) anno_file = os.path.join(dst_label_path, image_file_name + '.txt') wwtool.simpletxt_dump(objects_small_save, anno_file) return objects else: return [] else: return objects
def __dota_parse__(self, label_file, image_file): """ (xmin, ymin, xmax, ymax) """ with open(label_file, 'r') as f: lines = f.readlines() objects = [] total_object_num = len(lines) small_object_num = 0 large_object_num = 0 total_object_num = 0 basic_label_str = " " for line in lines: object_struct = {} line = line.rstrip().split(' ') label = basic_label_str.join(line[4:]) bbox = [float(_) for _ in line[0:4]] xmin, ymin, xmax, ymax = bbox bbox_w = xmax - xmin bbox_h = ymax - ymin if bbox_w * bbox_h <= self.small_object_area: continue total_object_num += 1 if bbox_h * bbox_w <= small_size: small_object_num += 1 if bbox_h * bbox_w >= large_object_size: large_object_num += 1 object_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h] object_struct['segmentation'] = wwtool.bbox2pointobb( [xmin, ymin, xmax, ymax]) object_struct['label'] = original_class[label] objects.append(object_struct) if total_object_num > self.max_object_num_per_image: self.max_object_num_per_image = total_object_num if just_keep_small or generate_small_dataset: if small_object_num >= total_object_num * small_object_rate and large_object_num < 1: return objects else: return [] else: return objects
def __dior_parse__(self, label_file, image_file): """ (xmin, ymin, xmax, ymax) """ objects = [] tree = ET.parse(label_file) root = tree.getroot() total_object_num = len(root.findall('object')) small_object_num = 0 large_object_num = 0 total_object_num = 0 for single_object in root.findall('object'): bndbox = single_object.find('bndbox') object_struct = {} xmin = float(bndbox.find('xmin').text) ymin = float(bndbox.find('ymin').text) xmax = float(bndbox.find('xmax').text) ymax = float(bndbox.find('ymax').text) bbox_w = xmax - xmin bbox_h = ymax - ymin total_object_num += 1 if bbox_h * bbox_w <= small_size: small_object_num += 1 if bbox_h * bbox_w >= large_object_size: large_object_num += 1 object_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h] object_struct['segmentation'] = wwtool.bbox2pointobb( [xmin, ymin, xmax, ymax]) object_struct['label'] = original_dior_class[single_object.find( 'name').text] objects.append(object_struct) if total_object_num > self.max_object_num_per_image: self.max_object_num_per_image = total_object_num if just_keep_small or generate_small_dataset: if small_object_num >= total_object_num * small_object_rate and large_object_num < 1: return objects else: return [] else: return objects
def __airbus_ship_parse__(self, label_file, image_file): """ (xmin, ymin, xmax, ymax) """ image_objects = airbus_ship_parse.airbus_ship_parse( os.path.basename(image_file)) objects = [] total_object_num = len(image_objects) small_object_num = 0 large_object_num = 0 total_object_num = 0 for image_object in image_objects: object_struct = {} xmin, ymin, xmax, ymax = image_object['bbox'] bbox_w = xmax - xmin bbox_h = ymax - ymin total_object_num += 1 if bbox_h * bbox_w <= small_size: small_object_num += 1 if bbox_h * bbox_w >= large_object_size: large_object_num += 1 object_struct['bbox'] = [xmin, ymin, bbox_w, bbox_h] object_struct['segmentation'] = wwtool.bbox2pointobb( [xmin, ymin, xmax, ymax]) object_struct['label'] = 1 objects.append(object_struct) if total_object_num > self.max_object_num_per_image: self.max_object_num_per_image = total_object_num if just_keep_small or generate_small_dataset: if small_object_num >= total_object_num * small_object_rate and large_object_num < 1: return objects else: return [] else: return objects