-
Notifications
You must be signed in to change notification settings - Fork 2
/
HistogramLevelOne.py
175 lines (130 loc) · 4.62 KB
/
HistogramLevelOne.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
__author__ = 'GongLi'
import Utility as util
import numpy as np
from scipy.cluster.vq import *
import os
import time
def buildHistogramLevelOne(videoPath):
frames = os.listdir(videoPath)
boundary = len(frames) / 2
voc = util.loadObject("Data/voc.pkl")
vocSize = len(voc)
front = [np.zeros(vocSize).reshape(1, vocSize)] * 4
back = [np.zeros(vocSize).reshape(1, vocSize)] * 4
# process front
for frame in frames[:boundary]:
framePath = videoPath +"/"+ frame
frameHistograms = imageHistogramLevelOne(framePath, voc)
if not frameHistograms:
continue
assert len(frameHistograms) == 4
for i in range(4):
front[i] = np.vstack((front[i], frameHistograms[i]))
for i in range(len(front)):
front[i] = front[i][1:]
# process back
for frame in frames[boundary: ]:
framePath = videoPath +"/"+ frame
frameHistograms = imageHistogramLevelOne(framePath, voc)
if not frameHistograms:
continue
for i in range(4):
back[i] = np.vstack((back[i], frameHistograms[i]))
for i in range(len(back)):
back[i] = back[i][1:]
VideoEightHistograms = front + back
return VideoEightHistograms
def imageHistogramLevelOne(framePath, voc):
completePath = framePath
lines = open(completePath, "r").readlines()
frameFeatures = np.zeros(128).reshape(1, 128)
featureLocations = np.zeros(2).reshape(1, 2)
for line in lines[1:]:
data = line.split(" ")
feature = data[4:]
locations = data[:2]
for i in range(len(feature)):
item = int(feature[i])
feature[i] = item
for i in range(len(locations)):
item = float(locations[i])
locations[i] = item
feature = util.normalizeSIFT(feature)
frameFeatures = np.vstack((frameFeatures, feature))
featureLocations = np.vstack((featureLocations, locations))
frameFeatures = frameFeatures[1:]
if len(frameFeatures) == 0:
return None
featureLocations = featureLocations[1:]
maxValues = np.amax(featureLocations, axis=0)
rowMax = maxValues[0]
columnMax = maxValues[1]
if rowMax > 240 or columnMax > 320:
width = 640
height = 480
else:
width = 320
height = 240
# construct the histograms
vocSize = len(voc)
list1 = []
list2 = []
list3 = []
list4 = []
halfWidth = width / 2.0
halfHeight = height / 2.0
for i in range(featureLocations.shape[0]):
position = featureLocations[i]
x = position[1]
y = position[0]
if x < halfWidth and y < halfHeight:
list1.append(i)
elif x >= halfWidth and y < halfHeight:
list2.append(i)
elif x < halfWidth and y >= halfHeight:
list3.append(i)
else:
list4.append(i)
featureSectionOne = frameFeatures[list1]
featureSectionTwo = frameFeatures[list2]
featureSectionThree = frameFeatures[list3]
featureSectionFour = frameFeatures[list4]
allFourHistogram = []
histogram = np.zeros(vocSize)
codes, distance = vq(featureSectionOne, voc)
for code in codes:
histogram[code] += 1
allFourHistogram.append(histogram)
histogram = np.zeros(vocSize)
codes, distance = vq(featureSectionTwo, voc)
for code in codes:
histogram[code] += 1
allFourHistogram.append(histogram)
histogram = np.zeros(vocSize)
codes, distance = vq(featureSectionThree, voc)
for code in codes:
histogram[code] += 1
allFourHistogram.append(histogram)
histogram = np.zeros(vocSize)
codes, distance = vq(featureSectionFour, voc)
for code in codes:
histogram[code] += 1
allFourHistogram.append(histogram)
return allFourHistogram
if __name__ == "__main__":
for label in os.listdir("/Users/GongLi/Dropbox/FYP/Duan Lixin Data Set/sift_features/Kodak"):
path = "/Users/GongLi/Dropbox/FYP/Duan Lixin Data Set/sift_features/Kodak/" + label
print path
if label in ["birthday", "parade","picnic", ".DS_Store"]:
continue
for video in os.listdir(path):
if video == ".DS_Store":
continue
if label == "show" and video in ["100_0204", "100_0205", "100_0207", "100_0208"]:
continue
fileName = "KodakLevelOneHistograms/"+label+"_"+video+".pkl"
videoPath = path +"/"+video
print videoPath
eightHistograms = buildHistogramLevelOne(videoPath)
util.storeObject(fileName, eightHistograms)
print time.ctime()