forked from remi2257/sudoku-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_solver_kivy.py
196 lines (129 loc) · 4.24 KB
/
sudoku_solver_kivy.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.uix.widget import Widget
# from kivy.uix.togglebutton import ToggleButton
import cv2
from tensorflow.keras.models import load_model
from src.main_single_img import process_single_img
kivy.require('1.9.0')
model_default_name = 'model/my_model.h5'
# noinspection PyArgumentList
def convert_opencv_to_texture(frame):
if frame is None:
return Texture()
buf1 = cv2.flip(frame, 0)
buf = buf1.tostring()
image_texture = Texture.create(
size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
return image_texture
def convert_texture_to_opencv(texture):
pixels = texture.pixels
img_opencv = cv2.flip(pixels, 0)
return img_opencv
class MyKivyCamera(Image):
def __init__(self, fps=30, **kwargs):
super().__init__(**kwargs)
self.android = kivy.platform == 'android'
self.__freeze = False
self.last_image_read = None
self.capture = None
Clock.schedule_interval(self.update, 1.0 / fps)
def update(self, _dt):
ret, frame = self.read_img()
if ret:
self.last_image_read = frame
if not self.__freeze:
# display image from the texture
self.texture = convert_opencv_to_texture(frame)
def start_capture(self):
if self.android:
self.capture = None
else:
self.capture = cv2.VideoCapture(-1)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
def stop_capture(self):
if self.android:
pass
else:
self.capture.release()
self.capture = None
def read_img(self):
if self.capture is None:
return False, None
if self.android:
return False, None
else:
return self.capture.read()
class MonBouton(Widget):
btn1 = ObjectProperty(None)
btn2 = ObjectProperty(None)
class SolverScreen(Screen):
def __init__(self, **kwargs):
super(SolverScreen, self).__init__(**kwargs)
self.model = load_model(model_default_name)
@property
def target_image(self):
raise NotImplementedError
def solve(self):
frame = self.target_image
solved_frame = process_single_img(frame, self.model)
self.display_solved(solved_frame)
def display_solved(self, opencv_frame):
raise NotImplementedError
class LiveSolverScreen(SolverScreen):
camera = ObjectProperty(None)
def __init__(self, **kwargs):
super(LiveSolverScreen, self).__init__(**kwargs)
self.model = load_model(model_default_name)
@property
def target_image(self):
return self.camera.last_image_read
def display_solved(self, opencv_frame):
self.camera.texture = convert_opencv_to_texture(opencv_frame)
def start_stream(self):
self.camera.start_capture()
def stop_stream(self):
self.camera.stop_capture()
class GallerySolverScreen(SolverScreen):
"""
Voir FileChooser
https://kivy.org/doc/stable/api-kivy.uix.filechooser.html
"""
kivy_image = ObjectProperty(None)
original_image = None
@property
def target_image(self):
return self.original_image
def display_solved(self, opencv_frame):
self.camera.texture = convert_opencv_to_texture(opencv_frame)
class ManualSolverScreen(Screen):
pass
class HelpScreen(Screen):
pass
class AboutScreen(Screen):
pass
class MainScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("sudoku.kv")
class SudokuSolverApp(App):
def build(self):
return kv
if __name__ == "__main__":
SudokuSolverApp().run()
"""
Useful stuff
def display_solved(self, frame):
self.manager.ids.solver.image.texture = MyCamera.convert_to_texture(frame)
self.manager.transition.direction = "left"
self.manager.current = "solver"
"""