diff --git a/CHANGELOG.md b/CHANGELOG.md index 22948ff3..41b88085 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,11 @@ All notable changes to this project will be documented in this file. the artifact that the API serves, for example `weights.onnx`. Before, it always used `weights.pt` ([#582](https://github.com/roboflow/roboflow-python/pull/582)). +- Folder uploads (`upload_dataset()` and `roboflow image upload `) skip + a CSV with fewer than six columns, such as a table of image metadata, with a + warning. Before, its rows were sent as the annotation of every image they + named, which failed the save, even when the folder's annotation CSV also + listed the image ([#583](https://github.com/roboflow/roboflow-python/pull/583)). ## 1.7.3 diff --git a/roboflow/util/folderparser.py b/roboflow/util/folderparser.py index 047cdaee..32be6bde 100644 --- a/roboflow/util/folderparser.py +++ b/roboflow/util/folderparser.py @@ -290,6 +290,7 @@ def _filterIndividualAnnotations(image, annotation, format, imgRefMap, annotatio def _loadAnnotations(folder, annotations): valid_extensions = {".json", ".csv", ".jsonl"} annotations = [a for a in annotations if a["extension"] in valid_extensions] + annotations = [a for a in annotations if a["extension"] != ".csv" or _is_annotation_csv(folder, a)] for ann in annotations: extension = ann["extension"] if extension == ".json": @@ -309,6 +310,19 @@ def _loadAnnotations(folder, annotations): return annotations +def _is_annotation_csv(folder, ann): + # _classes.csv holds multi-label classification. The rows of any other CSV go to the server, whose CSV + # parser reads boxes only from rows of six or more columns: a narrower CSV, like a table of image metadata, + # holds no annotations. + if ann["name"] == "_classes.csv": + return True + with open(f"{folder}{ann['file']}") as f: + columns = len(f.readline().split(",")) + if columns < 6: + print(f"warning: skipping {ann['file']}: annotation CSVs have at least six columns") + return columns >= 6 + + def _read_jsonl(path): data = [] with open(path) as file: diff --git a/tests/util/test_folderparser.py b/tests/util/test_folderparser.py index 4f9ddb5b..89373b01 100644 --- a/tests/util/test_folderparser.py +++ b/tests/util/test_folderparser.py @@ -1,3 +1,5 @@ +import contextlib +import io import json import os import tempfile @@ -54,6 +56,26 @@ def test_parse_mosquitos_csv(self): expected += "train_10308.jpeg,1058,943,japonicus/koreicus,28,187,908,815\n" assert testImage["annotationfile"]["rawText"] == expected + def test_parse_csv_skips_metadata_csv(self): + # A metadata table lists the same images as the annotation CSV, and sorts before it. + with tempfile.TemporaryDirectory() as tmpdir: + os.makedirs(os.path.join(tmpdir, "train")) + for name in ("a.jpg", "b.jpg"): + open(os.path.join(tmpdir, "train", name), "wb").close() + annotations = "filename,width,height,class,xmin,ymin,xmax,ymax\na.jpg,640,480,cat,10,20,110,220\n" + with open(os.path.join(tmpdir, "train", "_annotations.csv"), "w") as f: + f.write(annotations) + with open(os.path.join(tmpdir, "metadata.csv"), "w") as f: + f.write("filename,split,count,status\na.jpg,train,1,ok\nb.jpg,train,0,ok\n") + + with contextlib.redirect_stdout(io.StringIO()) as stdout: + parsed = folderparser.parsefolder(tmpdir) + + images = {i["file"]: i for i in parsed["images"]} + self.assertEqual(images["/train/a.jpg"]["annotationfile"], {"name": "annotation.csv", "rawText": annotations}) + self.assertIsNone(images["/train/b.jpg"].get("annotationfile")) + self.assertIn("/metadata.csv", stdout.getvalue()) + def test_paligemma_format(self): folder = f"{thisdir}/../datasets/paligemma" parsed = folderparser.parsefolder(folder)