|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +# [START documentai_batch_parse_form_beta] |
| 17 | +from google.cloud import documentai_v1beta2 as documentai |
| 18 | +from google.cloud import storage |
| 19 | +import re |
| 20 | + |
| 21 | + |
| 22 | +def batch_parse_form( |
| 23 | + project_id='YOUR_PROJECT_ID', |
| 24 | + input_uri='gs://cloud-samples-data/documentai/form.pdf', |
| 25 | + destination_uri='gs://your-bucket-id/path/to/save/results/'): |
| 26 | + """Parse a form""" |
| 27 | + |
| 28 | + client = documentai.DocumentUnderstandingServiceClient() |
| 29 | + |
| 30 | + gcs_source = documentai.types.GcsSource(uri=input_uri) |
| 31 | + |
| 32 | + # mime_type can be application/pdf, image/tiff, |
| 33 | + # and image/gif, or application/json |
| 34 | + input_config = documentai.types.InputConfig( |
| 35 | + gcs_source=gcs_source, mime_type='application/pdf') |
| 36 | + |
| 37 | + # where to write results |
| 38 | + output_config = documentai.types.OutputConfig( |
| 39 | + gcs_destination=documentai.types.GcsDestination( |
| 40 | + uri=destination_uri), |
| 41 | + pages_per_shard=1 # Map one doc page to one output page |
| 42 | + ) |
| 43 | + |
| 44 | + # Improve form parsing results by providing key-value pair hints. |
| 45 | + # For each key hint, key is text that is likely to appear in the |
| 46 | + # document as a form field name (i.e. "DOB"). |
| 47 | + # Value types are optional, but can be one or more of: |
| 48 | + # ADDRESS, LOCATION, ORGANIZATION, PERSON, PHONE_NUMBER, ID, |
| 49 | + # NUMBER, EMAIL, PRICE, TERMS, DATE, NAME |
| 50 | + key_value_pair_hints = [ |
| 51 | + documentai.types.KeyValuePairHint( |
| 52 | + key='Emergency Contact', |
| 53 | + value_types=['NAME']), |
| 54 | + documentai.types.KeyValuePairHint( |
| 55 | + key='Referred By') |
| 56 | + ] |
| 57 | + |
| 58 | + # Setting enabled=True enables form extraction |
| 59 | + form_extraction_params = documentai.types.FormExtractionParams( |
| 60 | + enabled=True, key_value_pair_hints=key_value_pair_hints) |
| 61 | + |
| 62 | + # Location can be 'us' or 'eu' |
| 63 | + parent = 'projects/{}/locations/us'.format(project_id) |
| 64 | + request = documentai.types.ProcessDocumentRequest( |
| 65 | + input_config=input_config, |
| 66 | + output_config=output_config, |
| 67 | + form_extraction_params=form_extraction_params) |
| 68 | + |
| 69 | + # Add each ProcessDocumentRequest to the batch request |
| 70 | + requests = [] |
| 71 | + requests.append(request) |
| 72 | + |
| 73 | + batch_request = documentai.types.BatchProcessDocumentsRequest( |
| 74 | + parent=parent, requests=requests |
| 75 | + ) |
| 76 | + |
| 77 | + operation = client.batch_process_documents(batch_request) |
| 78 | + |
| 79 | + # Wait for the operation to finish |
| 80 | + operation.result() |
| 81 | + |
| 82 | + # Results are written to GCS. Use a regex to find |
| 83 | + # output files |
| 84 | + match = re.match(r'gs://([^/]+)/(.+)', destination_uri) |
| 85 | + output_bucket = match.group(1) |
| 86 | + prefix = match.group(2) |
| 87 | + |
| 88 | + storage_client = storage.client.Client() |
| 89 | + bucket = storage_client.get_bucket(output_bucket) |
| 90 | + blob_list = list(bucket.list_blobs(prefix=prefix)) |
| 91 | + print('Output files:') |
| 92 | + for blob in blob_list: |
| 93 | + print(blob.name) |
| 94 | + |
| 95 | + |
| 96 | +# [END documentai_batch_parse_form_beta] |
0 commit comments