-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainCodFilterSVM.py
176 lines (102 loc) · 4.48 KB
/
TrainCodFilterSVM.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
"""
Created on Fri May 12 18:48:05 2023
@author: Alfonso Blanco
"""
# -*- coding: utf-8 -*-
"""
Created on May 2023
@author: Alfonso Blanco
"""
######################################################################
# PARAMETERS
######################################################################
dirname="Training"
dirnameTest="Test"
######################################################################
import numpy as np
import cv2
import os
import re
import imutils
#####################################################################
def loadCodFilterTraining(dirname):
thresoldpath = dirname
arry=[]
print("Reading codfilters from ",thresoldpath)
Conta=0
for root, dirnames, filenames in os.walk(thresoldpath):
for filename in filenames:
if re.search("\.(txt)$", filename):
Conta=Conta+1
#arry=[]
filepath = os.path.join(root, filename)
f=open(filepath,"r")
for linea in f:
arry.append(int(linea))
f.close()
Y_train=np.array(arry)
return Y_train
#########################################################################
def loadimages (dirname ):
#########################################################################
# adapted from:
# https://www.aprendemachinelearning.com/clasificacion-de-imagenes-en-python/
# by Alfonso Blanco García
########################################################################
imgpath = dirname
images = []
imagesFlat=[]
Licenses=[]
arr=[]
Conta=0
ContFirst=0
print("Reading imagenes from ",imgpath)
NumImage=-2
for root, dirnames, filenames in os.walk(imgpath):
NumImage=NumImage+1
for filename in filenames:
if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename):
Conta=Conta+1
filepath = os.path.join(root, filename)
License=filename[:len(filename)-4]
image = cv2.imread(filepath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, (416,416), interpolation = cv2.INTER_AREA)
images.append(image)
imagesFlat.append(gray.flatten())
Licenses.append(License)
return imagesFlat, Licenses
###########################################################
# MAIN
##########################################################
Y_train=loadCodFilterTraining(dirname)
#print(Y_train)
X_train, Licenses=loadimages(dirname)
X_test, LicensesTest=loadimages(dirnameTest)
print("Number of imagenes to test : " + str(len(X_train)))
print("Number of CodFilters : " + str(len(Y_train)))
from sklearn.svm import SVC
import pickle #to save the model
from sklearn.multiclass import OneVsRestClassifier
#https://scikit-learn.org/stable/modules/generated/sklearn.multiclass.OneVsRestClassifier.html
model = OneVsRestClassifier(SVC(kernel='linear', probability=True, max_iter=1000)) #Creates model instance here
#model = OneVsRestClassifier(SVC(kernel='linear', probability=True, verbose=True, max_iter=1000)) #Creates model instance here
#model = OneVsRestClassifier(SVC(kernel='poly', degree=8)) #Creates model instance here
#model = OneVsRestClassifier(SVC(kernel='rbf'))
#model = OneVsRestClassifier(SVC(kernel='sigmoid'))
Y_train=Y_train.astype(int)
X_train=np.array(X_train)
X_train=X_train.astype(int)
model.fit(X_train, Y_train) #fits model with training data
pickle.dump(model, open("./model.pickle", 'wb')) #save model as a pickled file
model2= pickle.load( open("./model.pickle", 'rb'))
predictions=model2.predict(X_test)
#
TotHits=0
TotFailures=0
NumberImageOrder=0
for i in range (len( LicensesTest)):
NumberImageOrder=NumberImageOrder+1
CodFilter=predictions[i]
print(LicensesTest[i] + " CodFilter = "+ str(CodFilter))