31
31
project_id = os .environ ["GCP_PROJECT" ]
32
32
# [END functions_ocr_setup]
33
33
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
+
34
57
35
58
# [START functions_ocr_detect]
36
59
def detect_text (bucket : str , filename : str ) -> None :
@@ -57,10 +80,12 @@ def detect_text(bucket: str, filename: str) -> None:
57
80
)
58
81
text_detection_response = vision_client .text_detection (image = image )
59
82
annotations = text_detection_response .text_annotations
83
+
60
84
if len (annotations ) > 0 :
61
85
text = annotations [0 ].description
62
86
else :
63
87
text = ""
88
+
64
89
print (f"Extracted text { text } from image ({ len (text )} chars)." )
65
90
66
91
detect_language_response = translate_client .detect_language (text )
@@ -85,39 +110,8 @@ def detect_text(bucket: str, filename: str) -> None:
85
110
futures .append (future )
86
111
for future in futures :
87
112
future .result ()
88
-
89
-
90
113
# [END functions_ocr_detect]
91
114
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
-
121
115
122
116
# [START functions_ocr_process]
123
117
def process_image (file_info : dict , context : dict ) -> None :
@@ -136,16 +130,13 @@ def process_image(file_info: dict, context: dict) -> None:
136
130
137
131
detect_text (bucket , name )
138
132
139
- print ("File {} processed." .format (file_info ["name" ]))
140
-
141
-
133
+ print (f"File '{ file_info ['name' ]} ' processed." )
142
134
# [END functions_ocr_process]
143
135
144
136
145
137
# [START functions_ocr_translate]
146
138
def 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
149
140
a subscription.
150
141
151
142
Translates the text in the message from the specified source language
@@ -184,8 +175,6 @@ def translate_text(event: dict, context: dict) -> None:
184
175
topic_path = publisher .topic_path (project_id , topic_name )
185
176
future = publisher .publish (topic_path , data = encoded_message )
186
177
future .result ()
187
-
188
-
189
178
# [END functions_ocr_translate]
190
179
191
180
@@ -224,6 +213,4 @@ def save_result(event: dict, context: dict) -> None:
224
213
blob .upload_from_string (text )
225
214
226
215
print ("File saved." )
227
-
228
-
229
216
# [END functions_ocr_save]
0 commit comments