-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgclassifier.py
87 lines (64 loc) · 3.05 KB
/
imgclassifier.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
import numpy as np
import re
from sklearn import svm, metrics
from skimage import io, feature, filters, exposure, color
class ImageClassifier:
def __init__(self):
self.classifer = None
def imread_convert(self, f):
return io.imread(f).astype(np.uint8)
def load_data_from_folder(self, dir):
# read all images into an image collection
ic = io.ImageCollection(dir+"*.bmp", load_func=self.imread_convert)
#create one large array of image data
data = io.concatenate_images(ic)
#extract labels from image names
labels = np.array(ic.files)
for i, f in enumerate(labels):
m = re.search("_", f)
labels[i] = f[len(dir):m.start()]
return(data,labels)
def extract_image_features(self, data):
# extract feature vector from image data
feature_data = []
for pic in data:
image = color.rgb2gray(pic)
newimg = filters.gaussian(image, sigma = 3)
ImgFeature = feature.hog(newimg, block_norm = "L2-Hys", orientations = 7, pixels_per_cell = (35,35), cells_per_block = (5,5))
feature_data.append(ImgFeature)
return(feature_data)
def train_classifier(self, train_data, train_labels):
# train model and save the trained model to self.classifier
self.classifier = svm.SVC(kernel='linear')
self.classifier.fit(train_data, train_labels)
#print(self.classifier)
def predict_labels(self, data):
# predict labels of test data using trained model in self.classifier
# the code below expects output to be stored in predicted_labels
predicted_labels = self.classifier.predict(data)
return predicted_labels
def main():
img_clf = ImageClassifier()
# load images
(train_raw, train_labels) = img_clf.load_data_from_folder('./train/')
(test_raw, test_labels) = img_clf.load_data_from_folder('./test/')
# convert images into features
train_data = img_clf.extract_image_features(train_raw)
test_data = img_clf.extract_image_features(test_raw)
# train model and test on training data
img_clf.train_classifier(train_data, train_labels)
predicted_labels = img_clf.predict_labels(train_data)
print("\nTraining results")
print("=============================")
print("Confusion Matrix:\n",metrics.confusion_matrix(train_labels, predicted_labels))
print("Accuracy: ", metrics.accuracy_score(train_labels, predicted_labels))
print("F1 score: ", metrics.f1_score(train_labels, predicted_labels, average='micro'))
# test model
predicted_labels = img_clf.predict_labels(test_data)
print("\nTest results")
print("=============================")
print("Confusion Matrix:\n",metrics.confusion_matrix(test_labels, predicted_labels))
print("Accuracy: ", metrics.accuracy_score(test_labels, predicted_labels))
print("F1 score: ", metrics.f1_score(test_labels, predicted_labels, average='micro'))
if __name__ == "__main__":
main()