forked from wihoho/NearDuplicateRecognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyFramesIdentification.py
266 lines (188 loc) · 7.49 KB
/
keyFramesIdentification.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
__author__ = 'GongLi'
import os
import numpy as np
import sift
from scipy.cluster.vq import *
import utility as util
from sklearn.cluster import KMeans
import pca
from random import randint
class Graph:
def __init__(self, numOfNodes):
bagOfadj = []
for i in range(numOfNodes):
bagOfadj.append([])
self.bagOfAdj = bagOfadj
self.numOfNodes = numOfNodes
def connect(self, w, v):
if w == v:
return
if v not in self.bagOfAdj[w]:
self.bagOfAdj[w].append(v)
if w not in self.bagOfAdj[v]:
self.bagOfAdj[v].append(w)
def getAdj(self, w):
if w > self.numOfNodes - 1:
return None
return self.bagOfAdj[w]
def getNumOfEdges(self, w):
lst = self.getAdj(w)
if not lst:
return 0
return len(lst)
def connectedComponent(self):
self.marked = [False] * self.numOfNodes
connectedComponentList = []
for i in range(self.numOfNodes):
if not self.marked[i]:
lst = []
self.dfs(i, lst)
connectedComponentList.append(lst)
return connectedComponentList
def dfs(self, w, lst):
aja = self.bagOfAdj[w]
self.marked[w] = True
lst.append(w)
for potential in aja:
if not self.marked[potential]:
self.dfs(potential, lst)
class Video:
def __init__(self, videoPath):
self.videoPath = videoPath
SIFTfeatures = [] #
imageNames = [] # name of each image
# Read in video frames
for item in os.listdir(videoPath):
imagePath = videoPath +"/"+ item
locations, features = sift.siftFeature(imagePath)
SIFTfeatures.append(features)
imageNames.append(item)
# Histogramize each image
imageHistograms = []
vocabulary = util.loadObject("data/voc.pkl")
vocSize = len(vocabulary)
for imageFeature in SIFTfeatures:
imageFeature = util.normalizeSIFT(imageFeature)
histogram = self.buildHistogram(imageFeature, vocabulary)
imageHistograms.append(histogram)
imageHistograms = np.array(imageHistograms)
self.imageNames = imageNames
self.imageHistograms = imageHistograms
self.SIFTfeatures = SIFTfeatures
# Cluster frames
self.numOfFrames = len(imageNames)
self.numOfCentriods = int(self.numOfFrames / 10)
kmeans = KMeans(init="k-means++", n_clusters=self.numOfCentriods, n_init=10)
kmeans.fit(self.imageHistograms)
cluster_centroids = kmeans.cluster_centers_
# Get components of each cluter
codes, distance = vq(self.imageHistograms, cluster_centroids)
dict = {}
indice = 0
for code in codes:
keys = dict.keys()
if str(code) in keys:
dict[str(code)].append(indice)
else:
dict[str(code)] = []
dict[str(code)].append(indice)
indice += 1
# stack all SIFT features to perform PCA
stackOfSIFTfeatures = SIFTfeatures[0]
for eachFeature in SIFTfeatures[1:]:
stackOfSIFTfeatures = np.vstack((stackOfSIFTfeatures, eachFeature))
V,S, mean = pca.pca(stackOfSIFTfeatures)
self.V = V
# Perform near duplicate within each cluster
KEYFRAMES = []
keys = dict.keys()
for key in keys:
cluster = dict[key]
clusterFeatures = []
for i in cluster:
clusterFeatures.append(self.SIFTfeatures[i])
potentialKeyFrames = self.identifyKeyFrame(clusterFeatures, cluster)
KEYFRAMES += potentialKeyFrames
print str(cluster) +": "+ str(potentialKeyFrames)
self.keyFrames = KEYFRAMES
compressedHistogram = self.imageHistograms[KEYFRAMES[0]]
compressedImageName = [self.imageNames[KEYFRAMES[0]]]
for keyframe in KEYFRAMES[1:]:
compressedHistogram = np.vstack((compressedHistogram, self.imageHistograms[keyframe]))
compressedImageName.append(self.imageNames[keyframe])
self.compressedHistogram = compressedHistogram
self.compressedImageName = compressedImageName
def buildHistogram(self, imageFeature, vocabulary):
vocSize = len(vocabulary)
histogram = np.zeros(vocSize)
codes, distance = vq(imageFeature, vocabulary)
for code in codes:
histogram[code] += 1
return histogram
# SIFTFeatures is a list of SIFT feature of each image
def identifyKeyFrame(self, SIFTFeatures, indices, threshold = 0.15):
if len(indices) in [1,2]:
lst = []
lst.append(indices[0])
return lst
# build up graph structure
numberOfNodes = len(SIFTFeatures)
graph = Graph(numberOfNodes)
for i in range(numberOfNodes):
for j in range(i+1, numberOfNodes, 1):
one = SIFTFeatures[i]
two = SIFTFeatures[j]
# check whether one and two are near duplicate
pcaFeatures1 = pca.project(one, self.V, 36)
pcaFeatures2 = pca.project(two, self.V, 36)
# normalize features
util.normalize(pcaFeatures1)
util.normalize(pcaFeatures2)
np.savetxt("pcafeature1", pcaFeatures1, delimiter="\t")
np.savetxt("pcafeature2", pcaFeatures2, delimiter="\t")
# interface with Java program to do matching
rowX = pcaFeatures1.shape[0]
rowY = pcaFeatures2.shape[0]
column = pcaFeatures1.shape[1]
matchCommand = "java match " +str(rowX)+ " " +str(rowY)+ " " +str(column)
print matchCommand
os.system(matchCommand)
# plot according to match stored in "data" folder
matchFile = open("data/match", 'r')
lines = matchFile.readlines()
matchSize = len(lines)
oneSize = one.shape[0]
twoSize = two.shape[0]
ratio = matchSize / float(min(oneSize, twoSize))
if ratio > threshold:
graph.connect(i,j)
# Find nodes with largest edges in each connected component
connectedComponents = graph.connectedComponent()
tempkeyFrames = []
for component in connectedComponents:
edges = []
for node in component:
edges.append(graph.getNumOfEdges(node))
maxIndice = 0
maxValue = edges[0]
for i in range(1, len(edges), 1):
if maxValue < edges[i]:
maxValue = edges[i]
maxIndice = i
# random choose one if there are many within one component
maxEdges = []
for i in range(len(edges)):
if edges[i] == maxValue:
maxEdges.append(i)
if len(maxEdges) == 1:
tempkeyFrames.append(component[maxIndice])
else:
maxEdgeSize = len(maxEdges)
randomNumber = randint(0, maxEdgeSize - 1)
tempkeyFrames.append(component[randomNumber])
keyFrames = []
for indice in tempkeyFrames:
keyFrames.append(indices[indice])
return keyFrames
if __name__ == "__main__":
v = Video("videos/google glass")