def main(gt_fp: str): gt = OBBAnns(gt_fp) gt.load_annotations() root = split(gt_fp)[0] # First make perfect proposals bboxes = gt.ann_info[['bbox', 'cat_id', 'img_id']] proposals = {'proposals': bboxes.to_dict('records')} with open(join(root, 'proposals_perfect.json'), 'w') as prop_file: json.dump(proposals, prop_file) # Now randomly "forget" certain proposals selector = [ True if random() > 0.2 else False for _ in range(len(gt.ann_info)) ] bboxes = gt.ann_info[['bbox', 'cat_id', 'img_id']][selector].apply(fudge_bboxes, 1, result_type='expand') proposals = {'proposals': bboxes.to_dict('records')} with open(join(root, 'proposals.json'), 'w') as prop_file: json.dump(proposals, prop_file)
def load_annotations(self, ann_file): self.obb = OBBAnns(ann_file) self.obb.load_annotations() self.obb.set_annotation_set_filter(['deepscores']) # self.obb.set_class_blacklist(["staff"]) self.cat_ids = list(self.obb.get_cats().keys()) self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)} self.label2cat = {v: k for k, v in self.cat2label.items()} self.CLASSES = tuple( [v["name"] for (k, v) in self.obb.get_cats().items()]) self.img_ids = [id['id'] for id in self.obb.img_info] return self.obb.img_info
def __init__(self, image_set, year, devkit_path=None): imdb.__init__(self, 'DeepScoresV2' + year + '_' + image_set) self._year = year self._devkit_path = self._get_default_path() if devkit_path is None \ else devkit_path self._image_set = image_set self._data_path = self._devkit_path + "/images" self.blacklist = ["staff", 'legerLine'] self.o = OBBAnns(self._devkit_path+'/deepscores_'+image_set+'.json') self.o.load_annotations() print(self.o.annotation_sets) self.o.set_annotation_set_filter(['deepscores']) self.o.set_class_blacklist(self.blacklist) self._classes = [v["name"] for (k, v) in self.o.get_cats().items()] self._class_ids = [k for (k, v) in self.o.get_cats().items()] self._class_to_ind = dict(list(zip(self.classes, list(range(self.num_classes))))) self._class_ids_to_ind = dict(list(zip(self._class_ids, list(range(self.num_classes))))) self._ind_to_class_ids = {v: k for k, v in self._class_ids_to_ind.items()} self._image_index = self._load_image_set_index() # self.cat_ids = list(self.o.get_cats().keys()) # self.cat2label = { # cat_id: i # for i, cat_id in enumerate(self.cat_ids) # } # self.label2cat = {v: k for k, v in self.cat2label.items()} # self.CLASSES = tuple([v["name"] for (k, v) in self.o.get_cats().items()]) # self.img_ids = [id['id'] for id in self.o.img_info] self._image_ext = '.png' # Default to roidb handler self._roidb_handler = self.gt_roidb self._salt = str(uuid.uuid4()) self._comp_id = 'comp4' # PASCAL specific config options self.config = {'cleanup': True, 'use_salt': True, 'use_diff': False, 'matlab_eval': False, 'rpn_file': None}
import shutil import json import numpy as np from obb_anns import OBBAnns from pathlib import Path ann_in_1 = OBBAnns('data/deep_scores_dense/deepscores_test.json') ann_in_2 = OBBAnns('../ili_subset/scores.json') SUFFIX = '_scn' ann_out_path = Path('..', 'ds_test_ili', 'deepscores_test.json') ann_in_1.load_annotations() ann_in_2.load_annotations() target_ds_root = ann_out_path.parent assert target_ds_root.exists(), f"Directory of the target dataset ({str(target_ds_root)}) must exist!" # Make dirs images_path = Path('images') segmentation_path = Path('segmentation') instance_path = Path('instance') for subdir in [images_path, segmentation_path, instance_path]: (target_ds_root / subdir).mkdir(exist_ok=True) print("Copying image files") def copy_images(ann: OBBAnns, suffix: str = ''): ds_root = Path(ann.ann_file).parent for img_info in ann.img_info: img_path = Path(img_info['filename']) img_path_new = img_path.with_name(img_path.with_suffix('').name + suffix + img_path.suffix)
parser.add_argument('PROPOSAL', type=str, nargs='?', help='name of the proposals json') return parser.parse_args() def extract_bbox_list(): all_bboxes = [] return all_bboxes if __name__ == '__main__': args = parse_args() a = OBBAnns(join(args.ROOT, args.ANNS)) a.load_annotations() a.set_annotation_set_filter(['deepscores']) np_annotations = np.stack(a.ann_info['a_bbox']) height = np_annotations[:, 3] - np_annotations[:, 1] width = np_annotations[:, 2] - np_annotations[:, 0] aspect_ratio = height / width area = a.ann_info['area'] d = { 'height': height, 'width': width, 'aspect_ratio': aspect_ratio, 'area': area }
parser = ArgumentParser(description='runs the obb_anns.py file') parser.add_argument('ROOT', type=str, help='path to the root of the dataset directory') return parser.parse_args() if __name__ == '__main__': args = parse_args() root_dir = Path(args.ROOT) file_names_in_annotations = [] num_ann_files = len(list(root_dir.glob("*.json"))) for i, dataset_ann_fp in enumerate(root_dir.glob("*.json")): print(f'Checking file {i + 1} of {num_ann_files}...') a = OBBAnns(join(args.ROOT, dataset_ann_fp)) a.load_annotations() a.set_annotation_set_filter(['deepscores']) for img in tqdm(a.img_info, unit='imgs'): file_names_in_annotations.append(img['filename']) try: b = a.get_anns(img_id=img['id']) except: print(f'{img["id"]} caused an exception') file_names_in_annotations = set(file_names_in_annotations) images_dir = root_dir / 'images_png' print("Checking if every image has its annotation in the dataset...")
from PIL.PngImagePlugin import PngImageFile from obb_anns import OBBAnns from pathlib import Path # source_images = Path('..', 'ili_subset', 'images') # source_images = Path('..', 'scanned_deepscore_images_png') source_images = Path('..', 'scanned_deepscore_images_clean') # source_index = Path('..', 'clean_scans') # source_index = Path('..', 'scanned_deepscore_images_png') source_index = Path('..', 'scanned_deepscore_images_clean') # target_ds = Path('..', 'ili_subset') target_ds = Path('..', 'deepscores_scanned') target_ds_file = target_ds / 'deepscores.json' orig_ann_root = Path('data', 'deep_scores_dense') orig_ann_train = OBBAnns(str(orig_ann_root / 'deepscores_train.json')) orig_ann_train.load_annotations() orig_ann_test = OBBAnns(str(orig_ann_root / 'deepscores_test.json')) orig_ann_test.load_annotations() def construct_reverse_lookup(ann: OBBAnns) -> dict: reverse_lookup = {} for entry in ann.img_info: reverse_lookup[entry['filename']] = entry return reverse_lookup reverse_lookup_train = construct_reverse_lookup(orig_ann_train) reverse_lookup_test = construct_reverse_lookup(orig_ann_test)
def main(image_set, single_scale=False): # (1) if image_set == 'train': #dir_txt = os.path.join(dir_dataset, 'labelTxt', image_set) out_dir_json = os.path.join(dir_dataset, 'annotations', image_set) os.makedirs(out_dir_json, exist_ok=True) #txt2json(dir_txt, out_dir_json) o = OBBAnns(dir_dataset + 'deepscores_train.json') o.load_annotations() #print(o) cats = o.get_cats() img_idxs = [i for i in range(len(o.img_info))] imgs, anns = o.get_img_ann_pair(idxs=img_idxs, ann_set_filter="deepscores") filenames = [] for img in anns: objs = [] img_np = np.array(img) for object_instance in img_np: obj = dict() coord = np.array(object_instance[1], dtype=np.float32).reshape([4, 2]) bbox = cv.boxPoints(cv.minAreaRect(coord)).astype( np.int).tolist() obj['name'] = cats[object_instance[2][0]]['name'] obj['bbox'] = bbox objs.append(obj) if objs: filename = o.get_imgs(ids=[int(img_np[0][4])])[0]['filename'] filenames.append(os.path.splitext(filename)[0]) json_filename = os.path.splitext(filename)[0] + '.json' json.dump(objs, open(os.path.join(out_dir_json, json_filename), 'wt'), indent=2) # Split images and json annotations in train and val files out_dir_train = os.path.join(dir_dataset, 'images2', 'train') out_dir_val = os.path.join(dir_dataset, 'images2', 'val') out_dir_val_json = os.path.join(dir_dataset, 'annotations', 'val') out_dir_test = os.path.join(dir_dataset, 'images2', 'test') os.makedirs(out_dir_train, exist_ok=True) os.makedirs(out_dir_val, exist_ok=True) os.makedirs(out_dir_test, exist_ok=True) os.makedirs(out_dir_val_json, exist_ok=True) filenames_train, filenames_val = train_test_split(filenames, test_size=272, random_state=8) for filename in os.listdir(os.path.join(dir_dataset, 'images')): if os.path.splitext(filename)[0] in filenames_train: shutil.move(os.path.join(dir_dataset, 'images', filename), os.path.join(out_dir_train, filename)) elif os.path.splitext(filename)[0] in filenames_val: shutil.move(os.path.join(dir_dataset, 'images', filename), os.path.join(out_dir_val, filename)) shutil.move( os.path.join(out_dir_json, os.path.splitext(filename)[0] + '.json'), os.path.join(out_dir_val_json, os.path.splitext(filename)[0] + '.json')) else: shutil.move(os.path.join(dir_dataset, 'images', filename), os.path.join(out_dir_test, filename)) os.rmdir(os.path.join(dir_dataset, 'images')) os.rename(os.path.join(dir_dataset, 'images2'), os.path.join(dir_dataset, 'images')) # (2) pairs = [] for filename in os.listdir(os.path.join(dir_dataset, 'images', image_set)): anno = os.path.join(dir_dataset, 'annotations', image_set, filename.replace('png', 'json')) img = os.path.join(dir_dataset, 'images', image_set, filename) if not os.path.exists(anno): anno = None pairs.append([img, anno]) overlap = 0.25 sizes = [768] if single_scale else [512, 768, 1024, 1536] save_empty = image_set == 'test' image_set = f"{image_set}-{sizes[0]}" if single_scale else image_set out_dir_images = os.path.join(dir_dataset, 'images', f'{image_set}-crop') out_dir_annos = os.path.join(dir_dataset, 'annotations', f'{image_set}-crop') cropper = Cropper(sizes, overlap) cropper.crop_batch(pairs, out_dir_images, out_dir_annos, save_empty) # (3) pairs = [] for filename in os.listdir(out_dir_images): img = os.path.join('images', f'{image_set}-crop', filename) anno = None if image_set == 'test' else os.path.join( 'annotations', f'{image_set}-crop', filename.replace( 'jpg', 'json')) pairs.append([img, anno]) out_dir = os.path.join(dir_dataset, 'image-sets') os.makedirs(out_dir, exist_ok=True) json.dump(pairs, open(os.path.join(out_dir, f'{image_set}.json'), 'wt'), indent=2)
from mmdet.apis import init_detector, inference_detector import mmcv import os import cv2 from mmdet.core import rotated_box_to_poly_np from mmcv.visualization import imshow_det_bboxes import numpy as np from obb_anns import OBBAnns annotations_file = "data/deep_scores_dense/deepscores_test.json" obb = OBBAnns(annotations_file) obb.load_annotations() obb.set_annotation_set_filter(['deepscores']) CLASSES = tuple([v["name"] for (k, v) in obb.get_cats().items()]) config_file = 'DeepScoresV2_s2anet/fullrez_crop/s2anet_r50_fpn_1x_deepscoresv2_tugg_lowrez.py' checkpoint_file = 'DeepScoresV2_s2anet/fullrez_crop/epoch_500.pth' model_name = "s2anet_fullrez_crop" images_folder = "/home/tugg/Documents/RealScores/Realworld_Test" resize = 1.0 model = init_detector(config_file, checkpoint_file, device='cuda:0') images = os.listdir(images_folder) os.makedirs(os.path.join(images_folder, model_name), exist_ok=True) for img in images: if os.path.isdir(os.path.join(images_folder, img)): continue
else: label = self.cat_info[cat_id]['name'] if print_label: pos = print_text_label(pos, label, '#ffffff', '#303030') if print_onset and 'onset' in parsed_comments.keys(): pos = print_text_label(pos, parsed_comments['onset'], '#ffffff', '#091e94') if print_staff_pos and 'rel_position' in parsed_comments.keys(): print_text_label(pos, parsed_comments['rel_position'], '#ffffff', '#0a7313') return draw ann = OBBAnns('../scanned_ds/ili_scores.json') ann.load_annotations() data_root = '../scanned_ds' out_dir = 'out_ili' annotation_set = 'deepscores' annotation_set = ann.annotation_sets.index(annotation_set) ann.chosen_ann_set = ann.chosen_ann_set[annotation_set] for img_info in ann.img_info: img_id = img_info['id'] ann.visualize(img_id=img_id, data_root=data_root, out_dir=out_dir) # img_info, ann_info = ann.get_img_ann_pair(ids=[img_id]) # img_info, ann_info = img_info[0], ann_info[0] # # # Get the data_root from the ann_file path if it doesn't exist # if data_root is None: # data_root = osp.split(ann.ann_file)[0]