コード例 #1
0
 def test_training_auth_bad_key(self, formrecognizer_test_endpoint,
                                formrecognizer_test_api_key):
     client = FormTrainingClient(formrecognizer_test_endpoint,
                                 AzureKeyCredential("xxxx"))
     with self.assertRaises(ClientAuthenticationError):
         poller = client.begin_training("xx", use_training_labels=False)
コード例 #2
0
    def create_composed_model(self):
        # [START begin_create_composed_model]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer import FormTrainingClient

        endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
        key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
        po_supplies = os.environ['PURCHASE_ORDER_OFFICE_SUPPLIES_SAS_URL']
        po_equipment = os.environ['PURCHASE_ORDER_OFFICE_EQUIPMENT_SAS_URL']
        po_furniture = os.environ['PURCHASE_ORDER_OFFICE_FURNITURE_SAS_URL']
        po_cleaning_supplies = os.environ['PURCHASE_ORDER_OFFICE_CLEANING_SUPPLIES_SAS_URL']

        form_training_client = FormTrainingClient(endpoint=endpoint, credential=AzureKeyCredential(key))
        supplies_poller = form_training_client.begin_training(
            po_supplies, use_training_labels=True, model_name="Purchase order - Office supplies"
        )
        equipment_poller = form_training_client.begin_training(
            po_equipment, use_training_labels=True, model_name="Purchase order - Office Equipment"
        )
        furniture_poller = form_training_client.begin_training(
            po_furniture, use_training_labels=True, model_name="Purchase order - Furniture"
        )
        cleaning_supplies_poller = form_training_client.begin_training(
            po_cleaning_supplies, use_training_labels=True, model_name="Purchase order - Cleaning Supplies"
        )
        supplies_model = supplies_poller.result()
        equipment_model = equipment_poller.result()
        furniture_model = furniture_poller.result()
        cleaning_supplies_model = cleaning_supplies_poller.result()

        models_trained_with_labels = [
            supplies_model.model_id,
            equipment_model.model_id,
            furniture_model.model_id,
            cleaning_supplies_model.model_id
        ]

        poller = form_training_client.begin_create_composed_model(
            models_trained_with_labels, model_name="Office Supplies Composed Model"
        )
        model = poller.result()

        print("Office Supplies Composed Model Info:")
        print("Model ID: {}".format(model.model_id))
        print("Model name: {}".format(model.model_name))
        print("Is this a composed model?: {}".format(model.properties.is_composed_model))
        print("Status: {}".format(model.status))
        print("Composed model creation started on: {}".format(model.training_started_on))
        print("Creation completed on: {}".format(model.training_completed_on))

        # [END begin_create_composed_model]

        print("Recognized fields:")
        for submodel in model.submodels:
            print("The submodel has model ID: {}".format(submodel.model_id))
            print("...The submodel with form type {} has an average accuracy '{}'".format(
                submodel.form_type, submodel.accuracy
            ))
            for name, field in submodel.fields.items():
                print("...The model found the field '{}' with an accuracy of {}".format(
                    name, field.accuracy
                ))

        # Training result information
        for doc in model.training_documents:
            print("Document was used to train model with ID: {}".format(doc.model_id))
            print("Document name: {}".format(doc.name))
            print("Document status: {}".format(doc.status))
            print("Document page count: {}".format(doc.page_count))
            print("Document errors: {}".format(doc.errors))
コード例 #3
0
                        print("......Selection mark is '{}' and has a confidence of {}".format(
                            selection_mark.state,
                            selection_mark.confidence
                        ))

            print("-----------------------------------")
        # [END recognize_custom_forms]


if __name__ == '__main__':
    sample = RecognizeCustomForms()
    model_id = None
    if os.getenv("CONTAINER_SAS_URL_V2"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer import FormTrainingClient

        endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT")
        key = os.getenv("AZURE_FORM_RECOGNIZER_KEY")

        if not endpoint or not key:
            raise ValueError("Please provide endpoint and API key to run the samples.")

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key)
        )
        model = form_training_client.begin_training(os.getenv("CONTAINER_SAS_URL_V2"), use_training_labels=True).result()
        model_id = model.model_id

    sample.recognize_custom_forms(model_id)
    labeled_model_id = None
    unlabeled_model_id = None
    if os.getenv("CONTAINER_SAS_URL_WITH_LABELS_V2") or os.getenv(
            "CONTAINER_SAS_URL_WITHOUT_LABELS_V2"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer import FormTrainingClient

        endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT")
        key = os.getenv("AZURE_FORM_RECOGNIZER_KEY")
        labeled = os.getenv("CONTAINER_SAS_URL_WITH_LABELS_V2")
        unlabeled = os.getenv("CONTAINER_SAS_URL_WITHOUT_LABELS_V2")

        if not endpoint or not key:
            raise ValueError(
                "Please provide endpoint and API key to run the samples.")

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key))

        if labeled:
            model = form_training_client.begin_training(
                labeled, use_training_labels=True).result()
            labeled_model_id = model.model_id
        if unlabeled:
            model = form_training_client.begin_training(
                unlabeled, use_training_labels=False).result()
            unlabeled_model_id = model.model_id

    sample.recognize_custom_forms(labeled_model_id, unlabeled_model_id)
            for idx, items in enumerate(field.value):
                print("...Item #{}".format(idx + 1))
                for item_name, item in items.value.items():
                    print("......{}: {} has confidence {}".format(
                        item_name, item.value, item.confidence))
        else:
            print("{}: {} has confidence {}".format(name, field.value,
                                                    field.confidence))
# </snippet_receipts>

# <snippet_train>
# To train a model you need an Azure Storage account.
# Use the SAS URL to access your training files.
trainingDataUrl = "<SAS-URL-of-your-form-folder-in-blob-storage>"

poller = form_training_client.begin_training(trainingDataUrl,
                                             use_training_labels=False)
model = poller.result()

print("Model ID: {}".format(model.model_id))
print("Status: {}".format(model.status))
print("Training started on: {}".format(model.training_started_on))
print("Training completed on: {}".format(model.training_completed_on))

print("\nRecognized fields:")
for submodel in model.submodels:
    print(
        "The submodel with form type '{}' has recognized the following fields: {}"
        .format(
            submodel.form_type,
            ", ".join([
                field.label if field.label else name
コード例 #6
0
 def test_training_auth_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
     client = FormTrainingClient(form_recognizer_account, AzureKeyCredential("xxxx"))
     with self.assertRaises(ClientAuthenticationError):
         poller = client.begin_training("xx", use_training_labels=False)
コード例 #7
0
if __name__ == '__main__':
    sample = TestDifferentiateOutputLabeledTables()
    fixed_model_id = None
    dynamic_model_id = None
    if os.getenv("CONTAINER_SAS_URL_FIXED") or os.getenv("CONTAINER_SAS_URL_DYNAMIC"):

        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer import FormTrainingClient

        endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT")
        key = os.getenv("AZURE_FORM_RECOGNIZER_KEY")
        fixed = os.getenv("CONTAINER_SAS_URL_FIXED")
        dynamic = os.getenv("CONTAINER_SAS_URL_DYNAMIC")

        if not endpoint or not key:
            raise ValueError("Please provide endpoint and API key to run the samples.")

        form_training_client = FormTrainingClient(
            endpoint=endpoint, credential=AzureKeyCredential(key)
        )

        if fixed:
            model = form_training_client.begin_training(fixed, use_training_labels=True).result()
            fixed_model_id = model.model_id
        if dynamic:
            model = form_training_client.begin_training(dynamic, use_training_labels=True).result()
            dynamic_model_id = model.model_id

    sample.test_recognize_tables_fixed_rows(fixed_model_id)
    sample.test_recognize_tables_dynamic_rows(dynamic_model_id)