3131project_id = os .environ ["GCP_PROJECT" ]
3232# [END functions_ocr_setup]
3333
34+ T = TypeVar ("T" )
35+
36+
37+ def validate_message (message : Dict [str , T ], param : str ) -> T :
38+ """
39+ Placeholder function for validating message parts.
40+
41+ Args:
42+ message: message to be validated.
43+ param: name of the message parameter to be validated.
44+
45+ Returns:
46+ The value of message['param'] if it's valid. Throws ValueError
47+ if it's not valid.
48+ """
49+ var = message .get (param )
50+ if not var :
51+ raise ValueError (
52+ f"{ param } is not provided. Make sure you have "
53+ f"property { param } in the request"
54+ )
55+ return var
56+
3457
3558# [START functions_ocr_detect]
3659def detect_text (bucket : str , filename : str ) -> None :
@@ -57,10 +80,12 @@ def detect_text(bucket: str, filename: str) -> None:
5780 )
5881 text_detection_response = vision_client .text_detection (image = image )
5982 annotations = text_detection_response .text_annotations
83+
6084 if len (annotations ) > 0 :
6185 text = annotations [0 ].description
6286 else :
6387 text = ""
88+
6489 print (f"Extracted text { text } from image ({ len (text )} chars)." )
6590
6691 detect_language_response = translate_client .detect_language (text )
@@ -85,39 +110,8 @@ def detect_text(bucket: str, filename: str) -> None:
85110 futures .append (future )
86111 for future in futures :
87112 future .result ()
88-
89-
90113# [END functions_ocr_detect]
91114
92- T = TypeVar ("T" )
93-
94-
95- # [START message_validatation_helper]
96- def validate_message (message : Dict [str , T ], param : str ) -> T :
97- """
98- Placeholder function for validating message parts.
99-
100- Args:
101- message: message to be validated.
102- param: name of the message parameter to be validated.
103-
104- Returns:
105- The value of message['param'] if it's valid. Throws ValueError
106- if it's not valid.
107- """
108- var = message .get (param )
109- if not var :
110- raise ValueError (
111- "{} is not provided. Make sure you have \
112- property {} in the request" .format (
113- param , param
114- )
115- )
116- return var
117-
118-
119- # [END message_validatation_helper]
120-
121115
122116# [START functions_ocr_process]
123117def process_image (file_info : dict , context : dict ) -> None :
@@ -136,16 +130,13 @@ def process_image(file_info: dict, context: dict) -> None:
136130
137131 detect_text (bucket , name )
138132
139- print ("File {} processed." .format (file_info ["name" ]))
140-
141-
133+ print (f"File '{ file_info ['name' ]} ' processed." )
142134# [END functions_ocr_process]
143135
144136
145137# [START functions_ocr_translate]
146138def translate_text (event : dict , context : dict ) -> None :
147- """
148- Cloud Function triggered by PubSub when a message is received from
139+ """Cloud Function triggered by PubSub when a message is received from
149140 a subscription.
150141
151142 Translates the text in the message from the specified source language
@@ -184,8 +175,6 @@ def translate_text(event: dict, context: dict) -> None:
184175 topic_path = publisher .topic_path (project_id , topic_name )
185176 future = publisher .publish (topic_path , data = encoded_message )
186177 future .result ()
187-
188-
189178# [END functions_ocr_translate]
190179
191180
@@ -224,6 +213,4 @@ def save_result(event: dict, context: dict) -> None:
224213 blob .upload_from_string (text )
225214
226215 print ("File saved." )
227-
228-
229216# [END functions_ocr_save]
0 commit comments