def load_annotations_gt(self): ret = [] if self.rad_gt_format_coco_json.isChecked(): ret = converter.coco2bb(self.dir_annotations_gt) elif self.rad_gt_format_cvat_xml.isChecked(): ret = converter.cvat2bb(self.dir_annotations_gt) elif self.rad_gt_format_openimages_csv.isChecked(): ret = converter.openimage2bb(self.dir_annotations_gt, self.dir_images_gt, BBType.GROUND_TRUTH) elif self.rad_gt_format_labelme_xml.isChecked(): ret = converter.labelme2bb(self.dir_annotations_gt) elif self.rad_gt_format_pascalvoc_xml.isChecked(): ret = converter.vocpascal2bb(self.dir_annotations_gt) elif self.rad_gt_format_imagenet_xml.isChecked(): ret = converter.imagenet2bb(self.dir_annotations_gt) elif self.rad_gt_format_abs_values_text.isChecked(): ret = converter.text2bb(self.dir_annotations_gt, bb_type=BBType.GROUND_TRUTH) elif self.rad_gt_format_yolo_text.isChecked(): ret = converter.yolo2bb(self.dir_annotations_gt, self.dir_images_gt, self.filepath_classes_gt, bb_type=BBType.GROUND_TRUTH) # Make all types as GT [bb.set_bb_type(BBType.GROUND_TRUTH) for bb in ret] return ret
def test_toy_example_gts(): ############################################################################ # Verify if all files in the toy example follow their expected format ############################################################################ # PASCAL VOC dir_annots_gts_pascal = 'toyexample/gts_vocpascal_format' files = general_utils.get_files_recursively(dir_annots_gts_pascal) assert len(files) > 0 for f in files: assert validations.is_pascal_format( f), 'File {f} does not follow the expected format (PASCAL VOC)' # COCO dir_annots_gts_coco = 'toyexample/gts_coco_format' files = general_utils.get_files_recursively(dir_annots_gts_coco) assert len(files) > 0 for f in files: assert validations.is_coco_format(f), 'File {f} does not follow the expected format (COCO)' ############################################################################ # Compare if all bounding boxes are the same ############################################################################ pascal_bbs = converter.vocpascal2bb(dir_annots_gts_pascal) coco_bbs = converter.coco2bb(dir_annots_gts_coco) coco_bbs.sort(key=lambda x: str(x), reverse=True) pascal_bbs.sort(key=lambda x: str(x), reverse=True) assert len(coco_bbs) == len(pascal_bbs) for coco_bb, pascal_bb in zip(coco_bbs, pascal_bbs): assert coco_bb == pascal_bb
def test_converters_gts(): # Defining paths with images and annotations images_dir = 'data/database/images' gts_dir = 'data/database/gts' assert os.path.isdir(images_dir) assert os.path.isdir(gts_dir) # COCO coco_dir = os.path.join(gts_dir, 'coco_format_v1') coco_bbs_v1 = converter.coco2bb(coco_dir) coco_bbs_v1.sort(key=lambda x: str(x), reverse=True) # COCO coco_dir = os.path.join(gts_dir, 'coco_format_v2') coco_bbs_v2 = converter.coco2bb(coco_dir) coco_bbs_v2.sort(key=lambda x: str(x), reverse=True) # CVAT cvat_dir = os.path.join(gts_dir, 'cvat_format') cvat_bbs = converter.cvat2bb(cvat_dir) cvat_bbs.sort(key=lambda x: str(x), reverse=True) # IMAGENET imagenet_dir = os.path.join(gts_dir, 'imagenet_format/Annotations') imagenet_bbs = converter.imagenet2bb(imagenet_dir) imagenet_bbs.sort(key=lambda x: str(x), reverse=True) # LABEL ME labelme_dir = os.path.join(gts_dir, 'labelme_format') labelme_bbs = converter.labelme2bb(labelme_dir) labelme_bbs.sort(key=lambda x: str(x), reverse=True) # OPEN IMAGE openimage_dir = os.path.join(gts_dir, 'openimages_format') openimage_bbs = converter.openimage2bb(openimage_dir, images_dir) openimage_bbs.sort(key=lambda x: str(x), reverse=True) # VOC PASCAL vocpascal_dir = os.path.join(gts_dir, 'pascalvoc_format') vocpascal_bbs = converter.vocpascal2bb(vocpascal_dir) vocpascal_bbs.sort(key=lambda x: str(x), reverse=True) # YOLO yolo_annotations_dir = os.path.join(gts_dir, 'yolo_format/obj_train_data') yolo_names_file = os.path.join(gts_dir, 'yolo_format/obj.names') yolo_bbs = converter.yolo2bb(yolo_annotations_dir, images_dir, yolo_names_file, bb_type=BBType.GROUND_TRUTH) yolo_bbs.sort(key=lambda x: str(x), reverse=True) assert len(coco_bbs_v1) == len(coco_bbs_v2) == len(cvat_bbs) == len( imagenet_bbs) == len(labelme_bbs) == len(openimage_bbs) == len( vocpascal_bbs) == len(yolo_bbs) for coco_bb_v1, coco_bb_v2, cvat_bb, imagenet_bb, labelme_bb, openimage_bb, vocpascal_bb, yolo_bb in zip( coco_bbs_v1, coco_bbs_v2, cvat_bbs, imagenet_bbs, labelme_bbs, openimage_bbs, vocpascal_bbs, yolo_bbs): assert coco_bb_v1 == coco_bb_v2 == cvat_bb == imagenet_bb == labelme_bb == openimage_bb == vocpascal_bb == yolo_bb
plt.xlabel('amount of bounding boxes') plt.ylabel('classes') else: plt.bar(dict_bbs_per_class.keys(), dict_bbs_per_class.values()) plt.xlabel('classes') plt.ylabel('amount of bounding boxes') plt.xticks(rotation=rotation) title = f'Distribution of bounding boxes per class {extra_title}' plt.title(title) if show: plt.tick_params(axis='x', labelsize=10) # Set the x-axis label size plt.show(block=True) return plt # Get annotations (ground truth and detections) gt_bbs = converter.vocpascal2bb(dir_gts) det_bbs = converter.text2bb(dir_dets, bb_type=BBType.DETECTED, bb_format=BBFormat.XYWH,type_coordinates=CoordinatesType.ABSOLUTE, img_dir=dir_imgs) # Leave only the annotations of cats gt_bbs = [bb for bb in gt_bbs if bb.get_class_id() == 'cat'] det_bbs = [bb for bb in det_bbs if bb.get_class_id() == 'cat'] # Uncomment to plot the distribution bounding boxes per classes # dict_gt = BoundingBox.get_amount_bounding_box_all_classes(gt_bbs, reverse=False) # plot_bb_per_classes(dict_gt, horizontally=True, rotation=0, show=True, extra_title=' (groundtruths)') # clases_gt = [b.get_class_id() for b in gt_bbs] # dict_det = BoundingBox.get_amount_bounding_box_all_classes(det_bbs, reverse=True) # general_utils.plot_bb_per_classes(dict_det, horizontally=False, rotation=80, show=True, extra_title=' (detections)') ############################################################# # EVALUATE WITH COCO METRICS
cv2.putText(image, label, (xin_bb, yin_bb), font, fontScale, (0, 0, 0), fontThickness, cv2.LINE_AA) return image # Drawing bb into the images green_color = [62, 235, 59] red_color = [255, 0, 0] for img_file in general_utils.get_files_recursively(dir_imgs): # Get corresponding GT bounding boxes gt_annotation_file = general_utils.find_file(dir_gts, os.path.basename(img_file), match_extension=False) if gt_annotation_file is None: continue gt_bbs = converter.vocpascal2bb(gt_annotation_file) # Get corresponding detections bounding boxes det_annotation_file = general_utils.find_file(dir_dets, os.path.basename(img_file), match_extension=False) if det_annotation_file is None: det_bbs = [] else: det_bbs = converter.text2bb(det_annotation_file, bb_type=BBType.DETECTED, bb_format=BBFormat.XYWH, type_coordinates=CoordinatesType.ABSOLUTE, img_dir=dir_imgs) # Leave only the annotations of cats gt_bbs = [bb for bb in gt_bbs if bb.get_class_id() == 'cat'] det_bbs = [bb for bb in det_bbs if bb.get_class_id() == 'cat']