1212from azure .cognitiveservices .inkrecognizer import InkRecognizerClient
1313
1414
15- # Ink Recognizer Client Config
15+ import logging
16+ logging .basicConfig (level = logging .DEBUG )
17+
18+ # <InkRecognizerClientConfig>
1619URL = "https://api.cognitive.microsoft.com/inkrecognizer"
1720CREDENTIAL = os .environ ['INK_RECOGNIZER_SUBSCRIPTION_KEY' ].strip ()
1821# You can also use Azure credential instance
3033# Default value is ApplicationKind.MIXED.
3134# If "kind" in a stroke is specified, this will be overlaped in that stroke.
3235APPLICATION_KIND = ApplicationKind .MIXED
36+ # </InkRecognizerClientConfig>
3337
3438
35- # UI Config
36- CANVAS_WIDTH = 800
37- CANVAS_HEIGHT = 500
38- STROKE_COLOR = "#476042" # python green
39- STROKE_WIDTH = 3
40-
41-
42- # Stroke Implementations
39+ # <StrokeImplementations>
4340# Shows simple implementation of InkPoint and InkStroke
4441InkPoint = namedtuple ("InkPoint" , "x y" )
4542
@@ -54,17 +51,28 @@ def __init__(self,
5451 self .points = ink_points
5552 self .kind = stroke_kind
5653 self .language = stroke_language
54+ # </StrokeImplementations>
5755
5856
59- # Sample wrapper for InkRecognizerClient that shows how to
57+ # <KeyScenarioExample>
58+ # Wrapper for InkRecognizerClient that shows how to
6059# (1) Convert stroke unit from pixel to mm
6160# (2) Set language recognition locale
6261# (3) Indexing a key word from recognition results
6362# (4) Set application kind if user know expected type of ink content
6463class RecognitionManager :
6564 def __init__ (self , pixel_per_mm ):
6665 self ._pixel_per_mm = pixel_per_mm
67- self ._client = InkRecognizerClient (URL , CREDENTIAL )
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
6876 self .reset_ink ()
6977
7078 def _reset_stroke (self ):
@@ -79,11 +87,13 @@ def reset_ink(self):
7987 self ._reset_stroke ()
8088
8189 def add_point (self , x , y ):
90+ # <UnitConversion>
8291 # Convert from pixel to mm before sending to InkPoint.
8392 # You can also specify keyword argument "unit_multiple" in
8493 # InkRecognizerClient constructor or in recognize_ink() request.
8594 self ._curr_stroke_points .append (
8695 InkPoint (self ._pixel_to_mm (x ), self ._pixel_to_mm (y )))
96+ # </UnitConversion>
8797
8898 def stroke_end (self ):
8999 stroke = InkStroke (len (self ._ink_stroke_list ), self ._curr_stroke_points )
@@ -95,10 +105,10 @@ def recognize(self):
95105 try :
96106 root = self ._client .recognize_ink (
97107 self ._ink_stroke_list ,
98- # Pre-set recognition type
99- application_kind = APPLICATION_KIND ,
100- # Set language recognition locale
101- language = LANGUAGE_RECOGNITION_LOCALE
108+ # <SetRecognitionLocale>
109+ language = LANGUAGE_RECOGNITION_LOCALE ,
110+ # </SetRecognitionLocale>
111+ logging_enable = True
102112 )
103113 # Aruments in request is for this request only
104114 # You can also specify these arguments in InkRecognizerClient constructor,
@@ -115,15 +125,26 @@ def recognize(self):
115125 messagebox .showinfo ("Error" , e )
116126
117127 def search (self , word ):
118- # Indexing a key word from recognition results
128+ # <IndexingKeyword>
119129 if self ._root is not None :
120- words = self ._root .find_word (word )
121- messagebox .showinfo ("Search Result" , "Find %s words" % len (words ))
130+ num_words = len (self ._root .find_word (word ))
122131 else :
123- messagebox .showinfo ("Search Result" , "Find %s words" % 0 )
132+ num_words = 0
133+ search_result = "Find %s word%s" % (num_words , "s" if num_words != 1 else "" )
134+ messagebox .showinfo ("Search Result" , search_result )
135+ # </IndexingKeyword>
136+ # </KeyScenarioExample>
137+
138+
139+ # <SampleUIConfig>
140+ CANVAS_WIDTH = 800
141+ CANVAS_HEIGHT = 500
142+ STROKE_COLOR = "#476042" # python green
143+ STROKE_WIDTH = 3
144+ # </SampleUIConfig>
124145
125146
126- # Sample UI
147+ # <SampleUI>
127148class InkRecognizerDemo :
128149 def __init__ (self ):
129150 self ._master = Tk ()
@@ -190,6 +211,7 @@ def _search(self):
190211
191212 def run (self ):
192213 mainloop ()
214+ # </SampleUI>
193215
194216
195217if __name__ == "__main__" :
0 commit comments