-
Notifications
You must be signed in to change notification settings - Fork 0
/
painting_app.py
68 lines (55 loc) · 1.9 KB
/
painting_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
import cv2
import numpy as np
from src.config import *
from src.dataset import CLASSES
import torch
def main():
# Load model
if torch.cuda.is_available():
model = torch.load("trained_models/whole_model_quickdraw")
else:
model = torch.load("trained_models/whole_model_quickdraw", map_location=lambda storage, loc: storage)
model.eval()
image = np.zeros((480, 640, 3), dtype=np.uint8)
cv2.namedWindow("Canvas")
global ix, iy, is_drawing
is_drawing = False
def paint_draw(event, x, y, flags, param):
global ix, iy, is_drawing
if event == cv2.EVENT_LBUTTONDOWN:
is_drawing = True
ix, iy = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if is_drawing == True:
cv2.line(image, (ix, iy), (x, y), WHITE_RGB, 5)
ix = x
iy = y
elif event == cv2.EVENT_LBUTTONUP:
is_drawing = False
cv2.line(image, (ix, iy), (x, y), WHITE_RGB, 5)
ix = x
iy = y
return x, y
cv2.setMouseCallback('Canvas', paint_draw)
while (1):
cv2.imshow('Canvas', 255 - image)
key = cv2.waitKey(10)
if key == ord(" "):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ys, xs = np.nonzero(image)
min_y = np.min(ys)
max_y = np.max(ys)
min_x = np.min(xs)
max_x = np.max(xs)
image = image[min_y:max_y, min_x: max_x]
image = cv2.resize(image, (28, 28))
image = np.array(image, dtype=np.float32)[None, None, :, :]
image = torch.from_numpy(image)
logits = model(image.cuda())
#logits = model(image)
print(CLASSES[torch.argmax(logits[0])])
image = np.zeros((480, 640, 3), dtype=np.uint8)
ix = -1
iy = -1
if __name__ == '__main__':
main()