Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update download method to support nested file structure #680

Open
csbrown opened this issue Jan 3, 2025 · 0 comments
Open

update download method to support nested file structure #680

csbrown opened this issue Jan 3, 2025 · 0 comments

Comments

@csbrown
Copy link

csbrown commented Jan 3, 2025

When I create a COCO-format file for my images, if the filenames are nested, then the download method fails.

For example, with the following COCO-format file named 'tiny_image.json':

{
  "info": {},
  "images": [
    {
      "id": 1,
      "file_name": "yummy/taco.jpg",
      "coco_url": "https://en.m.wikipedia.org/wiki/File:NCI_Visuals_Food_Taco.jpg"
    }
  ],
  "annotations": [],
  "licenses": []
}

Note the file_name, which contains a nested file.

When attempting to use the download method, we get the following:

>>> from pycocotools.coco import COCO
>>> coco = COCO('tiny_image.json')
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
>>> coco.download('.')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/scott/NOAA/research/deep7_modeling/venv/lib/python3.11/site-packages/pycocotools/coco.py", line 390, in download
    urlretrieve(img['coco_url'], fname)
  File "/usr/lib/python3.11/urllib/request.py", line 251, in urlretrieve
    tfp = open(filename, 'wb')
          ^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: './yummy/taco.jpg'

This is because the urlretrieve method does not automatically create the necessary directory structure. I think a one or two line fix of:

os.makedirs(os.path.dirname(filename), exist_ok=True)

On the line immediately before the call to urlretrieve will enable this. Happy to submit a PR if there is a maintainer willing to pull it.

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

No branches or pull requests

1 participant