-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto_draw.py
157 lines (114 loc) · 4.69 KB
/
photo_draw.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import io
import os
import tkinter as tk
import numpy as np
from PIL import Image, ImageTk
from keras.models import load_model
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
class CarApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.class_to_display = 0
self.frames = {}
for F in (DrawTest, GifPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(DrawTest)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class DrawTest(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.text = tk.Label(self,
text="Welcome to the car drawing detector !"
"\n Click on the button below "
"\n and draw something",
bd=1, font='{Comic Sans MS} 16')
self.text.grid(padx=30, pady=80)
self.button_start = tk.Button(self, text='20 seconds to draw', command=self.display_canvas)
self.button_start.grid(padx=30)
self.canvas = tk.Canvas(self, width=440, height=280)
self.canvas.old_coords = None
self.canvas.event_time = None
self.counter = 20
self.label = tk.Label(self, fg="green")
self.label.config(text=self.counter)
self.model = load_model(os.path.join(PROJECT_PATH, 'model.h5'))
def display_canvas(self):
self.button_start.grid_forget()
self.text.grid_forget()
self.canvas.grid(padx=10, pady=10)
self.label.grid()
self.controller.bind('<B1-Motion>', self.drawing)
self.counter_label(self.label)
def counter_label(self, label):
def count():
self.counter -= 1
if self.counter != 0:
self.label.config(text=self.counter)
self.label.after(1000, count)
else:
self.counter = 20
self.make_inference()
count()
def drawing(self, event):
x, y = event.x, event.y
event_time = event.time
if self.canvas.old_coords and (event_time - self.canvas.event_time) < 200:
x1, y1 = self.canvas.old_coords
self.canvas.create_line(x, y, x1, y1, width=3)
self.canvas.old_coords = x, y
self.canvas.event_time = event_time
def make_inference(self):
ps = self.canvas.postscript(colormode='color')
img = Image.open(io.BytesIO(ps.encode('utf-8')))
self.canvas.delete("all")
self.canvas.grid_forget()
self.label.grid_forget()
self.text.grid(padx=30, pady=80)
self.button_start.grid(padx=30)
img = img.resize((128, 128))
img.save(os.path.join(PROJECT_PATH, 'last_capture.jpg'))
image = np.array(img).reshape((1, 128, 128, 3))
prediction = self.model.predict_classes(image)[0][0]
self.controller.class_to_display = prediction
self.controller.frames[GifPage].index = 0
self.controller.show_frame(GifPage)
class GifPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.panel = tk.Label(self)
self.panel.grid(padx=80)
self.controller = controller
button_back = tk.Button(self, text='Back', command=lambda: self.controller.show_frame(DrawTest))
button_back.grid()
self.gif_images = {
0: [ImageTk.PhotoImage(
image=Image.open(
os.path.join(PROJECT_PATH, 'car_gif', 'frame_{}.jpg'.format(str(x + 1).zfill(2)))).resize(
(200, 240))) for x in
range(48)],
1: [ImageTk.PhotoImage(
image=Image.open(
os.path.join(PROJECT_PATH, 'not_car_gif', 'frame_{}.jpg'.format(str(x + 1).zfill(2)))).resize(
(200, 240))) for x
in range(89)]
}
self.index = 0
self.animate()
def animate(self):
image = self.gif_images[self.controller.class_to_display][self.index]
self.panel.configure(image=image)
self.panel.image = image
self.index = (self.index + 1) % len(self.gif_images[self.controller.class_to_display])
self.panel.after(100, self.animate)
if __name__ == '__main__':
app = CarApp()
app.mainloop()