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_identity_documents(
             b"xx", content_type="image/jpeg")
 def test_identity_document_bad_endpoint(self, formrecognizer_test_api_key):
     with open(self.identity_document_license_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_identity_documents(myfile)
    def recognize_identity_documents(self):
        path_to_sample_forms = os.path.abspath(
            os.path.join(os.path.abspath(__file__), "..", "..",
                         "./sample_forms/id_documents/license.jpg"))

        # [START recognize_identity_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_identity_documents(
                identity_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_region = id_document.fields.get("CountryRegion")
            if country_region:
                print("Country/Region: {} has confidence: {}".format(
                    country_region.value, country_region.confidence))
            region = id_document.fields.get("Region")
            if region:
                print("Region: {} has confidence: {}".format(
                    region.value, region.confidence))
def convert_to_and_from_dict():
    path_to_sample_forms = os.path.abspath(
        os.path.join(
            os.path.abspath(__file__),
            "..",
            "..",
            "./sample_forms/id_documents/license.jpg",
        ))

    from azure.core.serialization import AzureJSONEncoder
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import FormRecognizerClient, RecognizedForm

    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_identity_documents(
            identity_document=f)

    id_documents = poller.result()

    # convert the received model to a dictionary
    recognized_form_dict = [doc.to_dict() for doc in id_documents]

    # save the dictionary as JSON content in a JSON file, use the AzureJSONEncoder
    # to help make types, such as dates, JSON serializable
    # NOTE: AzureJSONEncoder is only available with azure.core>=1.18.0.
    with open('data.json', 'w') as f:
        json.dump(recognized_form_dict, f, cls=AzureJSONEncoder)

    # convert the dictionary back to the original model
    model = [RecognizedForm.from_dict(doc) for doc in recognized_form_dict]

    # use the model as normal
    for idx, id_document in enumerate(model):
        print("--------Recognizing converted 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))

    print("----------------------------------------")
Example #5
0
# </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"

with open(path_to_sample_forms, "rb") as f:
            poller = form_recognizer_client.begin_recognize_identity_documents(idURL=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: