-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
57 lines (36 loc) · 1.38 KB
/
app.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
"""
author: Lucas Lacerda @lucaaslb
Exemplo para preditar uma imagem com um numero a partir do modelo treinado, exemplo de imagens no diretorio './images'
Executar:
python3 app.py 'local_imagem'
*deve ser informado o path absoluto da imagem
"""
import cv2
import numpy as np
import sys
from keras.models import load_model
from keras.preprocessing import image
image_x, image_y = 28, 28
classifier = load_model('models/trained_model_mnist.h5')
def predictor(img):
test_image = image.img_to_array(img)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
maior, class_index = -1, -1
for x in range(10):
if result[0][x] > maior:
maior = result[0][x]
class_index = x
return [result, class_index]
def main() :
path_img = str(sys.argv[1])
img = cv2.imread(path_img)
img = cv2.resize(img, (image_x, image_y))
imggray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY, dstCn=1)
predict = predictor(imggray)
print('\n\n===========================\n')
print('Imagem: ', path_img)
print('Vetor de resultado: ', predict[0])
print('Classe: ', predict[1])
print('\n===========================\n')
__init__ = main()