Reputation: 862
i have a dictionary, where i have taken the images as value and indexes as key, i have stored it using zip function, and when am trying to retrieve it, its not displaying the images. what i have done is:
pth = 'D:\6th sem\Major project\Code'
resizedlist = dict()
for infile in glob.glob(os.path.join(path,'*.jpg')):
imge = cv2.imread(infile)
re_img = cv2.resize(imge,(256,256))
ImEdges = cv2.imwrite('{0:d}.jpg'.format(i),re_img)
resizelist.append(imge)
i = i + 1
resizelist_key = OrderedDict(sorted(enumerate(resizelist),key=lambda x: x[0])).keys()
for i,infle in enumerate(glob.glob(os.path.join(pth,'*.jpg'))):
img = cv2.imread(infle)
my_key = str(i)
resizedlist[my_key] = img
# Retreival code, result_list contains euclidean distance, and resultlist_key contains numbers
res_lst_srt = {'val': result_list,'key':resultlist_key}
res_lst_srt['val'], res_lst_srt['key'] = zip(*sorted(zip(res_lst_srt['val'], res_lst_srt['key'])))
cv2.imshow('query image',imge)
for key in res_lst_srt:
if key in resizedlist:
cv2.imshow("Result " + str(i + 1), resizedlist[i])
cv2.waitKey(0)
cv2.destroyAllWindows()
path contains the path for a set of images in the system. resizedlist_key contains the number starting from zero, till the n-1. Is there any way to retrieve the images from a dictionary based on it key? I have been working on this, and still i'm not getting the proper results, and i don't know whether my code is correct or not. so i'm asking your suggestion, i have a dataset of 49 images, and i need to put all images in a dictionary so that i can randomly retrieve images using its key value.
Thanks in advance!
Upvotes: 0
Views: 3714
Reputation: 11733
I don't understand very well your code and your answer, in particular I do not understand the zip
part of your code.
Anyway, I assume you want a dictionary with a number as key and an image for a value associated to a key.
I think you made some confusion on how a dict
works in python, you should study it a little bit. Google is full of nice tutorial about dicts in python. Also, try to practice a little bit with just simple number or strings before use the images from opencv, it is much more easy to understand what is happening under the hood of the so nice python data structures.
You are using 'value'
as a key, so that your dictionary contains only 1 item with the string 'value'
as a key. Ear for loop you are replacing the value associated with the string 'value'
with the last image from cv2.imread
.
The dictionary data structure has 2 properties, for each item in this type of collection you have ONE key and ONE value. using 'value'
as a key (in the []
operator) you are assuming that the key of the element has the SAME key: a string.
Try to print len(resizedlist)
and print resized list
and see what happen. Python is so good and cool at interactive coding that you can easily debug with prints.
This code is working and put all the images found in the given path (as numpy array that is the way python and opencv2 works) in a dictionary where the keys are number from 0 to n (given by enumerate
):
import glob, os
import cv2, numpy
path = './'
image_dict = dict()
for i,infile in enumerate(glob.glob(os.path.join(path,'*.jpg'))):
img = cv2.imread(infile)
my_key = i # or put here whatever you want as a key
image_dict[my_key] = img
#print image_dict
print len(image_dict)
print image_dict[0] # this is the value associated with the key 0
print image_dict.keys() # this is the list of all keys
print type(image_dict.keys()[0]) # this is <type 'int'>
print type(image_dict.values()[0]) # this is <type 'numpy.ndarray'>
To understand better how dict works in python try to use my_key = str(i)
, and see how your print-debug code changes.
I hope it helps and hope to have understood your question!!
Upvotes: 1