【问题标题】:How to solve KeyError: 'img' in this code I found on Github如何解决我在 Github 上找到的代码中的 KeyError: \'img\'
【发布时间】:2022-08-14 03:31:50
【问题描述】:

我正在尝试从https://github.com/pochih/CBIR 进行基于内容的图像检索。我使用下面的短代码尝试根据颜色直方图检索图像。

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\'])

但是,我收到以下错误:

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\'

有人能告诉我如何解决这个错误吗?

  • 你能打印 res 的值吗?

标签: python cbir


【解决方案1】:

你永远不会“解决”这个问题,因为你的 "result" 不包含 img 属性,只有以下内容:

  • cls
  • dis

您必须使用来自cls 属性的数据,即result.cls(尝试打印vars(result.cls))或ap 对象(我对此一无所知,因此请谨慎对待)。

此外,您不能指望results 将像samples 一样排序,因为results 按距离dis 排序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多