Beispiel #1
0
 def test_custom_form_bad_endpoint(self, resource_group, location,
                                   form_recognizer_account,
                                   form_recognizer_account_key):
     with open(self.form_jpg, "rb") as fd:
         myfile = fd.read()
     with self.assertRaises(ServiceRequestError):
         client = FormRecognizerClient(
             "http://notreal.azure.com",
             AzureKeyCredential(form_recognizer_account_key))
         poller = client.begin_recognize_custom_forms(model_id="xx",
                                                      form=myfile)
Beispiel #2
0
    def test_receipt_stream_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))

        responses = []

        def callback(raw_response, _, headers):
            analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response)
            extracted_receipt = prepare_receipt(analyze_result)
            responses.append(analyze_result)
            responses.append(extracted_receipt)

        with open(self.receipt_jpg, "rb") as fd:
            myfile = fd.read()

        poller = client.begin_recognize_receipts(
            receipt=myfile,
            include_text_content=True,
            cls=callback
        )

        result = poller.result()
        raw_response = responses[0]
        returned_model = responses[1]
        receipt = returned_model[0]
        actual = raw_response.analyze_result.document_results[0].fields
        read_results = raw_response.analyze_result.read_results
        document_results = raw_response.analyze_result.document_results
        page_results = raw_response.analyze_result.page_results

        # check dict values
        self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantPhoneNumber"), actual.get("MerchantPhoneNumber"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("Subtotal"), actual.get("Subtotal"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("Tax"), actual.get("Tax"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("Tip"), actual.get("Tip"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("Total"), actual.get("Total"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("TransactionDate"), actual.get("TransactionDate"), read_results)
        self.assertFormFieldTransformCorrect(receipt.fields.get("TransactionTime"), actual.get("TransactionTime"), read_results)

        # check page range
        self.assertEqual(receipt.page_range.first_page_number, document_results[0].page_range[0])
        self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1])

        # check receipt type
        receipt_type = receipt.fields.get("ReceiptType")
        self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence)
        self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string)

        # check receipt items
        self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results)

        # Check form pages
        self.assertFormPagesTransformCorrect(receipt.pages, read_results)
    def test_passing_unsupported_url_content_type(self, resource_group,
                                                  location,
                                                  form_recognizer_account,
                                                  form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))

        with self.assertRaises(TypeError):
            poller = client.begin_recognize_receipts(
                "https://badurl.jpg", content_type="application/json")
Beispiel #4
0
 def test_damaged_file_passed_as_bytes_io(self, resource_group, location,
                                          form_recognizer_account,
                                          form_recognizer_account_key):
     client = FormRecognizerClient(
         form_recognizer_account,
         AzureKeyCredential(form_recognizer_account_key))
     damaged_pdf = BytesIO(
         b"\x25\x50\x44\x46\x55\x55\x55"
     )  # still has correct bytes to be recognized as PDF
     with self.assertRaises(HttpResponseError):
         poller = client.begin_recognize_receipts(damaged_pdf, )
 def test_passing_bad_content_type_param_passed(
         self, resource_group, location, form_recognizer_account,
         form_recognizer_account_key):
     client = FormRecognizerClient(
         form_recognizer_account,
         AzureKeyCredential(form_recognizer_account_key))
     with open(self.receipt_jpg, "rb") as fd:
         myfile = fd.read()
     with self.assertRaises(ValueError):
         poller = client.begin_recognize_receipts(
             myfile, content_type="application/jpeg")
Beispiel #6
0
    def recognize_receipts_from_url(self):
        # [START recognize_receipts_from_url]
        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)
        )
        url = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"
        poller = form_recognizer_client.begin_recognize_receipts_from_url(receipt_url=url)
        receipts = poller.result()

        for idx, receipt in enumerate(receipts):
            print("--------Recognizing receipt #{}--------".format(idx+1))
            receipt_type = receipt.fields.get("ReceiptType")
            if receipt_type:
                print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence))
            merchant_name = receipt.fields.get("MerchantName")
            if merchant_name:
                print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence))
            transaction_date = receipt.fields.get("TransactionDate")
            if transaction_date:
                print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
            print("Receipt items:")
            for idx, item in enumerate(receipt.fields.get("Items").value):
                print("...Item #{}".format(idx+1))
                item_name = item.value.get("Name")
                if item_name:
                    print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
                item_quantity = item.value.get("Quantity")
                if item_quantity:
                    print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
                item_price = item.value.get("Price")
                if item_price:
                    print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
                item_total_price = item.value.get("TotalPrice")
                if item_total_price:
                    print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
            subtotal = receipt.fields.get("Subtotal")
            if subtotal:
                print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))
            tax = receipt.fields.get("Tax")
            if tax:
                print("Tax: {} has confidence: {}".format(tax.value, tax.confidence))
            tip = receipt.fields.get("Tip")
            if tip:
                print("Tip: {} has confidence: {}".format(tip.value, tip.confidence))
            total = receipt.fields.get("Total")
            if total:
                print("Total: {} has confidence: {}".format(total.value, total.confidence))
            print("--------------------------------------")
Beispiel #7
0
    def test_blank_page(self, resource_group, location,
                        form_recognizer_account, form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))

        with open(self.blank_pdf, "rb") as fd:
            blank = fd.read()
        poller = client.begin_recognize_receipts(blank, )
        result = poller.result()
        self.assertIsNotNone(result)
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("----------------------------------------")
Beispiel #9
0
 def test_passing_unsupported_url_content_type(self,
                                               formrecognizer_test_endpoint,
                                               formrecognizer_test_api_key):
     client = FormRecognizerClient(
         formrecognizer_test_endpoint,
         AzureKeyCredential(formrecognizer_test_api_key))
     with self.assertRaises(TypeError):
         poller = client.begin_recognize_custom_forms(
             model_id="xx",
             form="https://badurl.jpg",
             content_type="application/json")
Beispiel #10
0
    def test_pass_stream_into_url(self, formrecognizer_test_endpoint,
                                  formrecognizer_test_api_key):
        client = FormRecognizerClient(
            formrecognizer_test_endpoint,
            AzureKeyCredential(formrecognizer_test_api_key))

        with open(self.unsupported_content_py, "rb") as fd:
            with self.assertRaises(HttpResponseError):
                poller = client.begin_recognize_custom_forms_from_url(
                    model_id="xxx",
                    form_url=fd,
                )
Beispiel #11
0
    def test_receipt_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))

        with open(self.receipt_jpg, "rb") as fd:
            receipt = fd.read()

        initial_poller = client.begin_recognize_receipts(receipt)
        cont_token = initial_poller.continuation_token()
        poller = client.begin_recognize_receipts(receipt, continuation_token=cont_token)
        result = poller.result()
        self.assertIsNotNone(result)
        initial_poller.wait()  # necessary so azure-devtools doesn't throw assertion error
Beispiel #12
0
 def test_passing_enum_content_type(self, resource_group, location,
                                    form_recognizer_account,
                                    form_recognizer_account_key):
     client = FormRecognizerClient(
         form_recognizer_account,
         AzureKeyCredential(form_recognizer_account_key))
     with open(self.invoice_pdf, "rb") as fd:
         myfile = fd.read()
     poller = client.begin_recognize_content(
         myfile, content_type=FormContentType.application_pdf)
     result = poller.result()
     self.assertIsNotNone(result)
    def test_content_url_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        client = FormRecognizerClient(form_recognizer_account,
                                      AzureKeyCredential(form_recognizer_account_key))

        poller = client.begin_recognize_content_from_url(self.invoice_url_pdf)
        result = poller.result()
        self.assertEqual(len(result), 1)
        layout = result[0]
        self.assertEqual(layout.page_number, 1)
        self.assertFormPagesHasValues(result)
        self.assertEqual(layout.tables[0].row_count, 2)
        self.assertEqual(layout.tables[0].column_count, 6)
Beispiel #14
0
 def test_custom_forms_encoded_url(self, formrecognizer_test_endpoint,
                                   formrecognizer_test_api_key):
     client = FormRecognizerClient(
         formrecognizer_test_endpoint,
         AzureKeyCredential(formrecognizer_test_api_key))
     try:
         poller = client.begin_recognize_custom_forms_from_url(
             model_id="00000000-0000-0000-0000-000000000000",
             form_url="https://fakeuri.com/blank%20space")
     except HttpResponseError as e:
         self.assertIn("https://fakeuri.com/blank%20space",
                       e.response.request.body)
Beispiel #15
0
    def test_content_multipage_url(self, resource_group, location,
                                   form_recognizer_account,
                                   form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))
        poller = client.begin_recognize_content_from_url(
            self.multipage_url_pdf)
        result = poller.result()

        self.assertEqual(len(result), 3)
        self.assertFormPagesHasValues(result)
    def recognize_content(self):
        path_to_sample_forms = os.path.abspath(
            os.path.join(os.path.abspath(__file__), "..",
                         "./sample_forms/forms/form_selection_mark.png"))
        # [START recognize_content]
        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_content(form=f)
        form_pages = poller.result()

        for idx, content in enumerate(form_pages):
            print("----Recognizing content from page #{}----".format(idx + 1))
            print("Page has width: {} and height: {}, measured with unit: {}".
                  format(content.width, content.height, content.unit))
            for table_idx, table in enumerate(content.tables):
                print("Table # {} has {} rows and {} columns".format(
                    table_idx, table.row_count, table.column_count))
                print("Table # {} location on page: {}".format(
                    table_idx, format_bounding_box(table.bounding_box)))
                for cell in table.cells:
                    print(
                        "...Cell[{}][{}] has text '{}' within bounding box '{}'"
                        .format(cell.row_index, cell.column_index, cell.text,
                                format_bounding_box(cell.bounding_box)))

            for line_idx, line in enumerate(content.lines):
                print(
                    "Line # {} has word count '{}' and text '{}' within bounding box '{}'"
                    .format(line_idx, len(line.words), line.text,
                            format_bounding_box(line.bounding_box)))
                if line.appearance:
                    if line.appearance.style_name == "handwriting" and line.appearance.style_confidence > 0.8:
                        print(
                            "Text line '{}' is handwritten and might be a signature."
                            .format(line.text))
                for word in line.words:
                    print("...Word '{}' has a confidence of {}".format(
                        word.text, word.confidence))

            for selection_mark in content.selection_marks:
                print(
                    "Selection mark is '{}' within bounding box '{}' and has a confidence of {}"
                    .format(selection_mark.state,
                            format_bounding_box(selection_mark.bounding_box),
                            selection_mark.confidence))
            print("----------------------------------------")
Beispiel #17
0
    def recognize_custom_forms(self):
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.formrecognizer import FormRecognizerClient
        form_recognizer_client = FormRecognizerClient(
            endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
        )

        # Make sure your form's type is included in the list of form types the custom model can recognize
        with open("sample_forms/forms/Form_1.jpg", "rb") as f:
            stream = f.read()
        forms_with_labeled_model_poller = form_recognizer_client.begin_recognize_custom_forms(
            model_id=self.model_trained_with_labels_id, stream=stream
        )
        forms_with_unlabeled_model_poller = form_recognizer_client.begin_recognize_custom_forms(
            model_id=self.model_trained_without_labels_id, stream=stream
        )

        # Calling result after kicking off each call allows for server-side paralellization
        forms_with_labeled_model = forms_with_labeled_model_poller.result()
        forms_with_unlabeled_model = forms_with_unlabeled_model_poller.result()


        # With a form recognized by a model trained with labels, this 'name' key will be its
        # training-time label, otherwise it will be denoted by numeric indices.
        # Label data is not returned for model trained with labels.
        print("---------Recognizing forms with models trained with labeled data---------")
        for labeled_form in forms_with_labeled_model:
            for name, field in labeled_form.fields.items():
                print("...Field '{}' has value '{}' based on '{}' within bounding box '{}', with a confidence score of {}".format(
                    name,
                    field.value,
                    field.value_data.text,
                    format_bounding_box(field.value_data.bounding_box),
                    field.confidence
                ))

        print("-----------------------------------------------------------------------")
        print("-------Recognizing forms with models trained with unlabeled data-------")
        for unlabeled_form in forms_with_unlabeled_model:
            for name, field in unlabeled_form.fields.items():
                print("...Field '{}' has label '{}' within bounding box '{}', with a confidence score of {}".format(
                    name,
                    field.label_data.text,
                    format_bounding_box(field.label_data.bounding_box),
                    field.confidence
                ))
                print("...Field '{}' has value '{}' based on '{}' within bounding box '{}', with a confidence score of {}".format(
                    name,
                    field.value,
                    field.value_data.text,
                    format_bounding_box(field.value_data.bounding_box),
                    field.confidence
                ))
Beispiel #18
0
    def test_auto_detect_unsupported_stream_content(
            self, resource_group, location, form_recognizer_account,
            form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))

        with open(self.unsupported_content_py, "rb") as fd:
            myfile = fd.read()

        with self.assertRaises(ValueError):
            poller = client.begin_recognize_content(myfile)
Beispiel #19
0
    def test_content_multipage(self, resource_group, location,
                               form_recognizer_account,
                               form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))
        with open(self.multipage_invoice_pdf, "rb") as fd:
            invoice = fd.read()
        poller = client.begin_recognize_content(invoice)
        result = poller.result()

        self.assertEqual(len(result), 3)
        self.assertFormPagesHasValues(result)
Beispiel #20
0
    def test_auto_detect_unsupported_stream_content(
            self, formrecognizer_test_endpoint, formrecognizer_test_api_key):
        client = FormRecognizerClient(
            formrecognizer_test_endpoint,
            AzureKeyCredential(formrecognizer_test_api_key))
        with open(self.unsupported_content_py, "rb") as fd:
            myfile = fd.read()

        with self.assertRaises(ValueError):
            poller = client.begin_recognize_custom_forms(
                model_id="xxx",
                form=myfile,
            )
Beispiel #21
0
    def authentication_with_api_key_credential_form_recognizer_client(self):
        # [START create_fr_client_with_key]
        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,
                                                      AzureKeyCredential(key))
        # [END create_fr_client_with_key]
        poller = form_recognizer_client.begin_recognize_content_from_url(
            self.url)
        result = poller.result()
Beispiel #22
0
    def test_receipt(self, resource_group, location, form_recognizer_account,
                     form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))

        response = client.begin_extract_receipts_from_url(
            url=
            "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/contoso-allinone.jpg",
            include_text_details=True)

        result = response.result()
        self.assertIsNotNone(result)
Beispiel #23
0
    def test_get_form_training_client(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        transport = RequestsTransport()
        frc = FormRecognizerClient(endpoint=form_recognizer_account, credential=AzureKeyCredential(form_recognizer_account_key), transport=transport)

        with frc:
            poller = frc.begin_recognize_receipts_from_url(self.receipt_url_jpg)
            result = poller.result()
            assert transport.session is not None
            with frc.get_form_training_client() as ftc:
                assert transport.session is not None
                properties = ftc.get_account_properties()
            poller = frc.begin_recognize_receipts_from_url(self.receipt_url_jpg)
            result = poller.result()
            assert transport.session is not None
Beispiel #24
0
    def authentication_with_azure_active_directory_form_recognizer_client(self):
        """DefaultAzureCredential will use the values from these environment
        variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
        """
        # [START create_fr_client_with_aad]
        from azure.ai.formrecognizer import FormRecognizerClient
        from azure.identity import DefaultAzureCredential

        endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
        credential = DefaultAzureCredential()

        form_recognizer_client = FormRecognizerClient(endpoint, credential)
        # [END create_fr_client_with_aad]
        poller = form_recognizer_client.begin_recognize_receipts_from_url(self.url)
        receipt = poller.result()
Beispiel #25
0
    def test_content_stream_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
        client = FormRecognizerClient(form_recognizer_account,
                                      AzureKeyCredential(form_recognizer_account_key))
        with open(self.invoice_pdf, "rb") as fd:
            myform = fd.read()

        poller = client.begin_recognize_content(myform)
        result = poller.result()
        self.assertEqual(len(result), 1)
        layout = result[0]
        self.assertEqual(layout.page_number, 1)
        self.assertFormPagesHasValues(result)
        self.assertEqual(layout.tables[0].row_count, 2)
        self.assertEqual(layout.tables[0].column_count, 6)
        self.assertEqual(layout.tables[0].page_number, 1)
    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_data.text, sex.confidence))
            address = id_document.fields.get("Address")
            if address:
                print("Address: {} has confidence: {}".format(address.value, address.confidence))
            # FIXME: uncomment this
            # 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))
Beispiel #27
0
    def test_content_continuation_token(self, resource_group, location,
                                        form_recognizer_account,
                                        form_recognizer_account_key):
        client = FormRecognizerClient(
            form_recognizer_account,
            AzureKeyCredential(form_recognizer_account_key))
        initial_poller = client.begin_recognize_content_from_url(
            self.form_url_jpg)
        cont_token = initial_poller.continuation_token()

        poller = client.begin_recognize_content_from_url(
            self.form_url_jpg, continuation_token=cont_token)
        result = poller.result()
        self.assertIsNotNone(result)
        initial_poller.wait(
        )  # necessary so azure-devtools doesn't throw assertion error
Beispiel #28
0
def main():

    try:

        # Get configuration settings
        load_dotenv()
        form_endpoint = os.getenv('FORM_ENDPOINT')
        form_key = os.getenv('FORM_KEY')
        trainingDataUrl = os.getenv('STORAGE_URL')

        # Authenticate Form Training Client
        form_recognizer_client = FormRecognizerClient(
            form_endpoint, AzureKeyCredential(form_key))
        form_training_client = FormTrainingClient(form_endpoint,
                                                  AzureKeyCredential(form_key))

        # Train model
        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))

    except Exception as ex:
        print(ex)
Beispiel #29
0
def main():

    try:

        # Get configuration settings
        form_endpoint = "https://doors1.cognitiveservices.azure.com/"
        form_key = "70b2796924584d8da912296e8dea613a"
        trainingDataUrl = "https://doors.blob.core.windows.net/treinamento?sp=racwdl&st=2021-05-27T23:44:21Z&se=2021-08-02T07:44:21Z&sv=2020-02-10&sr=c&sig=9Tq5HVWS6Fzq5mHIIklZk3Z1wO%2B5junlwtlNTIFP194%3D"

        # Authenticate Form Training Client
        form_recognizer_client = FormRecognizerClient(
            form_endpoint, AzureKeyCredential(form_key))
        form_training_client = FormTrainingClient(form_endpoint,
                                                  AzureKeyCredential(form_key))

        # Train model
        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))

    except Exception as ex:
        print(ex)
    def test_polling_interval(self, formrecognizer_test_endpoint,
                              formrecognizer_test_api_key):
        client = FormRecognizerClient(
            formrecognizer_test_endpoint,
            AzureKeyCredential(formrecognizer_test_api_key),
            polling_interval=7)
        assert client._client._config.polling_interval == 7

        poller = client.begin_recognize_identity_documents_from_url(
            self.identity_document_url_jpg, polling_interval=6)
        poller.wait()
        assert poller._polling_method._timeout == 6
        poller2 = client.begin_recognize_identity_documents_from_url(
            self.identity_document_url_jpg)
        poller2.wait()
        assert poller2._polling_method._timeout == 7  # goes back to client default