forked from surya-veer/RealTime-DigitRecognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
118 lines (88 loc) · 3.01 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
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
###############################################################
######### By: suryaveer @IIT Indore ##########
######### GITHUB: https://github.com/surya-veer ##########
###############################################################
import pygame
#select model for prediction
# MODEL = 'SVC'
MODEL = 'KERAS'
# trained on 8X8 images
if(MODEL=='SVC'):
from models_svc import digit_recognition
# trained on 28X28 images
if(MODEL=='KERAS'):
from models_keras import digit_recognition
#pre defined colors, pen radius and font color
black = [0,0,0]
white = [255, 255, 255]
red = [255, 0, 0]
green = [0,255,0]
draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 10
font_size = 500
#initializing screen
screen = pygame.display.set_mode((1000,500))
screen.fill(white)
pygame.font.init()
def print_digit(num):
#line for partition
pygame.draw.rect(screen, white, [500, 0, 1000, 500])
#heading text
heading_font = pygame.font.SysFont('', 100)
heading = heading_font.render('Output is', False, green)
screen.blit(heading,(600,20))
#output text
myfont = pygame.font.SysFont('',font_size)
output_text = myfont.render(str(num)[1], False, red)
screen.blit(output_text,(680,130))
def crope(orginal):
cropped = pygame.Surface((500, 500))
cropped.blit(orginal, (0, 0), (0,0, 500, 500))
return cropped
def roundline(srf, color, start, end, radius=1):
dx = end[0]-start[0]
dy = end[1]-start[1]
distance = max(abs(dx), abs(dy))
for i in range(distance):
x = int( start[0]+float(i)/distance*dx)
y = int( start[1]+float(i)/distance*dy)
pygame.draw.circle(srf, color, (x, y), radius)
def draw_line():
pygame.draw.line(screen, black, [510, 0], [510, 500], 10)
try:
while True:
# get all events
e = pygame.event.wait()
draw_line()
#clear screen after right click
if(e.type == pygame.MOUSEBUTTONDOWN and e.button==3):
screen.fill(white)
#quit
if e.type == pygame.QUIT:
raise StopIteration
#start drawing after left click
if(e.type == pygame.MOUSEBUTTONDOWN and e.button!=3):
color = black
pygame.draw.circle(screen, color, e.pos, radius)
draw_on = True
#stop drawing after releasing left click
if e.type == pygame.MOUSEBUTTONUP and e.button!=3:
draw_on = False
fname = "assets/out.png"
img = crope(screen)
pygame.image.save(img, fname)
res = digit_recognition.check()
print("Output is:=>> ",res[0], '\n\n Getting...')
print_digit(res)
#start drawing line on screen if draw is true
if e.type == pygame.MOUSEMOTION:
if draw_on:
pygame.draw.circle(screen, color, e.pos, radius)
roundline(screen, color, e.pos, last_pos, radius)
last_pos = e.pos
pygame.display.flip()
except StopIteration:
pass
pygame.quit()