def test_authentication_bad_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): client = FormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential("xxxx")) with self.assertRaises(ClientAuthenticationError): poller = client.begin_recognize_id_documents( b"xx", content_type="image/jpeg")
def recognize_id_documents(self): path_to_sample_forms = os.path.abspath( os.path.join(os.path.abspath(__file__), "..", "./sample_forms/id_documents/license.jpg")) # [START recognize_id_documents] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer import FormRecognizerClient endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] form_recognizer_client = FormRecognizerClient( endpoint=endpoint, credential=AzureKeyCredential(key)) with open(path_to_sample_forms, "rb") as f: poller = form_recognizer_client.begin_recognize_id_documents( id_document=f) id_documents = poller.result() for idx, id_document in enumerate(id_documents): print("--------Recognizing ID document #{}--------".format(idx + 1)) first_name = id_document.fields.get("FirstName") if first_name: print("First Name: {} has confidence: {}".format( first_name.value, first_name.confidence)) last_name = id_document.fields.get("LastName") if last_name: print("Last Name: {} has confidence: {}".format( last_name.value, last_name.confidence)) document_number = id_document.fields.get("DocumentNumber") if document_number: print("Document Number: {} has confidence: {}".format( document_number.value, document_number.confidence)) dob = id_document.fields.get("DateOfBirth") if dob: print("Date of Birth: {} has confidence: {}".format( dob.value, dob.confidence)) doe = id_document.fields.get("DateOfExpiration") if doe: print("Date of Expiration: {} has confidence: {}".format( doe.value, doe.confidence)) sex = id_document.fields.get("Sex") if sex: print("Sex: {} has confidence: {}".format( sex.value, sex.confidence)) address = id_document.fields.get("Address") if address: print("Address: {} has confidence: {}".format( address.value, address.confidence)) country = id_document.fields.get("Country") if country: print("Country: {} has confidence: {}".format( country.value, country.confidence)) region = id_document.fields.get("Region") if region: print("Region: {} has confidence: {}".format( region.value, region.confidence))
def test_id_document_bad_endpoint(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): with open(self.id_document_jpg, "rb") as fd: myfile = fd.read() with self.assertRaises(ServiceRequestError): client = FormRecognizerClient( "http://notreal.azure.com", AzureKeyCredential(formrecognizer_test_api_key)) poller = client.begin_recognize_id_documents(myfile)
# </snippet_manage_getmodel> # <snippet_manage_delete> form_training_client.delete_model(model_id=custom_model.model_id) try: form_training_client.get_custom_model(model_id=custom_model.model_id) except ResourceNotFoundError: print("Successfully deleted model with id {}".format( custom_model.model_id)) # </snippet_manage_delete> # <snippet_id> idURL = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/id-license.jpg" poller = form_recognizer_client.begin_recognize_id_documents(idURL) id_documents = poller.result() for idx, id_document in enumerate(id_documents): print("--------Recognizing ID document #{}--------".format(idx + 1)) first_name = id_document.fields.get("FirstName") if first_name: print("First Name: {} has confidence: {}".format( first_name.value, first_name.confidence)) last_name = id_document.fields.get("LastName") if last_name: print("Last Name: {} has confidence: {}".format( last_name.value, last_name.confidence)) document_number = id_document.fields.get("DocumentNumber") if document_number: print("Document Number: {} has confidence: {}".format(