-
Notifications
You must be signed in to change notification settings - Fork 1
/
server2.py
199 lines (169 loc) · 4.93 KB
/
server2.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'''
Simple socket server using threads
'''
import socket
import sys
from mlabwrap import mlab
import pickle
from decaf.scripts.imagenet import DecafNet
import numpy,scipy,PIL,csv
from PIL import Image
from sklearn.decomposition import PCA
#import thread
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8080 # Arbitrary non-privileged port
cnt = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print('Socket bind complete')
#Start listening on socket
s.listen(10)
print('Socket now listening')
#################################
ucfloc = 'decaf/ucfaction/'
imgnetPath = 'decaf/imagenet_pretrained/'
numFrames = 2
#load Imagenet training dataset
net = DecafNet(imgnetPath+'imagenet.decafnet.epoch90', imgnetPath+'imagenet.decafnet.meta')
def loadPCA():
with open('pcaData.pkl', 'rb') as input:
pca = pickle.load(input)
return pca
def loadFlowPCA():
with open('pcaFlowData.pkl', 'rb') as input:
pca = pickle.load(input)
return pca
def imToNumpy(img):
return numpy.asarray(PIL.Image.open(img))
def getFeature(img):
scores = net.classify(img, center_only=True)
feature = net.feature('fc6_cudanet_out')
return feature
def listtoFile(lst,fname):
with open(fname,'wb') as m:
writer = csv.writer(m)
writer.writerows(lst)
def checkImage(querryImage):
pca = loadPCA()
sketch = pca.transform(getFeature(imToNumpy(querryImage)))
listtoFile(sketch,'querry_tmp.csv')
result = mlab.LmnnQuerry()
dic = {}
l1 =[]
l2 =[]
for x in result.vid:
l1.append(int(x[0]))
for y in result.dist:
l2.append(y[0])
mx = max(l2)
for x in range(len(l2)):
l2[x] = mx - l2[x]
# TODO : video query - 300 , flow qwery - 100
for i in range(len(result.vid)):
dic[i] = 0
for i in range(len(result.vid)):
dic[l1[i]] = l2[i]
dic2 = {}
c = 0
while c<100:
dic2[c] = (dic[3*c]+dic[3*c+1]+dic[3*c+2])/3
c += 1
return dic2
#for x in range(len(result.vid)):
# dic[int(result.vid[x][0])] = result.dist[x][0]
#print int(result[0][0])
#return int(result[0][0])
def checkFlow(querryImage):
pca = loadFlowPCA()
sketch = pca.transform(getFeature(imToNumpy(querryImage)))
listtoFile(sketch,'fquerry_tmp.csv')
result = mlab.LmnnFlowQuerry()
#result = mlab.LmnnQuerry()
dic = {}
l1 =[]
l2 =[]
for x in result.vid:
l1.append(int(x[0]))
for y in result.dist:
l2.append(y[0])
mx = max(l2)
for x in range(len(l2)):
l2[x] = mx - l2[x]
for i in range(len(result.vid)):
dic[i] = 0
for i in range(len(result.vid)):
dic[l1[i]] = l2[i]
return dic
#print int(result[0][0])
#return int(result[0][0])
def getScore(im1,im2):
alpha = 0.3
top = 5
d1 = checkImage(im1)
d2 = checkFlow(im2)
score = []
print len(d1),len(d2)
for x in d1:
print x,d1[x],d2[x]
s = (alpha * d1[x] + (1 - alpha) * d2[x] , x)
score.append(s)
score.sort()
videos = ""
for x in range(top):
videos += str(score[x][1])+":"
return videos
#################################
result = 0
mylock = False
#Function for handling connections. This will be used to create threads
def clientthread(conn,cnt):
global result,mylock
mylock = False
#infinite loop so that function do not terminate and thread do not end.
fname = "img" + str(cnt) + ".jpg"
f = open(fname,'wb')
while True:
#Receiving from client
data = conn.recv(1024)
f.write(data)
if not data:
break
f.close();
#result = checkImage(fname)
mylock = True
#came out of loop
conn.close()
#Function for handling connections. This will be used to create threads
def clientthread2(conn,cnt):
# global result,mylock
print "clientthread2"
fname = "img" + str(cnt) + ".jpg"
fname1 = "img" + str(cnt+1) + ".jpg"
#result = checkImage(fname)
#fresult = checkFlow(fname1)
result = getScore(fname,fname1)
data = conn.recv(1024)
print data,result
if data == "send val":
print result
conn.send(result+"\n")#str(result)+" "+str(fresult)+"\n")
#conn.send("25:28:15:80:89\n")
print "result sent"
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
cnt += 1
print('Connected with ' + addr[0] + ':' + str(addr[1]))
if cnt%3 == 0 :
start_new_thread(clientthread2 ,(conn,cnt-2))
else:
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,cnt,))
s.close()