-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
46 lines (36 loc) · 1.38 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
from sklearn import datasets, svm, metrics
import os
import cv2
def classifier(upload):
dict_label = {'normal_img': 0,
'white_background_img': 1,
'skin_background_img': 2,
'pattern_background_img': 3,
'curve_background_img': 4}
X = []
y = []
for folder in os.listdir('image_classified'):
for img_path in os.listdir('image_classified/' + folder):
img = cv2.imread('image_classified/' + folder + '/' + img_path)
img = cv2.resize(img, (128, 128))
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# resize img to (128, 128)
img = img/255
# print(img.shape)
# flatten img to 1D array
img = img.flatten()
X.append(img)
y.append(dict_label[folder])
n_samples = len(y)
clf = svm.SVC(gamma=0.001, C=100)
clf.fit(X, y)
# test_img = cv2.imread(img_path)
test_img = cv2.resize(img_array, (128, 128))
# test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
# resize test_img to (128, 128)
test_img = test_img/255
# print(test_img.shape)
# flatten test_img to 1D array
test_img = test_img.flatten()
predict = clf.predict([test_img])
return predict[0]