1

I have extracted images and have constructed two dictionaries that has file path as the key and list containing image digest data as a list.

Just to give an example lets say we have dictionary a = {}, which eventually looks like {'fileName' : [ 'x=1,2,3','y=1,2,3','z=1,2,3'... ]}

Here is the how my actual dictionary looks like:

{'/Users/cannon/AmazonBasics-Wireless-Remote-Control-for-Nikon-P700030004040x50500051006070700070s80-and90igital-SLR-Cameras.jpg': [
'fcolor=2,3,0,80,125,15,5,0,0,0,0,0,0,0,0,0,0,0,0,0,2,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0', 'edge=1,224,3,98,48,4,66,72,70,32,4,34,16,14,161,9,68,128,7', 'texture=1,173,20,9,5,3,3,4,34'],

'/Users/cannon/Sony-50mm-f-1.4-Lens-for-Sony-Alphaigital-SLR-Camera.jpg': [
'fcolor=2,111,1,18,100,1,0,0,0,0,0,0,0,0,0,1,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0', 'edge=1,252,35,64,99,236,127,0,17,0,1,16,139,241,24,128,2,132,31', 'texture=1,92,52,25,17,14,9,7,34']}

I have written a function that accepts two dictionaries like I have mentioned above and for each key in one dictionary iterate over for each key in the second dictionary and calculate the cosine values.

The dictionary has three type of values dumped into a list, fcolor, edge, and texture. My function calculates the dot product for each value in the list for each key in both dictionaries.

Question: What I want to do is for each key Ka dictionary A I want to plot all the values for Dictionary B, Kb.

Here is my current code:

def cosignSimilarity(aImageVectDict, eImageVectDict):
    #cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
    fo = open('/Users/AbovePointNine.txt','w')
    f1 = open('/Users/AbovePoint8.txt','w')
    pltfColor = []
    pltEdgeColor = []
    pltTexture = []
    #converting the string of vectors into numpy arrays and calculate cosine similarity
    for (fn1, lst1), (fn2, lst2) in product(aImageVectDict.iteritems(), eImageVectDict.iteritems()):
        print fn1
        print fn2
        fcolorCosine = None
        edgeColorCosine = None
        textureColorCosine = None
        for vector1, vector2 in zip(lst1, lst2):
            cx = None
            cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)
            v1fColor = vector1.split('=')[1]
            debug1 = v1fColor
            v2fColor = vector2.split('=')[1]
            debug2 = v2fColor
            if (vector1.split('=')[0] == 'fcolor' and vector2.split('=')[0] == 'fcolor'):
                v1fColor =  np.array(map(int,v1fColor.split(',')))
                v2fColor =  np.array(map(int,v2fColor.split(',')))
                fcolorCosine = cx(v1fColor, v2fColor)
                print fcolorCosine
                pltfColor.append(fcolorCosine)
            v1Edge = vector1.split('=')[1]
            v2Edge = vector2.split('=')[1]
            if (vector1.split('=')[0] == 'edge' and vector2.split('=')[0] == 'edge'):
                v1Edge = np.array(map(int,v1Edge.split(',')))
                v2Edge = np.array(map(int,v2Edge.split(',')))
                edgeColorCosine = cx(v1Edge, v2Edge)
                print edgeColorCosine
                pltEdgeColor.append(edgeColorCosine)
            v1Texture = vector1.split('=')[1]
            v2Texture = vector2.split('=')[1]
            if (vector1.split('=')[0] == 'texture' and vector2.split('=')[0] == 'texture'):
                v1Texture = np.array(map(int,v1Texture.split(',')))
                v2Texture =  np.array(map(int,v2Texture.split(',')))
                textureColorCosine = cx(v1Texture, v2Texture)
                print textureColorCosine
                pltTexture.append(textureColorCosine)
            if (fcolorCosine > 0.9 and edgeColorCosine > 0.9 and textureColorCosine > 0.9):
                fo.write(fn1)
                fo.write('\n')
                fo.write(fn2)
                fo.write('\n')
                fo.write(str(fcolorCosine))
                fo.write('\n')
                fo.write(str(edgeColorCosine))
                fo.write('\n')
                fo.write(str(textureColorCosine))
            if (fcolorCosine > 0.8 and edgeColorCosine > 0.8 and textureColorCosine > 0.8):
                f1.write(fn1)
                f1.write('\n')
                f1.write(fn2)
                f1.write('\n')
                f1.write(str(fcolorCosine))
                f1.write('\n')
                f1.write(str(edgeColorCosine))
                f1.write('\n')
                f1.write(str(textureColorCosine))
    fo.close()
    f1.close()

    #Plotting 
    plt.plot(pltfColor, 'r.')
    #plt.plot(pltEdgeColor, 'b.')
    #plt.plot(pltTexture, 'g.')
    plt.ylabel('cosine values')
    #plt.yscale('log')
    plt.show()
    fo.close()

Once again just to confirm what I really want is for each key in Dictionary A I want to find out all the results in dictionary B

4

0 回答 0