Package: amazon-textract-geofinder
Textract-Pipeline-GeoFinderProvides functions to use geometric information to extract information.Use cases include:Give context to key/value pairs from the Amazon Textract AnalyzeDocument API for FORMSFind values in specific areasInstall>python-mpipinstallamazon-textract-geofinderMake sure your environment is setup with AWS credentials through configuration files or environment variables or an attached role. (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)ConceptTo find information in a document based on geometry with this library the main advantage over defining x,y coordinates where the expected value should be is the concept of an area.An area is ultimately defined by a box with x_min, y_min, x_max, y_max coordinates but can be defined by finding words/phrases in the document and then use to create the area.From there functions to parse the information in the area help to extract the information. E. g. by defining the area based on the question like 'Did you feel fever or feverish lately?' we can associate the answers to it and create a new key/value pair specific to this question.SamplesGet context for key value pairsSample image:The Amazon Textract AnalyzeDocument API with the FORMS feature returns the following keys:KeyValueFirst Name:ALEJANDROFirst Name:CARLOSRelationship to Patient:BROTHERFirst Name:JANEMarital Status:MARRIEDPhone:646-555-0111Last Name:SALAZARPhone:212-555-0150Relationship to Patient:FRIENDLast Name:ROSALEZCity:ANYTOWNPhone:650-555-0123Address:123 ANY STREETYesSELECTEDYesNOT_SELECTEDDate of Birth:10/10/1982Last Name:DOESex:MYesNOT_SELECTEDYesNOT_SELECTEDYesNOT_SELECTEDState:CAZip Code:12345Email Address:NoNOT_SELECTEDNoSELECTEDNoNOT_SELECTEDYesSELECTEDNoSELECTEDNoSELECTEDNoSELECTEDBut the information to which section of the document the individual keys belong is not obvious. Most keys appear multiple times and we want to give them context to associate them with the 'Patient', 'Emergency Contact 1', 'Emergency Contact 2' or specific questions.This Jupyter notebook that walks through the sample:sample notebookMake sure to have AWS credentials setup when starting the notebook locally or use a SageMaker notebook with a role including permissions for Amazon Textract.This code snippet is take from the notebook.python-mpipinstallamazon-textract-helperamazon-textract-geofinderfromtextractgeofinder.ocrdbimportAreaSelectionfromtextractgeofinder.tgeofinderimportKeyValue,TGeoFinder,AreaSelection,SelectionElementfromtextractprettyprinter.t_pretty_printimportget_forms_stringfromtextractcallerimportcall_textractfromtextractcaller.t_callimportTextract_Featuresimporttrp.trp2ast2image_filename='./tests/data/patient_intake_form_sample.jpg'j=call_textract(input_document=image_filename,features=[Textract_Features.FORMS])t_document=t2.TDocumentSchema().load(j)doc_height=1000doc_width=1000geofinder_doc=TGeoFinder(j,doc_height=doc_height,doc_width=doc_width)defset_hierarchy_kv(list_kv:list[KeyValue],t_document:t2.TDocument,page_block:t2.TBlock,prefix="BORROWER"):forxinlist_kv:t_document.add_virtual_key_for_existing_key(key_name=f"{prefix}_{x.key.text}",existing_key=t_document.get_block_by_id(x.key.id),page_block=page_block)# patient informationpatient_information=geofinder_doc.find_phrase_on_page("patient information")[0]emergency_contact_1=geofinder_doc.find_phrase_on_page("emergency contact 1:",min_textdistance=0.99)[0]top_left=t2.TPoint(y=patient_information.ymax,x=0)lower_right=t2.TPoint(y=emergency_contact_1.ymin,x=doc_width)form_fields=geofinder_doc.get_form_fields_in_area(area_selection=AreaSelection(top_left=top_left,lower_right=lower_right))set_hierarchy_kv(list_kv=form_fields,t_document=t_document,prefix='PATIENT',page_block=t_document.pages[0])set_hierarchy_kv(list_kv=form_fields,t_document=t_document,prefix='PATIENT',page_block=t_document.pages[0])print(get_forms_string(t2.TDocumentSchema().dump(t_document)))KeyValue......PATIENT_first name:ALEJANDROPATIENT_address:123 ANY STREETPATIENT_sex:MPATIENT_state:CAPATIENT_zip code:12345PATIENT_marital status:MARRIEDPATIENT_last name:ROSALEZPATIENT_phone:646-555-0111PATIENT_email address:PATIENT_city:ANYTOWNPATIENT_date of birth:10/10/1982Using the Amazon Textact Helper command line tool with the sampleThis will show the full result, like the notebook.>python-mpipinstallamazon-textract-helperamazon-textract-geofinder
>cattests/data/patient_intake_form_sample.json|bin/amazon-textract-geofinder|amazon-textract--stdin--pretty-printFORMSKeyValueFirst Name:ALEJANDROFirst Name:CARLOSRelationship to Patient:BROTHERFirst Name:JANEMarital Status:MARRIEDPhone:646-555-0111Last Name:SALAZARPhone:212-555-0150Relationship to Patient:FRIENDLast Name:ROSALEZCity:ANYTOWNPhone:650-555-0123Address:123 ANY STREETYesSELECTEDYesNOT_SELECTEDDate of Birth:10/10/1982Last Name:DOESex:MYesNOT_SELECTEDYesNOT_SELECTEDYesNOT_SELECTEDState:CAZip Code:12345Email Address:NoNOT_SELECTEDNoSELECTEDNoNOT_SELECTEDYesSELECTEDNoSELECTEDNoSELECTEDNoSELECTEDPATIENT_first name:ALEJANDROPATIENT_address:123 ANY STREETPATIENT_sex:MPATIENT_state:CAPATIENT_zip code:12345PATIENT_marital status:MARRIEDPATIENT_last name:ROSALEZPATIENT_phone:646-555-0111PATIENT_email address:PATIENT_city:ANYTOWNPATIENT_date of birth:10/10/1982EMERGENCY_CONTACT_1_first name:CARLOSEMERGENCY_CONTACT_1_phone:212-555-0150EMERGENCY_CONTACT_1_relationship to patient:BROTHEREMERGENCY_CONTACT_1_last name:SALAZAREMERGENCY_CONTACT_2_first name:JANEEMERGENCY_CONTACT_2_phone:650-555-0123EMERGENCY_CONTACT_2_last name:DOEEMERGENCY_CONTACT_2_relationship to patient:FRIENDFEVER->YESSELECTEDFEVER->NONOT_SELECTEDSHORTNESS->YESNOT_SELECTEDSHORTNESS->NOSELECTEDCOUGH->YESNOT_SELECTEDCOUGH->NOSELECTEDLOSS_OF_TASTE->YESNOT_SELECTEDLOSS_OF_TASTE->NOSELECTEDCOVID_CONTACT->YESSELECTEDCOVID_CONTACT->NONOT_SELECTEDTRAVEL->YESNOT_SELECTEDTRAVEL->NOSELECTED