how to store set of images in a dictionary, and retrieve it using python opencv -
i have dictionary, have taken images value , indexes key, have stored using zip function, , when trying retrieve it, not displaying images. have done is:
pth = 'd:\6th sem\major project\code' resizedlist = dict() 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) = + 1 resizelist_key = ordereddict(sorted(enumerate(resizelist),key=lambda x: x[0])).keys() 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, , 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) key in res_lst_srt: if key in resizedlist: cv2.imshow("result " + str(i + 1), resizedlist[i]) cv2.waitkey(0) cv2.destroyallwindows()
path contains path set of images in system. resizedlist_key contains number starting zero, till n-1. there way retrieve images dictionary based on key? have been working on this, , still i'm not getting proper results, , don't know whether code correct or not. i'm asking suggestion, have dataset of 49 images, , need put images in dictionary can randomly retrieve images using key value.
thanks in advance!
i don't understand code , answer, in particular not understand zip
part of code.
anyway, assume want dictionary number key , image value associated key.
i think made confusion on how dict
works in python, should study little bit. google full of nice tutorial dicts in python. also, try practice little bit simple number or strings before use images opencv, more easy understand happening under hood of nice python data structures.
you using 'value'
key, dictionary contains 1 item string 'value'
key. ear loop replacing value associated string 'value'
last image cv2.imread
.
the dictionary data structure has 2 properties, each item in type of collection have 1 key , 1 value. using 'value'
key (in []
operator) assuming key of element has same key: string.
try print len(resizedlist)
, print resized list
, see happen. python , cool @ interactive coding can debug prints.
this code working , put images found in given path (as numpy array way python , opencv2 works) in dictionary keys number 0 n (given enumerate
):
import glob, os import cv2, numpy path = './' image_dict = dict() i,infile in enumerate(glob.glob(os.path.join(path,'*.jpg'))): img = cv2.imread(infile) my_key = # or put here whatever want key image_dict[my_key] = img #print image_dict print len(image_dict) print image_dict[0] # value associated key 0 print image_dict.keys() # list of keys print type(image_dict.keys()[0]) # <type 'int'> print type(image_dict.values()[0]) # <type 'numpy.ndarray'>
to understand better how dict works in python try use my_key = str(i)
, , see how print-debug code changes.
i hope helps , hope have understood question!!
Comments
Post a Comment