簡體   English   中英

如何解決 KeyError:'img'?

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

我正在嘗試從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'

有人能告訴我如何解決這個錯誤嗎?

您永遠不會“解決”這個問題,因為您的"result"不包含img屬性,僅包含以下內容:

  • cls
  • dis

您必須使用來自cls屬性的數據,即result.cls (嘗試打印vars(result.cls) )或ap對象(我對此一無所知,因此請謹慎使用)。

此外,您不能指望results會像samples一樣排序,因為results是按距離dis排序的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM