-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr_net.py
116 lines (94 loc) · 3.47 KB
/
ocr_net.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
from __future__ import print_function
#import potrebnih biblioteka
%matplotlib inline
import cv2
import numpy as np
import matplotlib.pyplot as plt
import collections
# keras
from keras.models import Sequential
from keras.layers.core import Dense,Activation
from keras.optimizers import SGD
from keras.models import load_model
import glob
import matplotlib.pylab as pylab
pylab.rcParams['figure.figsize'] = 16, 12 # za prikaz većih slika i plotova, zakomentarisati ako nije potrebno# -*- coding: utf-8 -*-
def load_image(path):
return cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
def image_gray(image):
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
def image_bin(image_gs):
height, width = image_gs.shape[0:2]
image_binary = np.ndarray((height, width), dtype=np.uint8)
ret,image_bin = cv2.threshold(image_gs, 127, 255, cv2.THRESH_BINARY)
return image_bin
def invert(image):
return 255-image
def display_image(image, color= False):
if color:
plt.imshow(image)
else:
plt.imshow(image, 'gray')
def dilate(image):
kernel = np.ones((3,3)) # strukturni element 3x3 blok
return cv2.dilate(image, kernel, iterations=1)
def erode(image):
kernel = np.ones((3,3)) # strukturni element 3x3 blok
return cv2.erode(image, kernel, iterations=1)
def resize_region(region):
return cv2.resize(region,(28,28), interpolation = cv2.INTER_NEAREST)
def scale_to_range(image): # skalira elemente slike na opseg od 0 do 1
return image/255
def matrix_to_vector(image):
return image.flatten()
def prepare_for_ann(regions):
ready_for_ann = []
for region in regions:
scale = scale_to_range(region)
ready_for_ann.append(matrix_to_vector(scale))
return ready_for_ann
def convert_output(alphabet):
nn_outputs = []
for index in range(len(alphabet)):
output = np.zeros(len(alphabet))
output[index] = 1
nn_outputs.append(output)
return np.array(nn_outputs)
def create_ann():
ann = Sequential()
ann.add(Dense(128, input_dim=784, activation='sigmoid'))
ann.add(Dense(43, activation='sigmoid'))
return ann
def train_ann(ann, X_train, y_train):
X_train = np.array(X_train, np.float32) # dati ulazi
y_train = np.array(y_train, np.float32) # zeljeni izlazi za date ulaze
# definisanje parametra algoritma za obucavanje
sgd = SGD(lr=0.01, momentum=0.9)
ann.compile(loss='mean_squared_error', optimizer=sgd)
# obucavanje neuronske mreze
ann.fit(X_train, y_train, epochs=2000, batch_size=1, verbose = 0, shuffle=False)
return ann
def winner(output): # output je vektor sa izlaza neuronske mreze
return max(enumerate(output), key=lambda x: x[1])[0]
def display_result(outputs, alphabet):
result = []
recenica=""
for output in outputs:
#result.append(alphabet[winner(output)])
recenica+=(alphabet[winner(output)])
return recenica
numbers = []
for i in glob.glob("slova1/*.png"):
az= load_image(i)
img = invert(image_bin(image_gray(az)))
numbers.append(img)
alphabet = [0,1,2,3,4,5,6,7,8,9,'A','B','C','Č','Ć','D','Đ','E','F','G','H','I','J','K','/','L','M','N','O','P','R',' ','S','Š','T','U','V','W','X','-','Y','Z','Ž']
inputs = prepare_for_ann(numbers)
outputs = convert_output(alphabet)
ann = create_ann()
ann = train_ann(ann, inputs, outputs)
result = ann.predict(np.array(inputs[12:15], np.float32))
print(result)
print(display_result(result, alphabet))
#save model
ann.save('my_model.h5')