Skip to content

Commit 9b96290

Browse files
authored
Merge pull request #68 from aahill/ir-sdk
Ink Recognizer SDK documentation updates
2 parents e7c152c + 0a2be83 commit 9b96290

File tree

1 file changed

+52
-65
lines changed

1 file changed

+52
-65
lines changed

samples/vision/inkrecognizer_sample.py

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# <imports>
12
import os
23
try:
34
from tkinter import *
@@ -11,36 +12,28 @@
1112
from azure.cognitiveservices.inkrecognizer import ApplicationKind, InkStrokeKind
1213
from azure.cognitiveservices.inkrecognizer import InkRecognizerClient
1314

14-
1515
import logging
1616
logging.basicConfig(level=logging.DEBUG)
17+
# </imports>
1718

19+
# You can also use an Azure credential instance
1820
# <InkRecognizerClientConfig>
19-
URL = "https://api.cognitive.microsoft.com/inkrecognizer"
20-
CREDENTIAL = os.environ['INK_RECOGNIZER_SUBSCRIPTION_KEY'].strip()
21-
# You can also use Azure credential instance
22-
21+
INK_RECOGNIZER_URL = "https://api.cognitive.microsoft.com/inkrecognizer"
22+
KEY = os.environ['INK_RECOGNIZER_SUBSCRIPTION_KEY'].strip()
2323

24-
# Recognition Config
25-
# This tell Ink Recognizer Service that the sample is in en-US.
26-
# Default value is "en-US".
27-
# If "language" in a stroke is specified, this will be overlaped in that stroke.
24+
# The default locale is "en-US". Setting a different language for a stroke will overload this value.
2825
LANGUAGE_RECOGNITION_LOCALE = "en-US"
29-
# This tell Ink Recognizer Service that domain of the application is mixed,
30-
# so Ink Recognizer Service will detect kind of each stroke.
31-
# You can set it into ApplicationKind.WRITING or ApplicationKind.DRAWING to specify
32-
# default kind of strokes and skip stroke kind detection precedure.
33-
# Default value is ApplicationKind.MIXED.
34-
# If "kind" in a stroke is specified, this will be overlaped in that stroke.
26+
27+
# The default ApplicationKind is "MIXED". Specify the kind of strokes being sent to the API with different ApplicationKind values.
28+
# For example, ApplicationKind.WRITING or ApplicationKind.DRAWING
29+
3530
APPLICATION_KIND = ApplicationKind.MIXED
3631
# </InkRecognizerClientConfig>
3732

38-
39-
# <StrokeImplementations>
4033
# Shows simple implementation of InkPoint and InkStroke
34+
# <StrokeImplementations>
4135
InkPoint = namedtuple("InkPoint", "x y")
4236

43-
4437
class InkStroke():
4538
def __init__(self,
4639
ink_stroke_id,
@@ -53,26 +46,47 @@ def __init__(self,
5346
self.language = stroke_language
5447
# </StrokeImplementations>
5548

56-
57-
# <KeyScenarioExample>
5849
# Wrapper for InkRecognizerClient that shows how to
5950
# (1) Convert stroke unit from pixel to mm
6051
# (2) Set language recognition locale
6152
# (3) Indexing a key word from recognition results
6253
# (4) Set application kind if user know expected type of ink content
54+
55+
# <inkClient>
56+
class InkClient:
57+
def __init__(self, url, key):
58+
self._client = InkRecognizerClient(
59+
url,
60+
key,
61+
application_kind=APPLICATION_KIND, # default arguments for each request.
62+
)
63+
64+
def send_request(self, ink_stroke_list):
65+
self._root = None
66+
try:
67+
root = self._client.recognize_ink(
68+
ink_stroke_list, # Arguments for this request only.
69+
language=LANGUAGE_RECOGNITION_LOCALE,
70+
logging_enable=True
71+
)
72+
73+
result_text = []
74+
for word in root.ink_words:
75+
result_text.append(word.recognized_text)
76+
for shape in root.ink_drawings:
77+
result_text.append(shape.recognized_shape.value)
78+
result_text = "\n".join(result_text)
79+
return result_text
80+
self._root = root
81+
except Exception as e:
82+
messagebox.showinfo("Error", e)
83+
# </inkClient>
84+
85+
# <KeyScenarioExample>
6386
class RecognitionManager:
6487
def __init__(self, pixel_per_mm):
6588
self._pixel_per_mm = pixel_per_mm
66-
self._client = InkRecognizerClient(
67-
URL,
68-
CREDENTIAL,
69-
# <SetApplicationKind>
70-
application_kind=APPLICATION_KIND,
71-
# </SetApplicationKind>
72-
)
73-
# Aruments in constructor becomes default arguments for each request
74-
# You can also specify these arguments in recognize_ink() requests,
75-
# which influence that request only
89+
self._client = InkClient(INK_RECOGNIZER_URL, KEY)
7690
self.reset_ink()
7791

7892
def _reset_stroke(self):
@@ -86,65 +100,37 @@ def reset_ink(self):
86100
self._root = None
87101
self._reset_stroke()
88102

103+
# Convert from pixel to mm before adding to InkPoint.
89104
def add_point(self, x, y):
90-
# <UnitConversion>
91-
# Convert from pixel to mm before sending to InkPoint.
92-
# You can also specify keyword argument "unit_multiple" in
93-
# InkRecognizerClient constructor or in recognize_ink() request.
105+
94106
self._curr_stroke_points.append(
95107
InkPoint(self._pixel_to_mm(x), self._pixel_to_mm(y)))
96-
# </UnitConversion>
97108

98109
def stroke_end(self):
99110
stroke = InkStroke(len(self._ink_stroke_list), self._curr_stroke_points)
100111
self._ink_stroke_list.append(stroke)
101112
self._reset_stroke()
102113

103114
def recognize(self):
104-
self._root = None
105-
try:
106-
root = self._client.recognize_ink(
107-
self._ink_stroke_list,
108-
# <SetRecognitionLocale>
109-
language=LANGUAGE_RECOGNITION_LOCALE,
110-
# </SetRecognitionLocale>
111-
logging_enable=True
112-
)
113-
# Aruments in request is for this request only
114-
# You can also specify these arguments in InkRecognizerClient constructor,
115-
# which will be default arguments for each call.
116-
result_text = []
117-
for word in root.ink_words:
118-
result_text.append(word.recognized_text)
119-
for shape in root.ink_drawings:
120-
result_text.append(shape.recognized_shape.value)
121-
result_text = "\n".join(result_text)
122-
messagebox.showinfo("Result", result_text)
123-
self._root = root
124-
except Exception as e:
125-
messagebox.showinfo("Error", e)
126-
115+
result_text = self._client.send_request(self._ink_stroke_list)
116+
messagebox.showinfo("Result", result_text)
117+
127118
def search(self, word):
128-
# <IndexingKeyword>
129119
if self._root is not None:
130120
num_words = len(self._root.find_word(word))
131121
else:
132122
num_words = 0
133123
search_result = "Find %s word%s" % (num_words, "s" if num_words != 1 else "")
134124
messagebox.showinfo("Search Result", search_result)
135-
# </IndexingKeyword>
136125
# </KeyScenarioExample>
137126

138127

139-
# <SampleUIConfig>
128+
# <SampleUI>
140129
CANVAS_WIDTH = 800
141130
CANVAS_HEIGHT = 500
142131
STROKE_COLOR = "#476042" # python green
143132
STROKE_WIDTH = 3
144-
# </SampleUIConfig>
145133

146-
147-
# <SampleUI>
148134
class InkRecognizerDemo:
149135
def __init__(self):
150136
self._master = Tk()
@@ -213,7 +199,8 @@ def run(self):
213199
mainloop()
214200
# </SampleUI>
215201

216-
202+
# <entrypoint>
217203
if __name__ == "__main__":
218204
demo = InkRecognizerDemo()
219205
demo.run()
206+
# </entrypoint>

0 commit comments

Comments
 (0)