Skip to content

fix: read annotation files and labelmaps as utf-8 on every platform - #598

Open
adhavan18 wants to merge 1 commit into
roboflow:mainfrom
adhavan18:fix/read-annotations-as-utf8
Open

adhavan18 wants to merge 1 commit into
roboflow:mainfrom
adhavan18:fix/read-annotations-as-utf8

Conversation

@adhavan18

Copy link
Copy Markdown

What does this PR do?

on windows, uploading a dataset whose class names aren't plain ascii silently corrupts the names, or fails outright.

the annotation readers open files without an encoding, so python falls back to the locale code page (cp1252 on a default windows install) while the files are utf-8. hit by:

  • folder uploads (Workspace.upload_dataset / roboflow image upload <dir>): folderparser reading coco / createml json, csv and jsonl, plus load_labelmap for data.yaml and .labels
  • Project.upload(annotation_path=...), which is what reads voc xml and the other 1:1 annotation files
  • roboflow image annotate (annotation file and --labelmap)

repro on windows 11, python 3.12, main at 6e10039:

# train/_annotations.coco.json written as utf-8 with categories "café" and "señal"
parsed = parsefolder(root)
json.loads(parsed["images"][0]["annotationfile"]["rawText"])["categories"]
# -> names 'caf\xc3\xa9', 'se\xc3\xb1al'  (café, señal), uploaded like that, no warning

load_labelmap("classes.txt")   # same for data.yaml
# -> {0: 'caf\xc3\xa9', 1: 'se\xc3\xb1al'}

# a category "loďka" (ď is c4 8f in utf-8, and 0x8f is undefined in cp1252)
parsefolder(root)
# UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 66

the first case is the worse one since nothing fails, the project just ends up with mojibake class names. cjk names mostly go the same way.

changes

  • encoding="utf-8" on the 4 reads in folderparser.py, the 2 in load_labelmap, the one in Project._annotation_params and the 2 in roboflow image annotate
  • in _annotation_params the read now goes through the with handle instead of a second open(), since that line had to change anyway
  • changelog entry under unreleased

testing

the ci workflow sets PYTHONUTF8: 1, which turns this off on the windows runners, so the new test runs the parser in a child process with -X utf8=0, LC_ALL=C and PYTHONCOERCECLOCALE=0. that gives ascii on linux and cp1252 on windows, and it checks coco categories and data.yaml names come back unchanged.

  • without the fix it fails on both (windows: UnicodeDecodeError ... byte 0x8f, linux/wsl: wrong names / decode error), with it it passes
  • python -m unittest: 1313 tests ok on linux (wsl, python 3.12), tests.util.test_folderparser ok on windows
  • make check_code_quality clean, ruff format --check / ruff check on tests clean

notes

  • fix: close annotation file handle via context manager #484 touches the same _annotation_params lines (the leaked handle). this pr fixes that too as a side effect, so whichever lands second just needs a trivial rebase, happy to drop that hunk if you'd rather keep it with fix: close annotation file handle via context manager #484
  • files that really are in another encoding (a latin-1 voc xml, say) now raise on windows like they already did on linux and mac, instead of being read as cp1252
  • setup.py has the same problem with README.md (the 🏆 in it), which is probably why ci sets PYTHONUTF8. kept it out of this pr since it's a different path

folder uploads, Project.upload() and roboflow image annotate opened annotation
files and labelmaps without an encoding, so on windows they were decoded with
the locale code page (cp1252). a utf-8 class name like café was uploaded as
café, and characters like ď failed the whole upload with UnicodeDecodeError.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant