def _test_helper(self, exception=None): with tempfile.NamedTemporaryFile(suffix='.xml') as f: f.write(ET.tostring(self.root)) f.seek(0) if exception: with self.assertRaises(exception): _ = annotation.read(f.name) else: image_filename, boxes = annotation.read(f.name) return image_filename, boxes
def test_nominal_case(self): """Test successfully reading bounding boxes from PASCAL VOC XML file.""" boxes = list(annotation.read(self.filename)) self.assertEqual(len(boxes), 2) width = 400 height = 300 b = boxes[0] self.assertEqual(b.xmin, 10 / width) self.assertEqual(b.ymin, 20 / height) self.assertEqual(b.xmax, 30 / width) self.assertEqual(b.ymax, 40 / height)
def gen_csv_from_annotations( input_dir: str, output_file=constants.DEFAULT_CSV_FILENAME, out_path_prefix='', dataset_type=constants.DEFAULT_DATASET_TYPE): """Generates AutoML dataset CSV from annotation files. Args: input_dir: Directory of annotation files. output_file: Output CSV filename. out_path_prefix: Filepath prefix to prepend to the image files. e.g. src_image_filename = '/tmp/path/to/image.jpg' out_path_prefix = 'gs://bucket/images' output_image_filename = 'gs://bucket/images/image.jpg' dataset_type: Dataset type (TRAIN, VAL, TEST, UNSPECIFIED) to use for all the parsed images. """ if not gfile.exists(input_dir): raise ValueError('Input directory not found.') with gfile.GFile(os.path.expanduser(output_file), 'w') as outf: writer = csv.writer(outf, delimiter=',') for filename in gfile.listdir(os.path.expanduser(input_dir)): filepath = os.path.join(input_dir, filename) image_filename, boxes = annotation.read(filepath) out_image_filename = os.path.join(out_path_prefix, image_filename) for b in boxes: row = [ dataset_type, out_image_filename, b.label, b.xmin, b.ymin, '', '', b.xmax, b.ymax, '', '', ] writer.writerow(row)