-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path2_computeBoWFeatures.py
More file actions
executable file
·118 lines (93 loc) · 3.46 KB
/
2_computeBoWFeatures.py
File metadata and controls
executable file
·118 lines (93 loc) · 3.46 KB
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
import pandas as pd
import numpy as np
import cv2
from scipy.cluster.vq import *
import os
dictionarySize = 512
descriptorType = "SIFT"
print("Computing dictionarySize: {} descriptorType: {}"
.format(dictionarySize, descriptorType))
def computeFeatures(gray, descriptorType):
detector = cv2.xfeatures2d.SIFT_create(100)
(kps, descs) = detector.detectAndCompute(gray, None)
if descs is None:
return (None, None)
return (kps, descs.astype("float"))
i = 0
print("Generate dictionary ..")
dictionaryList = []
i = 0
step = 5
folderImages = 'db_faces/{}/'
currentFolder = folderImages.format('train')
folders = ['0', '1']
for folder in folders:
print('Processing folder ' + folder)
images = [os.path.join(currentFolder, folder, file) for file in os.listdir(currentFolder + folder)]
i = 0
for path in images:
if i % step == 0:
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(kps, descs) = computeFeatures(gray, descriptorType)
if len(dictionaryList) == 0:
dictionaryList = descs
dictionaryList = np.vstack((dictionaryList, descs))
i += 1
print("Perform KMEANS clustering ..")
dictionary, variance = kmeans(dictionaryList, dictionarySize, 1)
i = 0
print("Training db ..")
xTrain = []
yTrain = []
currentFolder = folderImages.format('train')
print('Compute train features ')
folders = ['0', '1']
for folder in folders:
images = [os.path.join(currentFolder, folder, file) for file in os.listdir(currentFolder + folder)]
for path in images:
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(kps, descs) = computeFeatures(gray, descriptorType)
if descs is not None and kps is not None:
words, distance = vq(descs, dictionary)
feature = np.zeros(dictionary.shape[0], "int32")
for w in words:
feature[w] += 1
hist = feature / sum(feature)
xTrain.append(hist)
yTrain.append(int(folder))
if i % 100 == 0:
print(i)
i += 1
xTest = []
yTest = []
i = 0
currentFolder = folderImages.format('test')
print('Compute test features ')
for folder in folders:
images = [os.path.join(currentFolder, folder, file) for file in os.listdir(currentFolder + folder)]
for path in images:
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(kps, descs) = computeFeatures(gray, descriptorType)
if descs is not None and kps is not None:
words, distance = vq(descs, dictionary)
feature = np.zeros(dictionary.shape[0], "int32")
for w in words:
feature[w] += 1
hist = feature / sum(feature)
xTest.append(hist)
yTest.append(int(folder))
if i % 100 == 0:
print(i)
i += 1
featureName = "{}_{}".format(dictionarySize, descriptorType)
df = pd.DataFrame(xTrain)
df.to_csv("features/xTrain_{}.csv".format(featureName), index=False)
df = pd.DataFrame(xTest)
df.to_csv("features/xTest_{}.csv".format(featureName), index=False)
df = pd.DataFrame(yTrain)
df.to_csv("features/yTrain_{}.csv".format(featureName), index=False)
df = pd.DataFrame(yTest)
df.to_csv("../features/yTest_{}.csv".format(featureName), index=False)