Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <folder>`) 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

Expand Down
14 changes: 14 additions & 0 deletions roboflow/util/folderparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/util/test_folderparser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import contextlib
import io
import json
import os
import tempfile
Expand Down Expand Up @@ -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)
Expand Down
Loading