-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_recognition.py
68 lines (53 loc) · 2.21 KB
/
number_recognition.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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import cv2
from math import floor, ceil
from tensorflow.keras.models import load_model
from PIL import Image
from skimage.exposure import rescale_intensity
from skimage import util
from helpers import rescale_img
import helpers
model = None
images_for_nr=[]
images_for_nn=[]
def predict(cut_digit_img: np.ndarray) -> int:
global model
global images_for_nn
global images_for_nr
images_for_nr.append(rescale_img(cut_digit_img,28))
minmax = (cut_digit_img.flatten().min(), cut_digit_img.flatten().max())
cut_digit_img = rescale_intensity(cut_digit_img, minmax)
cut_digit_img = util.invert(cut_digit_img)
# _, cut_digit_img = cv2.threshold(cut_digit_img, 110, 255, cv2.THRESH_TOZERO) # Possible deletion
cut_digit_img = cv2.erode(cut_digit_img, np.ones((2, 1), np.uint8), iterations=3)
cut_digit_img = rescale_img(cut_digit_img, 24)
if len(cut_digit_img[0])>28:
return 0
helpers.wait_for_key_on_value_error("digit wider then its height! maybe board detection problem?")
if model is None:
# model = load_model("number_recognition_model/number_recognition_model_conv.h5") # Conv model
model = load_model("number_recognition_model/number_recognition_model.h5") # Dense model
image_for_nn = process_img(cut_digit_img)
img_for_show=image_for_nn.reshape(28,28)
images_for_nn.append(img_for_show)
res = model.predict([image_for_nn])[0]
return np.argmax(res)
def process_img(cut_digit_img: np.ndarray) -> np.ndarray:
# cut_digit_img = cut_digit_img.convert('L')
# cut_digit_img = np.array(cut_digit_img)
cut_digit_img = cut_digit_img / 255
new_image = np.zeros((28, 28))
old_image_rows, old_image_cols = cut_digit_img.shape
new_image[
14 - floor(old_image_rows / 2):14 + ceil(old_image_rows / 2),
14 - floor(old_image_cols / 2):14 + ceil(old_image_cols / 2)
] = cut_digit_img
# new_image = new_image.reshape(1, 28, 28, 1) # Conv model
new_image = new_image.reshape(1, 784) # Dense model
new_image[new_image < 0.2] = 0
return new_image
def show_imgs_for_nn():
if(len(images_for_nn)>0):
cv2.imshow('imgsForNN',np.hstack(images_for_nn))