-
Notifications
You must be signed in to change notification settings - Fork 1
/
texture_classifier.py
116 lines (98 loc) · 4.07 KB
/
texture_classifier.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 13:26:31 2020
@author: steve
"""
import cv2
import numpy as np
import os
import glob
import mahotas as mt
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from segment.LocalBinaryPatterns import LocalBinaryPatterns
from sklearn.ensemble import AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
import pickle
# function to extract haralick textures from an image
def extract_features(image):
# calculate haralick texture features for 4 types of adjacency
textures = mt.features.haralick(image)
# take the mean of it and return it
ht_mean = textures.mean(axis=0)
return ht_mean
# load the training dataset
train_path = "textureDataset/train"
train_names = os.listdir(train_path)
# empty list to hold feature vectors and train labels
train_features = []
train_labels = []
# loop over the training dataset
print ("[STATUS] Started extracting haralick textures..")
for train_name in train_names:
cur_path = train_path + "/" + train_name
cur_label = train_name
i = 1
for file in glob.glob(cur_path + "/*.jpg"):
print ("Processing Image - {} in {}".format(i, cur_label))
# read the training image
image = cv2.imread(file)
imagehsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
colorbg = cv2.calcHist([imagehsv], [0, 1], None, [8,10], [0, 256, 0, 256])
colorbg = cv2.normalize(colorbg, colorbg).flatten()
# extract haralick texture from the image
desclbp = LocalBinaryPatterns(100, 3) # (The number of points p in a circularly symmetric neighborhood to consider, radius of the circle)
lbpfg = desclbp.describe(imagehsv[:, :, 2])
features = extract_features(gray)
fv = np.concatenate((colorbg,lbpfg),axis=None)
#print((fv))
# append the feature vector and label
train_features.append(fv)
train_labels.append(cur_label)
# show loop update
i += 1
# have a look at the size of our feature vector and labels
print ("Training features: {}".format(np.array(train_features).shape))
print ("Training labels: {}".format(np.array(train_labels).shape))
# create the classifier
print ("[STATUS] Creating the classifier..")
#clf_svm = SVC(kernel='linear',probability=True,random_state=9)
#clf_svm = RandomForestClassifier()
clf_svm = RandomForestClassifier()
# fit the training data and labels
print ("[STATUS] Fitting data/label to model..")
clf_svm.fit(train_features, train_labels)
# save the model to disk
filename = 'rf_pascal_pattern.sav'
pickle.dump(clf_svm, open(filename, 'wb'))
# loop over the test images
test_path = "textureDataset/test"
for file in glob.glob(test_path + "/*.jpg"):
# read the input image
image = cv2.imread(file)
imagehsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
colorbg = cv2.calcHist([imagehsv], [0, 1], None, [8,10], [0, 256, 0, 256])
colorbg = cv2.normalize(colorbg, colorbg).flatten()
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# extract haralick texture from the image
features = extract_features(gray)
lbpfg = desclbp.describe(imagehsv[:, :, 2])
fv = np.concatenate((colorbg,lbpfg),axis=None)
# evaluate the model and predict label
prediction = clf_svm.predict(fv.reshape(1, -1))[0]
#class_probabilities = clf_svm.predict_proba(lbpfg.reshape(1, -1))[0]
#print(class_probabilities)
# show the label
cv2.putText(image, prediction, (20,30), cv2.FONT_HERSHEY_SIMPLEX,0.5, (0,255,255), 1)
print ("Prediction - {}".format(prediction))
cv2.imwrite(os.getcwd() + "/segmentresult/fg.jpg",image)
# display the output image
cv2.imshow("Test_Image", image)
cv2.waitKey(0)