-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
40 lines (31 loc) · 980 Bytes
/
train.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
"""
openImage -> open image with given file name
train -> train a SVC model with the pictures under the directory specified in `config.ini`
loadModel -> load the pickle file
"""
import numpy as np
from sklearn import svm
import os, cv2, pickle, configparser
config = configparser.ConfigParser()
config.read('config.ini')
def openImage(filename):
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.ravel()
return img
def train(saveModel, modelName=''):
X = []
y = []
for i in range(3, 10):
for filename in os.listdir(f"{config['PATH']['TRAINING']}/{i}"):
img = openImage(f"{config['PATH']['TRAINING']}/{i}/{filename}")
X.append(img)
y.append(i)
model = svm.SVC()
model.fit(X, y)
if saveModel:
pickle.dump(model, open(modelName, 'wb'))
return model
def loadModel(modelName):
model = pickle.load(open(modelName, 'rb'))
return model