-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnn.py
executable file
·172 lines (102 loc) · 3.23 KB
/
cnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from PIL import Image
import imagehash , numpy
from scipy import spatial
import os.path
import re
import sys
import tarfile
import numpy as np
from six.moves import urllib
import tensorflow as tf
def make_patches(image1 ,image2):
im1 = Image.open(image1)
im1 = im1.resize((200, 200), Image.ANTIALIAS)
im1.save(image1)
im2 = Image.open(image2)
im2 = im2.resize((200, 200), Image.ANTIALIAS)
im2.save(image2)
pix1 = numpy.asarray(im1)
pix2 = numpy.asarray(im2)
i=0
while(i+150<=200):
j=0
while(j+150<=200):
im_temp = Image.fromarray(pix1[i:i+150][j:j+150])
im_temp.save(image1[:-3]+"_"+str(i)+"_"+str(j)+".jpg")
j=j+50
i=i+50
i=0
while(i+150<=200):
j=0
while(j+150<=200):
im_temp = Image.fromarray(pix2[i:i+150][j:j+150])
im_temp.save(image2[:-3]+"_"+str(i)+"_"+str(j)+".jpg")
j=j+50
i=i+50
def similarity1(image1 , image2):
image_data1 = tf.gfile.FastGFile(image1, 'rb').read()
image_data2 = tf.gfile.FastGFile(image2 ,'rb').read()
with open('inception/classify_image_graph_def.pb', 'rb') as graph_file:
graph_def = tf.GraphDef()
graph_def.ParseFromString(graph_file.read())
tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
pool_3_tensor = sess.graph.get_tensor_by_name('pool_3:0')
feat1 = sess.run(pool_3_tensor,{'DecodeJpeg/contents:0': image_data1})
feat1 = np.squeeze(feat1)
feat2 = sess.run(pool_3_tensor,{'DecodeJpeg/contents:0': image_data2})
feat2 = np.squeeze(feat2)
return (numpy.sum(numpy.square(feat2 - feat1)))
def similarity2(image1 , image2):
hash1 = imagehash.phash(Image.open(image1))
hash2 = imagehash.phash(Image.open(image2))
diff1 = abs(hash1 - hash2)
#print (diff1)
if(diff1<=12):
return (1 - (float(diff1)/64))
return 0
#main
def eval1(image1 , image2):
make_patches(image1 , image2)
l=0
arr= []
arr.append(similarity1(image1,image2))
while(l+150<=200):
k=0
while(k+150<=200):
i=0
while(i+150<=200):
j=0
while(j+150<=200):
arr.append(similarity1(image1[:-3]+"_"+str(l)+"_"+str(k)+".jpg",image2[:-3]+"_"+str(i)+"_"+str(j)+".jpg"))
j=j+50
i=i+50
k=k+50
l=l+50
c = similarity2(image1,image2)
d = min(arr)
#a non linear ranking function
if(c==0):
if(d<200):
return (1-(float(d)/500))
else:
if(d < 400):
return (1-(float(d)/400))
else:
return 0
else:
if(c>0.95):
return(c)
elif(d < 25):
return (1-(float(d)/500))
else:
if(d<200):
return ((c+(1-(float(d)/500)))/2)
else:
if(d < 400):
return ((c+(1-(float(d)/400)))/2)
else:
return 0