python-3.ximageimage-processingdeep-learningmscoco

How to change the sample image in MS-COCO demo (Image captioning)


I am working on Image captioning, and I found this great tutorial for MS-COCO challenge. MS_COCO Image captioning demo

Anyway, I ran the demo and everything went well. Except, when I try to change the sample image (I want to run another images), I get this error:

KeyError                                  Traceback (most recent call last)
<ipython-input-126-68bce2986aae> in <module>()
      1 # load and display image
----> 2 I = io.imread('%s/images/%s/%s'%(dataDir,dataType,img['000000014226.jpg']))
      3 
      4 plt.axis('off')
      5 plt.imshow(I)

KeyError: '000000014226.jpg'

I know that the code responsible for this error is this:

# load and display image
I = io.imread('%s/images/%s/%s'%(dataDir,dataType,img['000000014226.jpg']))
# use url to load image
# I = io.imread(img['coco_url'])  # originally they used to run this
plt.axis('off')
plt.imshow(I)
plt.show()

I made sure that I am in the directory of val2017. So what is the issue here? Any ideas. Many thanks!


Solution

  • From the demo, the code to get a random image from "person", "dog" or "skateboard" is:

    catIds = coco.getCatIds(catNms=['person','dog','skateboard']);
    imgIds = coco.getImgIds(catIds=catIds );
    imgIds = coco.getImgIds(imgIds = [324158])
    img = coco.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0]
    

    I'm not too sure how you edited the above code and got 14226.jpg but it might be that you didn't change this specific part of the code above? So maybe it resulted in the situation whereby they can't find the image?

    Also, I'm not sure if nesting the script in val2017 directory works, because I think the instructions are to place images (val2017) in a folder called "images" and the annotations in a folder called "annotations". I put my own scripts (using a local text editor, personally not using ipython) in PythonAPI directory.