-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstylus.py
133 lines (111 loc) · 4.07 KB
/
stylus.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
# @Author: amaneureka
# @Date: 2017-05-19 00:54:11
# @Last Modified by: amaneureka
# @Last Modified time: 2017-05-19 12:52:17
import cv2
import pygame
import random
import argparse
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import utility
from models import cnn
sample_width = 74
sample_height = 77
sample_num_class = 62
def select_color():
return (random.randrange(256), random.randrange(256), random.randrange(256))
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.display.update(pygame.draw.circle(srf, color, (x, y), radius))
def normalize_pygame_image(screen, topleft, width, height):
data = pygame.image.tostring(screen, 'RGB')
img = np.fromstring(data, dtype=np.uint8)
img.shape = (screen.get_height(), screen.get_width(), -1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img[topleft[1]:, topleft[0]:]
_, img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[len(contours) - 1])
img = img[y:y+h, x:x+w]
factor = min(float(width/w), float(height/h))
img = cv2.resize(img, None, fx=factor, fy=factor, interpolation = cv2.INTER_CUBIC)[:height, :width]
return img
if __name__ == '__main__':
# parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--width', action='store', dest='scr_width',
help='screen width', type=int, default=800)
parser.add_argument('--height', action='store', dest='scr_height',
help='screen height', type=int, default=600)
parser.add_argument('--radius', action='store', dest='stroke_radius',
help='stroke radius', type=int, default=10)
parser.add_argument('--restore', action='store', dest='restore',
help='restore network model', required=True)
args = parser.parse_args()
# restore network
x, y, _, _ = cnn.create_network(sample_height, sample_width, sample_num_class)
y_pred_cls = tf.argmax(y, dimension=1)
session = tf.Session()
session.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess=session, save_path=args.restore)
# paint program
pygame.init()
screen = pygame.display.set_mode((args.scr_width, args.scr_height))
pygame.display.set_caption('Stylus')
myfont = pygame.font.SysFont("arial", 20)
last_pos = (0, 0)
status_pos = (5, 10)
enable_drawing = False
radius = args.stroke_radius
prev_rect = pygame.Rect(0, 0, 0, 0)
img_canvas = np.zeros((sample_height, sample_width), dtype=np.uint8)
try:
while True:
e = pygame.event.wait()
if e.type == pygame.QUIT:
raise StopIteration
elif e.type == pygame.KEYDOWN:
pygame.display.update(screen.fill((0, 0, 0)))
elif e.type == pygame.MOUSEBUTTONDOWN:
color = select_color()
enable_drawing = True
elif e.type == pygame.MOUSEBUTTONUP:
enable_drawing = False
try:
# get bounding image
img = normalize_pygame_image(screen, prev_rect.bottomright, sample_width, sample_height)
w = img.shape[1]
h = img.shape[0]
img_canvas.fill(0)
img_canvas[:h,:w] = img
# plt.imshow(img_canvas, cmap='gray')
# plt.show()
feed = {x : img_canvas.reshape(1, -1)}
prd_matrix, prd = session.run([y, y_pred_cls], feed_dict=feed)
loss = prd_matrix[0, prd - 1][0]
prd = utility.SAMPLE(prd).name
print(prd, loss)
# show prediction status
label = myfont.render("Prediction: {0} {1}".format(prd, loss), 1, (255, 255, 255))
pygame.display.update(screen.fill((0, 0, 0), rect=prev_rect))
pygame.display.update(screen.blit(label, status_pos))
prev_rect = label.get_rect(topleft=status_pos)
except:
pass
elif e.type == pygame.MOUSEMOTION:
if enable_drawing:
pygame.display.update(pygame.draw.circle(screen, color, e.pos, radius))
roundline(screen, color, e.pos, last_pos, radius)
last_pos = e.pos
except StopIteration:
pass
pygame.quit()