简体   繁体   English

如何解决 KeyError:'img'?

[英]How to solve KeyError: 'img'?

I'm trying the content based image retrieval from https://github.com/pochih/CBIR .我正在尝试从https://github.com/pochih/CBIR进行基于内容的图像检索。 I used the short code below to try retrieve images based on color histogram.我使用下面的短代码尝试根据颜色直方图检索图像。

from evaluate import infer
from six.moves import cPickle
import os

CACHE_DIR = 'cache'
SAMPLE_CACHE = 'histogram_cache-region-n_bin12-n_slice3' # Change this to your cache
NUM_IMAGES = 10

samples = cPickle.load(open(os.path.join(CACHE_DIR, SAMPLE_CACHE), "rb"))

query = samples[30]

ap, res = infer(query, samples, db=None, sample_db_fn=None, depth=NUM_IMAGES, d_type='d1')

print('Query: {}'.format(query['img']))

print ('Top {} similar images: '.format(NUM_IMAGES))

for result in res:
  print(result['img'])

However, I got the error below:但是,我收到以下错误:

Query: database\bawang\bawang_126.jpg
Top 10 similar images: 
Traceback (most recent call last):
  File "C:/Users/HP/PycharmProjects/USM/CBIRR/src/retrieve.py", line 20, in <module>
    print(result['img'])
KeyError: 'img'

Can someone tell me how to fix this error?有人能告诉我如何解决这个错误吗?

You will never "solve" this because your "result" doesn't contain img attribute, only the following:您永远不会“解决”这个问题,因为您的"result"不包含img属性,仅包含以下内容:

  • cls
  • dis

You must use data either from the cls attribute, that is result.cls (try to print vars(result.cls) ) or ap object (which I don't know anything about so take it with grain of salt).您必须使用来自cls属性的数据,即result.cls (尝试打印vars(result.cls) )或ap对象(我对此一无所知,因此请谨慎使用)。

Also, you can't count on the fact that results will be ordered like samples because results are sorted by the distance dis .此外,您不能指望results会像samples一样排序,因为results是按距离dis排序的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM