-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGaborFeatureExtraction.py
101 lines (82 loc) · 2.99 KB
/
GaborFeatureExtraction.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
import cv2
import numpy as np
import shutil
import os
from sklearn.cluster import KMeans
from collections import Counter
import csv
# define gabor filter bank with different orientations and at different scales
def build_filters():
gaborfilters = []
ksize = 9
# define the range for theta and nu
for theta in np.arange(0, np.pi, np.pi / 8):
for nu in np.arange(0, 6 * np.pi / 4, np.pi / 4):
kern = cv2.getGaborKernel((ksize, ksize), 1.0, theta, nu, 0.5, 0, ktype=cv2.CV_32F)
kern /= 1.5 * kern.sum()
gaborfilters.append(kern)
# print(gaborfilters)
# print(len(gaborfilters))
return gaborfilters
# function to convolve the image with the filters
def process(img, filters):
accum = np.zeros_like(img)
for kern in filters:
fimg = cv2.filter2D(img, cv2.CV_8UC3, kern)
np.maximum(accum, fimg, accum)
return accum
if __name__ == '__main__':
# instantiating the filters
filters = build_filters()
f = np.asarray(filters)
allKeyFramesFeat = []
numOfKeyFrames = len([f for f in os.listdir("/home/aman/Desktop/Mini-Project/KeyFrames/")
if os.path.isfile(os.path.join("/home/aman/Desktop/Mini-Project/KeyFrames/", f))])
# print(numOfKeyFrames)
counter = 0
while counter < numOfKeyFrames:
print("Processing for KeyFrame{0}".format(counter))
# reading the input image
imgg = cv2.imread("/home/aman/Desktop/Mini-Project/KeyFrames/frame{0}.jpg".format(counter), 0)
# initializing the feature vector
# feat matrix is the feature vector for the image
feat = []
# calculating the local energy for each convolved image
for j in range(40):
res = process(imgg, f[j])
temp = 0
for p in range(128):
for q in range(128):
temp = temp + res[p][q] * res[p][q]
feat.append(temp)
# calculating the mean amplitude for each convolved image
for j in range(40):
res = process(imgg, f[j])
temp = 0
for p in range(128):
for q in range(128):
temp = temp + abs(res[p][q])
feat.append(temp)
print(feat)
# print(len(feat))
allKeyFramesFeat.append(feat)
counter += 1
print(len(allKeyFramesFeat))
shutil.rmtree("KeyFrames")
# Clustering
kmeans = KMeans(n_clusters=5, random_state=0).fit(allKeyFramesFeat)
print(kmeans.labels_)
vectorForVideo = []
for eachCentroid in kmeans.cluster_centers_:
vectorForVideo.extend(eachCentroid)
counter = Counter(kmeans.labels_)
sorted(counter.items())
print(counter)
for i in range(5):
vectorForVideo.append(counter[i])
print(vectorForVideo)
print(len(vectorForVideo))
# Making feature_vector.csv
with open("feature_vector.csv", 'a') as outfile:
writer = csv.writer(outfile, delimiter=' ')
writer.writerow(vectorForVideo)