使用imread下载图像数据集时出错

时间:2019-12-06 03:55:37

标签: python python-3.x numpy scikit-image

我正在从打开的图像数据集V4下载图像,事实证明,此刻出现错误。

代码如下:

<img src="https://images-eu.amazon.com/images/P/isbn.03.LZZZZZZZ.jpg" />

并输出:

from skimage import io

saved_dirs = ['/content/drive/My Drive/AI/Dataset/Open Images Dataset v4 (Bounding Boxes)/Person','/content/drive/My Drive/AI/Dataset/Open Images Dataset v4 (Bounding Boxes)/Mobile Phone','/content/drive/My Drive/AI/Dataset/Open Images Dataset v4 (Bounding Boxes)/Car']
classes = ['Person', 'Mobile phone', 'Car']

# Download images
for i in range(len(classes)):
    # Create the directory
    os.mkdir(saved_dirs[i])
    saved_dir = saved_dirs[i]
    for url in urls[i]:
        img = io.imread(url)
        saved_path = os.path.join(saved_dir, url[-20:])
        if img.shape[0] == 2:
               img = img[0]
        io.imsave(saved_path, img)

在我添加之前:

KeyErrorTraceback (most recent call last)
<ipython-input-33-3a84148b069d> in <module>()
      9         if img.shape[0] == 2:
     10                img = img[0]
---> 11         io.imsave(saved_path, img)

2 frames
/usr/local/lib/python2.7/dist-packages/skimage/util/dtype.pyc in dtype_limits(image, clip_negative)
     55         warn('The default of `clip_negative` in `skimage.util.dtype_limits` '
     56              'will change to `False` in version 0.15.')
---> 57     imin, imax = dtype_range[image.dtype.type]
     58     if clip_negative:
     59         imin = 0

KeyError: <type 'numpy.object_'>

现在将更多图像下载到不同的文件夹中,但最终还是以相同的方式下载。

我发现了这个问题,同样的问题KeyError: class 'numpy.object_' while downloading image dataset using imread

1 个答案:

答案 0 :(得分:2)

我已经从link you provided下载了subcar_img_url.csvsubperson_img_url.csvsubphone_img_url.csv并将它们保存在我当前的工作目录中。然后运行以下代码:

import pandas as pd
from skimage import io
import os

folder = 'path/of/your/current/working/directory'
for klass in ['car', 'person', 'phone']:
    fn = f'sub{klass}_img_url.csv'
    print(fn)
    df = pd.read_csv(os.path.join(folder, fn))
    for i, url in enumerate(df.image_url):
        print(i, end='-')
        img = io.imread(url)
        io.imsave(os.path.join(folder, url[-20:]), img)
    print('\n')

我能够毫无问题地下载所有图像(3000)。这是我得到的输出(没有引发异常):

subcar_img_url.csv
0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-
32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-49-50-51-52-53-54-55-56-57-58-59-
...
975-976-977-978-979-980-981-982-983-984-985-986-987-988-989-990-991-992-993-994-995-
996-997-998-999-

subperson_img_url.csv
0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-
32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-49-50-51-52-53-54-55-56-57-58-59-
...
975-976-977-978-979-980-981-982-983-984-985-986-987-988-989-990-991-992-993-994-995-
996-997-998-999-

subphone_img_url.csv
0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-
32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-49-50-51-52-53-54-55-56-57-58-59-
...
975-976-977-978-979-980-981-982-983-984-985-986-987-988-989-990-991-992-993-994-995-
996-997-998-999-

使用的scikit-image版本0.15.0。