-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (74 loc) · 2.78 KB
/
main.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
from scipy.ndimage.filters import gaussian_filter
import pygame
from keras.models import load_model # TensorFlow is required for Keras to work
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import requests
import time
label_names = requests.get("https://raw.githubusercontent.com/googlecreativelab/quickdraw-dataset/master/categories.txt").text.split("\n")
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([784, 784])
# Run until the user asks to quit
running = True
screen.fill((0, 0, 0))
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
# Load the model
model = load_model("model.h5", compile=False)
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
data = np.ndarray(shape=(28, 28, 1), dtype=np.float32)
def predict_image(model, x):
x = x.astype('float32')
x = x / 255.0
x = np.expand_dims(x, axis=0)
image_predict = model.predict(x, verbose=0)
print("Predicted Label: ", label_names[np.argmax(image_predict)])
return image_predict
def plot_image(img):
plt.imshow(np.squeeze(img))
plt.xticks([])
plt.yticks([])
plt.show()
draw_last = (0, 0)
pressed_last = False
last_time = 0
while running:
x, y = pygame.mouse.get_pos()
# Did the user click the window close button?
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
pygame.image.save(screen, "screenshot.jpg")
# Replace this with the path to your image
imgd = tf.keras.preprocessing.image.load_img('screenshot.jpg', target_size=(28, 28, 1),
color_mode="grayscale")
img = tf.keras.preprocessing.image.img_to_array(imgd)
img = gaussian_filter(img, sigma=0.5)
predict_image(model, img)
# if time.time() >= last_time + 1:
# pygame.image.save(screen, "screenshot.jpg")
#
# # Replace this with the path to your image
# imgd = tf.keras.preprocessing.image.load_img('screenshot.jpg', target_size=(28, 28, 1),
# color_mode="grayscale")
# img = tf.keras.preprocessing.image.img_to_array(imgd)
# img = gaussian_filter(img, sigma=0.5)
#
# predict_image(model, img)
#
# last_time = time.time()
mouse_presses = pygame.mouse.get_pressed()
if mouse_presses[0]:
pygame.draw.circle(screen, (255, 255, 255), (x, y), 30)
if mouse_presses[2]:
screen.fill((0, 0, 0))
drawn = []
pressed_last = mouse_presses[0]
draw_last = (x, y)
# Flip the display
pygame.display.flip()
# Done! Time to quit.
pygame.quit()