def test_toy_example_dets(): dir_annots_dets = 'toyexample/dets_classid_rel_xcycwh' yolo_files = general_utils.get_files_recursively(dir_annots_dets) assert len(yolo_files) > 0 for pascal_file in yolo_files: assert validations.is_yolo_format(pascal_file, bb_types=[BBType.DETECTED])
def yolo2bb(annotations_path, images_dir, file_obj_names, bb_type=BBType.GROUND_TRUTH): ret = [] if not os.path.isfile(file_obj_names): print(f'Warning: File with names of classes {file_obj_names} not found.') return ret # Load classes all_classes = [] with open(file_obj_names, "r") as f: all_classes = [line.replace('\n', '') for line in f] # Get annotation files in the path annotation_files = _get_annotation_files(annotations_path) # Loop through each file for file_path in annotation_files: if not validations.is_yolo_format(file_path, bb_types=[bb_type]): continue img_name = os.path.basename(file_path) img_file = general_utils.find_file(images_dir, img_name, match_extension=False) img_resolution = general_utils.get_image_resolution(img_file) if img_resolution is None: print(f'Warning: It was not possible to find the resolution of image {img_name}') continue img_size = (img_resolution['width'], img_resolution['height']) # Loop through lines with open(file_path, "r") as f: for line in f: splitted_line = line.split(' ') class_id = splitted_line[0] if not general_utils.is_str_int(class_id): print( f'Warning: Class id represented in the {file_path} is not a valid integer.') return [] class_id = int(class_id) if class_id not in range(len(all_classes)): print( f'Warning: Class id represented in the {file_path} is not in the range of classes specified in the file {file_obj_names}.' ) return [] if bb_type == BBType.GROUND_TRUTH: confidence = None x1 = float(splitted_line[1]) y1 = float(splitted_line[2]) w = float(splitted_line[3]) h = float(splitted_line[4]) elif bb_type == BBType.DETECTED: confidence = float(splitted_line[1]) x1 = float(splitted_line[2]) y1 = float(splitted_line[3]) w = float(splitted_line[4]) h = float(splitted_line[5]) bb = BoundingBox(image_name=general_utils.get_file_name_only(img_file), class_id=all_classes[class_id], coordinates=(x1, y1, w, h), img_size=img_size, confidence=confidence, type_coordinates=CoordinatesType.RELATIVE, bb_type=bb_type, format=BBFormat.XYWH) ret.append(bb) return ret