-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInterface.asv
243 lines (179 loc) · 9.23 KB
/
UserInterface.asv
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function UserInterface
categories = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'add', 'sub', 'mul', 'div'};
defaultModel = "model_815_96_83_MIX.mat";
% Criar a interface gráfica
fig = uifigure('Name', 'Simple Calculator', 'Position', [500 500 500 400]);
% Carregar a rede treinada
trainedNet = load('models/' + defaultModel);
net = trainedNet.net;
% Adicionar botões de rádio para selecionar categories
categoryButtonGroup = uibuttongroup(fig, 'Position', [10 120 130 130], 'Title', 'Categories', 'SelectionChangedFcn', @changeCategories);
uiradiobutton(categoryButtonGroup, 'Text', 'All', 'Position', [10 70 80 20], 'UserData', {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'add', 'sub', 'mul', 'div'});
uiradiobutton(categoryButtonGroup, 'Text', 'Numbers', 'Position', [10 40 80 20], 'UserData', {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
uiradiobutton(categoryButtonGroup, 'Text', 'Operations', 'Position', [10 10 80 20], 'UserData', {'add', 'sub', 'mul', 'div'});
% Adicionar texto para mostrar o modelo de rede atual
modelLabel = uilabel(fig, 'Text', 'Current model: ' + defaultModel , 'Position', [10 345 300 20]);
categoriesLabel = uilabel(fig, 'Text', ['Current categories: ' + string(strjoin(categories,','))], 'Position', [10 365 300 20]);
% Adicionar botão para carregar outro modelo
loadModelButton = uibutton(fig, 'push', 'Text', 'Load Model', ...
'Position', [10 300 100 30], 'ButtonPushedFcn', @loadModel);
% Adicionar botão para selecionar imagem
selectImageButton = uibutton(fig, 'push', 'Text', 'Select Image', ...
'Position', [10 260 100 30], 'ButtonPushedFcn', @selectImage);
% Adicionar área de visualização de imagem
imgAx = uiaxes(fig, 'Position', [150 135 200 200]);
% Adicionar botão para identificar números e símbolos na imagem
identifyButton = uibutton(fig, 'push', 'Text', 'Identify', ...
'Position', [390 160 100 30], 'ButtonPushedFcn', @identifyExpression);
% Adicionar botão para desenhar uma imagem
drawButton = uibutton(fig, 'push', 'Text', 'Draw Image', ...
'Position', [390 240 100 30], 'ButtonPushedFcn', @drawImage);
verifyExpressionButton = uibutton(fig, 'push', 'Text', 'Verify Expression', ...
'Position', [390 120 100 30], 'ButtonPushedFcn', @verifyExpression);
% Adicionar área de texto para exibir resultado
resultText = uitextarea(fig, 'Position', [10 50 350 50], 'Editable', 'off');
img = []; % Imagem atual
% Função para carregar outro modelo
function loadModel(src, event)
[file, path] = uigetfile('*.mat', 'Select a model');
if file ~= 0
trainedNet = load(fullfile(path, file));
net = trainedNet.net;
modelLabel.Text = sprintf('Current model: %s', file);
end
end
% Função para selecionar imagem
function selectImage(src, event)
[file, path] = uigetfile({'*.png;*.jpg;*.jpeg;*.bmp', 'Image Files'}, 'Select an image');
if file ~= 0
img = imread(fullfile(path, file));
imshow(img, 'Parent', imgAx);
end
end
function verifyExpression(src, event)
if isempty(img)
resultText.Value = 'No image selected or drawn.';
else
% converte a imagem para grayscale caso nao seja
if ndims(img) == 3
img = rgb2gray(img);
end
% converter imagem para uma matrix binaria
binaryImg = imbinarize(img);
% procurar por colunas inteiramente brancas (1)
whiteColumns = all(binaryImg == 1, 1);
% encontrar os indices onde ocorrem as transições de colunas brancas para colunas não brancas
transitions = diff([true, whiteColumns, true]);
% encontrar o inicio e fim de cada segmento (símbolo ou número)
beginSegments = find(transitions == -1);
endSegments = find(transitions == 1) - 1;
% remover os segmentos que contêm apenas espaços brancos
minSegmentWidth = 1; % ajuste este valor conforme necessário
validSegments = (endSegments - beginSegments) >= minSegmentWidth;
beginSegments = beginSegments(validSegments);
endSegments = endSegments(validSegments);
disp("Total de operadores/numeros detetados: " + length(beginSegments));
results = {};
% iterar sobre cada segmento, redimensionar para 150x150 e enviar para a rede neural para identificação
for i = 1:length(beginSegments)
segment = binaryImg(:, beginSegments(i):endSegments(i));
resizedSegment = imresize(segment, [25 25]);
% Cria um novo uiaxes e mostra a imagem redimensionada
% Ajuste a posição e o tamanho do uiaxes conforme necessário
%segmentAxes = uiaxes(fig, 'Position', [450 + (i-1)*30, 50, 25, 25]);
%imshow(resizedSegment, 'Parent', segmentAxes);
%uistack(segmentAxes, 'bottom');
% converter a matriz para uma matriz vertical e coloca-la num array 3D
inputMatrixVertical = resizedSegment(:);
inputArray3D = zeros(1, 1, numel(inputMatrixVertical));
inputArray3D(1,1,:) = inputMatrixVertical;
% envia imagem para a rede neural e retorna o resultado da classificação
outputMatrix = net(inputMatrixVertical);
[~, result] = max(outputMatrix);
disp(" :Identified -> " + categories{result});
results = [results, categories{result}];
end
% exibe o resultado
resultText.Value = sprintf('Identified expression: %s', strjoin(results, ' '));
end
end
function changeCategories(src, event)
categories = event.NewValue.UserData;
categoriesLabel.Text = sprintf("Current Categories: %s", string(strjoin(categories,',')) )
end
function identifyExpression(src, event)
if isempty(img)
resultText.Value = 'No image selected or drawn.';
else
% converte a imagem para grayscale caso nao seja
if ndims(img) == 3
img = rgb2gray(img);
end
% converter imagem para uma matrix binaria
binaryImg = imbinarize(img);
% redimensionar a imagem para
resizedImg = imresize(binaryImg, [25 25]);
% converter a matriz para uma matriz vertical e coloca-la num array 3D
inputMatrixVertical = resizedImg(:);
inputArray3D = zeros(1, 1, numel(inputMatrixVertical));
inputArray3D(1,1,:) = inputMatrixVertical;
% envia imagem para a rede neural e retorna o resultado da classificação
outputMatrix = net(inputMatrixVertical);
[~, result] = max(outputMatrix);
disp("Identified -> " + categories{result});
% exibe o resultado
resultText.Value = sprintf('Identified character: %s', categories{result});
end
end
function drawImage(src, event)
canvas = figure('Name', 'Draw Image', 'Position', [600 300 150 150]);
imgAx2 = axes(canvas, 'Position', [0 0 1 1]);
axis(imgAx2, 'equal');
hold(imgAx2, 'on');
% Inicializar a imagem desenhada
img = ones(150, 150) * 255;
% Configurar o desenho interativo
set(canvas, 'WindowButtonDownFcn', @startDrawing);
set(canvas, 'WindowButtonUpFcn', @stopDrawing);
set(canvas, 'WindowButtonMotionFcn', @drawLine);
drawing = false;
function startDrawing(src, event)
drawing = true;
end
function stopDrawing(src, event)
drawing = false;
end
function drawLine(src, event)
if drawing
cp = imgAx2.CurrentPoint;
x = round(cp(1, 1));
y = round(cp(1, 2));
raio = 10;
% Desenhar um círculo preto com raio de 5 pixels
for dx = -raio:raio
for dy = -raio:raio
if dx^2 + dy^2 <= raio^2
ix = x + dx;
iy = y + dy;
if ix > 0 && ix <= 150 && iy > 0 && iy <= 150
img(iy, ix) = 0;
end
end
end
end
imshow(img, 'Parent', imgAx2);
end
end
% Adicionar botão para salvar a imagem desenhada
uicontrol(canvas, 'Style', 'pushbutton', 'String', 'Save Image', ...
'Units', 'normalized', 'Position', [0.8 0.05 0.15 0.1], ...
'Callback', @saveImage);
function saveImage(src, event)
[file, path] = uiputfile({'*.png', 'PNG Image'}, 'Save Image');
if file ~= 0
imwrite(img, fullfile(path, file), 'png');
end
close(canvas);
end
end
end