This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_stage.py
78 lines (62 loc) · 2.05 KB
/
training_stage.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
#!/usr/bin/python
import string
import cv2
import numpy
import math
import glob
from classes.preprocessing import Preprocessing
from classes.tools import Tools
from classes.database import Database
from classes.histogram import Histogram
from classes.featureExtraction import FeatureExtraction
from classes.sternmuster import Sternmuster
from classes.pca import PCA
tools = Tools()
database = Database()
database.initializeEmpty()
char_values = string.ascii_uppercase + string.ascii_lowercase # Expected characters in Trainings Set
splitted_t_set = []
for t_set in glob.glob('./trainingdata/*.png'):
print("Reading image: " + t_set)
img = cv2.imread(t_set, cv2.IMREAD_GRAYSCALE)
preprocess = Preprocessing(img)
preprocess.binariseImg()
splitted_chars = preprocess.splitChars()
splitted_t_set.append(splitted_chars)
# Histogram
for i in range(len(char_values)):
for font in splitted_t_set:
histogram = Histogram(font[i])
database.add(char_values[i], 'histogram', histogram.rowWert2wert())
# Pixel Average
for i in range(len(char_values)):
for font in splitted_t_set:
pix_av = FeatureExtraction(font[i])
database.add(char_values[i], 'pixelAv', pix_av.getpixelaverage())
# PCA
pca = PCA()
for i in range(len(char_values)):
temp = []
for j in splitted_t_set:
temp.append(j[i])
pca.trainChar(char_values[i], temp)
mean_vector = numpy.mean(pca.matrix, 0)
database.add('common', 'pca_mean', list(mean_vector))
pca.matrix -= mean_vector
# Merkmale für alle Buchstaben in Reihenfolge wie in char_values
eigenvector = pca.generate_eigenvector()
database_eig = []
for i in eigenvector:
temp = []
for j in i:
temp.append(float(j))
database_eig.append(temp)
database.add('common', 'pca_eig', database_eig)
pca_merkmale = pca.pca(eigenvector)
for i in range(len(pca_merkmale.T)):
temp = list()
for j in list(pca_merkmale.T[i]):
temp.append(float(j))
database.add(char_values[math.floor(i / len(splitted_t_set))], 'pca', temp)
# Datenbank speichern
database.saveDatabase()