Skip to content

update download method to support nested file structure #680

Open
@csbrown

Description

@csbrown

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions