-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
88 lines (61 loc) · 2.47 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
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
import pygame,sys
from pygame.locals import *
import numpy as np
from keras.models import load_model
import cv2
WINDOWSIZEX=640
WINDOWSIZEY=480
BOUNDARYINC=5
WHITE=(255,255,255)
BLACK=(0,0,0)
RED=(255,0,0)
IMGSAVE=False
MODEL=load_model("bestmodel.h5")
LABELS={0:"ZERO", 1:"ONE", 2:"TWO", 3:"THREE", 4:"FOUR", 5:"FIVE", 6:"SIX", 7:"SEVEN", 8:"EIGHT", 9:"NINE"}
# Intialize our pygame
pygame.init()
DISPLAYSURF=pygame.display.set_mode((WINDOWSIZEX,WINDOWSIZEY))
pygame.display.set_caption("Digit Board")
iswriting=False
FONT=pygame.font.Font("Roboto-Black.ttf",18)
number_xcord=[]
number_ycord=[]
image_cnt=1
PREDICT=True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION and iswriting:
xcord,ycord=event.pos
pygame.draw.circle(DISPLAYSURF,WHITE,(xcord,ycord),4,0)
number_xcord.append(xcord)
number_ycord.append(ycord)
if event.type == MOUSEBUTTONDOWN:
iswriting=True
if event.type == MOUSEBUTTONUP:
iswriting=False
number_xcord=sorted(number_xcord)
number_ycord=sorted(number_ycord)
rect_min_x,rect_max_x=max(number_xcord[0]-BOUNDARYINC,0),min(WINDOWSIZEX,number_xcord[-1]+BOUNDARYINC)
rect_min_y,rect_max_y=max(number_ycord[0]-BOUNDARYINC,0),min(WINDOWSIZEX,number_ycord[-1]+BOUNDARYINC)
number_xcord=[]
number_ycord=[]
img_arr=np.array(pygame.PixelArray(DISPLAYSURF))[rect_min_x:rect_max_x,rect_min_y:rect_max_y].T.astype(np.float32)
if IMGSAVE:
cv2.imwrite("image.png")
image_cnt+=1
if PREDICT:
image=cv2.resize(img_arr,(28,28))
image=np.pad(image,(10,10),'constant',constant_values=0)
image=cv2.resize(image,(28,28))/255
label=str(LABELS[np.argmax(MODEL.predict(image.reshape(1,28,28,1)))])
textSurface=FONT.render(label,True,RED,WHITE)
textRecObj= textSurface.get_rect()
textRecObj.left, textRecObj.bottom=rect_min_x,rect_min_y
DISPLAYSURF.blit(textSurface,textRecObj)
if event.type == KEYDOWN:
if event.unicode == "n":
DISPLAYSURF.fill(BLACK)
pygame.display.update()