-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtrain.py
More file actions
55 lines (46 loc) · 1.69 KB
/
train.py
File metadata and controls
55 lines (46 loc) · 1.69 KB
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
#############################################
# Universidad Tecnica Particular de Loja #
#############################################
# Professor: #
# Rodrigo Barba lrbarba@utpl.edu.ec #
#############################################
# Students: #
# Marcelo Bravo mdbravo4@utpl.edu.ec #
# Galo Celly gscelly@utpl.edu.ec #
# Nicholas Earley nearley@utpl.edu.ec #
#############################################
# python train.py --dataset data/digits.csv --model models/svm.cpickle
# import the necessary packages
from sklearn.svm import LinearSVC
from pyimagesearch.hog import HOG
from pyimagesearch import dataset
import argparse
import cPickle
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required = True,
help = "path to the dataset file")
ap.add_argument("-m", "--model", required = True,
help = "path to where the model will be stored")
args = vars(ap.parse_args())
# load the dataset and initialize the data matrix
(digits, target) = dataset.load_digits(args["dataset"])
data = []
# initialize the HOG descriptor
hog = HOG(orientations = 18, pixelsPerCell = (10, 10),
cellsPerBlock = (1, 1), normalize = True)
# loop over the images
for image in digits:
# deskew the image, center it
image = dataset.deskew(image, 20)
image = dataset.center_extent(image, (20, 20))
# describe the image and update the data matrix
hist = hog.describe(image)
data.append(hist)
# train the model
model = LinearSVC(random_state = 42)
model.fit(data, target)
# dump the model to file
f = open(args["model"], "w")
f.write(cPickle.dumps(model))
f.close()