-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
101 lines (81 loc) · 2.47 KB
/
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
from imutils import paths
from commonfunctions import *
import numpy as np
import argparse
import imutils
import cv2
import os
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from joblib import dump, load
from scipy.signal import find_peaks
path = 'eighthall'
imagePaths_eighth = list(paths.list_images(path))
path = 'quarterall'
imagePaths_quarter = list(paths.list_images(path))
def binarize(img):
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = img//255
return img
def get_feature1(img):
return img.shape[0]/img.shape[1]
def get_feature2(img):
#plt.show()
#bar(np.arange(img.shape[1]), np.sum(img,axis=0))
peaks, _ = find_peaks(np.sum(img,axis=0),height=40,distance=7)
return len(peaks)
def get_feature3(img):
#plt.show()
#bar(np.arange(img.shape[1]), np.sum(img,axis=0))
return np.sum(img)
def get_feature4(img):
sizeAfterResize = (25,25)
image = cv2.resize(img, sizeAfterResize)
return image.flatten()
def get_feature5(img):
return cv2.HuMoments(cv2.moments(img)).flatten()
def get_all_features(img):
features = []
#features.append(get_feature1(img))
#features.append(get_feature2(img))
#features.append(get_feature3(img))
features.extend(get_feature5(img))
return features
def predict(clf,img):
features = get_all_features(img)
features = np.asarray(features)
features = features.reshape((1, len(features)))
return clf.predict(features)[0]
'''
training_data = []
labels = []
for (i, imagePath) in enumerate(imagePaths_eighth):
img = cv2.imread(imagePath)
img = binarize(img)
features = get_all_features(img)
features = np.asarray(features)
training_data.append(features)
#print(features)
labels.append(0)
for (i, imagePath) in enumerate(imagePaths_quarter):
img = cv2.imread(imagePath)
img = binarize(img)
features = get_all_features(img)
features = np.asarray(features)
training_data.append(features)
print(features)
labels.append(1)
training_data = np.asarray(training_data)
labels = np.asarray(labels)
img = cv2.imread("test/57.jpg")
img = binarize(img)
features_test = []
features_test = get_all_features(img)
features_test = np.asarray(features_test)
# print(features_test)
model = KNeighborsClassifier(n_neighbors=3)
model.fit(training_data, labels)
features_test = features_test.reshape((1, training_data.shape[1]))
print(model.predict(features_test))
dump(model, 'model.joblib')
'''