Skip to content

Commit 11c8f76

Browse files
committed
polish format
1 parent c12b320 commit 11c8f76

File tree

2 files changed

+75
-51
lines changed

2 files changed

+75
-51
lines changed

samples/vision/inkrecognition_tkinter_sample.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
else:
66
from tkinter import *
77
from tkinter import messagebox
8-
from azure.ai.inkrecognizer import ApplicationKind, InkPointUnit, InkStrokeKind
9-
from azure.ai.inkrecognizer import InkRecognizerClient, InkRecognitionUnitKind
108
from collections import namedtuple
9+
from azure.ai.inkrecognizer import ApplicationKind, InkStrokeKind
10+
from azure.ai.inkrecognizer import InkRecognizerClient
1111

1212

1313
# Ink Recognizer Client Config
@@ -16,12 +16,14 @@
1616

1717

1818
# Recognition Config
19-
# This tell Ink Recognizer Service that the sample is in en-US.
19+
# This tell Ink Recognizer Service that the sample is in en-US.
2020
# Default value is "en-US".
2121
# If "language" in a stroke is specified, this will be overlaped in that stroke.
2222
LANGUAGE_RECOGNITION_LOCALE = "en-US"
23-
# This tell Ink Recognizer Service that domain of the application is writing, i.e. all strokes are writing.
24-
# Default value is ApplicationKind.MIXED, which means let Ink Recognizer Service detect kind of strokes.
23+
# This tell Ink Recognizer Service that domain of the application is writing,
24+
# i.e. all strokes are writing.
25+
# Default value is ApplicationKind.MIXED, which means let Ink Recognizer
26+
# Service detect kind of strokes.
2527
# If "kind" in a stroke is specified, this will be overlaped in that stroke.
2628
APPLICATION_KIND = ApplicationKind.WRITING
2729

@@ -66,16 +68,18 @@ def _reset_stroke(self):
6668

6769
def _pixel_to_mm(self, pixel):
6870
return pixel * 1.0 / self._pixel_per_mm
69-
71+
7072
def reset_ink(self):
7173
self._ink_stroke_list = []
7274
self._root = None
7375
self._reset_stroke()
7476

7577
def add_point(self, x, y):
76-
# Convert from pixel to mm before sending to InkPoint
77-
# You can also specify keyword argument "unit_multiple" in InkRecognizerClient constructor or in recognizer_ink() request
78-
self._curr_stroke_points.append(InkPoint(self._pixel_to_mm(x), self._pixel_to_mm(y)))
78+
# Convert from pixel to mm before sending to InkPoint.
79+
# You can also specify keyword argument "unit_multiple" in
80+
# InkRecognizerClient constructor or in recognizer_ink() request.
81+
self._curr_stroke_points.append(
82+
InkPoint(self._pixel_to_mm(x), self._pixel_to_mm(y)))
7983

8084
def stroke_end(self):
8185
stroke = InkStroke(len(self._ink_stroke_list), self._curr_stroke_points)
@@ -86,11 +90,15 @@ def recognize(self):
8690
self._root = None
8791
try:
8892
root = self._client.recognize_ink(
89-
self._ink_stroke_list,
93+
self._ink_stroke_list,
9094
# Pre-set recognition type
91-
application_kind=APPLICATION_KIND,
95+
application_kind=APPLICATION_KIND,
9296
# Set language recognition locale
93-
language=LANGUAGE_RECOGNITION_LOCALE)
97+
language=LANGUAGE_RECOGNITION_LOCALE
98+
)
99+
# Aruments in request is for this request only
100+
# You can also specify these arguments in InkRecognizerClient constructor,
101+
# which will be default arguments for each call.
94102
result_text = []
95103
for word in root.ink_words:
96104
result_text.append(word.recognized_text)
@@ -116,11 +124,12 @@ class InkRecognizerDemo:
116124
def __init__(self):
117125
self._master = Tk()
118126
self._pack_widgets()
119-
120-
self._recognition_manager = RecognitionManager(pixel_per_mm=self._master.winfo_fpixels("1m"))
127+
128+
self._recognition_manager = RecognitionManager(
129+
pixel_per_mm=self._master.winfo_fpixels("1m"))
121130
# point for drawing stroke
122131
self._last_point = None
123-
132+
124133
def _pack_widgets(self):
125134
self._master.title("Ink Recognizer Demo")
126135
# search words
@@ -130,12 +139,13 @@ def _pack_widgets(self):
130139
search_entry.pack(pady=5)
131140
search_button.pack()
132141
# main canvas
133-
self._canvas = Canvas(self._master,
134-
width=CANVAS_WIDTH,
135-
height=CANVAS_HEIGHT)
142+
self._canvas = Canvas(
143+
self._master,
144+
width=CANVAS_WIDTH,
145+
height=CANVAS_HEIGHT)
136146
self._canvas.pack(expand=YES, fill = BOTH)
137-
self._canvas.bind( "<B1-Motion>", self._draw)
138-
self._canvas.bind( "<Button-1>", self._stroke_start)
147+
self._canvas.bind("<B1-Motion>", self._draw)
148+
self._canvas.bind("<Button-1>", self._stroke_start)
139149
self._canvas.bind("<ButtonRelease-1>", self._stroke_end)
140150
# recognize and clear buttons
141151
recognize_button = Button(
@@ -146,7 +156,7 @@ def _pack_widgets(self):
146156
clear_button.pack(pady=5)
147157

148158
def _draw(self, event):
149-
# paint on canvas
159+
# paint on canvas
150160
x_curr, y_curr = event.x, event.y
151161
if self._last_point is not None:
152162
x_last, y_last = self._last_point[0], self._last_point[1]
@@ -159,25 +169,25 @@ def _draw(self, event):
159169
def _stroke_start(self, event):
160170
# nothing need to do
161171
pass
162-
172+
163173
def _stroke_end(self, event):
164174
self._recognition_manager.stroke_end()
165175
self._last_point = None
166176

167177
def _clear_canvas(self):
168178
self._canvas.delete("all")
169179
self._recognition_manager.reset_ink()
170-
180+
171181
def _recognize(self):
172182
self._recognition_manager.recognize()
173183

174184
def _search(self):
175185
self._recognition_manager.search(self._search_variable.get())
176186

177-
def run(self):
187+
def run(self):
178188
mainloop()
179189

180190

181191
if __name__ == "__main__":
182192
demo = InkRecognizerDemo()
183-
demo.run()
193+
demo.run()

samples/vision/inkrecognition_wxpython_sample.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010

1111

1212
# Recognition Config
13-
# This tell Ink Recognizer Service that the sample is in en-US.
13+
# This tell Ink Recognizer Service that the sample is in en-US.
1414
# Default value is "en-US".
1515
# If "language" in a stroke is specified, this will be overlaped in that stroke.
1616
LANGUAGE_RECOGNITION_LOCALE = "en-US"
17-
# This tell Ink Recognizer Service that domain of the application is writing, i.e. all strokes are writing.
18-
# Default value is ApplicationKind.MIXED, which means let Ink Recognizer Service detect kind of strokes.
17+
# This tell Ink Recognizer Service that domain of the application is writing,
18+
# i.e. all strokes are writing.
19+
# Default value is ApplicationKind.MIXED, which means let Ink Recognizer
20+
# Service detect kind of strokes.
1921
# If "kind" in a stroke is specified, this will be overlaped in that stroke.
2022
APPLICATION_KIND = ApplicationKind.WRITING
2123

2224

23-
# This ratio map the number of pixel for x and y axis coordinates on canvas into number of mm
24-
# In InK Recognizer Server, every coordinate in InkPoint will multiply this number
25-
# You may also want to mutliply /divide this value before sending request and after receiving response
25+
# This ratio map the number of pixel for x and y axis coordinates on canvas
26+
# into number of mm.
27+
# In InK Recognizer Server, every coordinate in InkPoint will multiply this number.
28+
# You may also want to mutliply /divide this value before sending request and
29+
# after receiving response.
2630
app = wx.App(False)
27-
mm_on_canvas = float(wx.GetDisplaySizeMM()[1])
31+
mm_on_canvas = float(wx.GetDisplaySizeMM()[1])
2832
pixel_on_canvas = float(wx.GetDisplaySize()[1])
29-
UNIT_MULTIPLE = mm_on_canvas / pixel_on_canvas
33+
UNIT_MULTIPLE = mm_on_canvas / pixel_on_canvas
3034

3135

3236
# UI config
@@ -60,18 +64,21 @@ def __init__(self,
6064
class RecognitionManager:
6165
def __init__(self):
6266
self._client = InkRecognizerClient(
63-
URL, CREDENTIAL,
64-
ink_point_unit=InkPointUnit.MM,
67+
URL,
68+
CREDENTIAL,
69+
ink_point_unit=InkPointUnit.MM,
6570
# Convert stroke unit from pixel to mm by specify unit_multiple
6671
# You can also multiply the number when creating InkPoints
6772
unit_multiple=UNIT_MULTIPLE,
6873
# Set language recognition locale
6974
language=LANGUAGE_RECOGNITION_LOCALE,
7075
# Pre-set recognition type
7176
application_kind=APPLICATION_KIND
72-
)
77+
)
78+
# Aruments in constructor becomes default arguments for each request
79+
# You can also specify these arguments in recognize_ink() requests.
7380
self._reset_ink()
74-
81+
7582
def _reset_ink(self):
7683
self._stroke_list = []
7784
self._reset_stroke()
@@ -88,11 +95,12 @@ def stroke_start(self):
8895
return
8996

9097
def stroke_end(self):
91-
stroke = InkStroke(len(self._stroke_list),
92-
self._curr_stroke_points)
98+
stroke = InkStroke(
99+
len(self._stroke_list),
100+
self._curr_stroke_points)
93101
self._stroke_list.append(stroke)
94102
self._reset_stroke()
95-
103+
96104
def get_stroke_list(self):
97105
return self._stroke_list
98106

@@ -150,12 +158,12 @@ def on_paint(self, event):
150158

151159
def on_click(self, event):
152160
self._recognition_manager.stroke_start()
153-
161+
154162
def on_drag(self, event):
155163
if event.Dragging():
156164
self._recognition_manager.add_point(event.X, event.y)
157165
self.Refresh()
158-
166+
159167
def on_release(self, event):
160168
self._recognition_manager.stroke_end()
161169
self.Refresh()
@@ -171,7 +179,7 @@ def search(self, event, word, call_back):
171179
self._recognition_manager.search(word, call_back)
172180

173181

174-
# Sample wxpython app
182+
# Sample wxpython app
175183
class InkRecognizerDemo(wx.Frame):
176184
def __init__(self):
177185
super(InkRecognizerDemo, self).__init__(None)
@@ -184,18 +192,24 @@ def __init__(self):
184192
self.clear_button = wx.Button(self.view, wx.ID_ANY, 'Clear', (0, canvas_height - 30))
185193

186194
self.search_text = wx.TextCtrl(self.view, wx.ID_ANY, "", (0, canvas_height - 120))
187-
func_search = lambda event: self.view.search(event, self.search_text.GetLineText(0), call_back=self.show_search_result)
188-
self.search_button.Bind(wx.EVT_BUTTON, func_search)
189-
func_recognize = lambda event: self.view.recognize(event, call_back=self.show_result)
190-
self.recognize_button.Bind(wx.EVT_BUTTON, func_recognize)
195+
self.search_button.Bind(wx.EVT_BUTTON, self._search_function)
196+
self.recognize_button.Bind(wx.EVT_BUTTON, self._recognize_function)
191197
self.clear_button.Bind(wx.EVT_BUTTON, self.view.clear)
192-
193198

194-
def show_result(self, result):
199+
def _search_function(self, event):
200+
return self.view.search(
201+
event,
202+
self.search_text.GetLineText(0),
203+
call_back=self._show_search_result)
204+
205+
def _recognize_function(self, event):
206+
return self.view.recognize(event, call_back=self._show_result)
207+
208+
def _show_result(self, result):
195209
dlg = wx.MessageDialog(self, result, "Recognition Result")
196210
dlg.ShowModal()
197-
198-
def show_search_result(self, num_words):
211+
212+
def _show_search_result(self, num_words):
199213
dlg = wx.MessageDialog(self, "Find %s words" % num_words, "Recognition Result")
200214
dlg.ShowModal()
201215

0 commit comments

Comments
 (0)