为什么原始数据不接受POSTMAN中的格式?

问题描述 投票:0回答:1

这是我的代码块,我使用 Flask 创建 api 并在 POSTMAN 上测试相同的 API。 utils.py

utils.py

import os
import base64
from urllib.parse import urlparse
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient

def get_client():
    endpoint = "endpoint"
    api_key = "apikey"
    client = DocumentIntelligenceClient(endpoint=endpoint,credential=AzureKeyCredential(api_key))
    return client

def is_file_or_url(input_string):
    if os.path.isfile(input_string):
        return 'file'
    elif urlparse(input_string).scheme in ['http', 'https']:
        return 'url'
    else:
        return 'unknown'

def load_file_as_base64(file_obj):
    # Read the contents of the file object
    data = file_obj.read()
    # Encode the data as base64
    base64_bytes = base64.b64encode(data)
    base64_string = base64_bytes.decode('utf-8')
    return base64_string

app.py

import os
from flask import Flask, request, jsonify
from pathlib import Path
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest
from utils import get_client, load_file_as_base64

app = Flask(__name__)

@app.route('/extract_invoice', methods=['POST'])
def extract_invoice():
    # Get the file from the request
    file = request.files['file']

    # Create the 'temp' directory if it doesn't exist
    temp_dir = 'temp'
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)

    # Save the file to disk
    file_path = os.path.join(temp_dir, file.filename)
    file.save(file_path)
    model_id = 'prebuilt-invoice'
    doc_source = Path(file_path)

    document_ai_client = get_client()

    with open(doc_source, 'rb') as file_obj:
        file_base64 = load_file_as_base64(file_obj)

    poller = document_ai_client.begin_analyze_document(
        model_id,
        {"base64Source": file_base64},
        locale="en-US",
    )

    result = poller.result()

    # Clean up the temporary file
    os.remove(file_path)

    # Extract the invoice details
    invoice_details = []
    for document in result.documents:
        document_fields = document['fields']
        fields = document_fields.keys()

        invoice_detail = {}
        for field in fields:
            if field == 'Items':
                items_list = []
                items = document_fields[field]

                for item in items['valueArray']:
                    item_fields = item['valueObject']
                    item_dict = {}
                    for item_field in item_fields.keys():
                        value = item_fields[item_field].get('content', '')
                        item_dict[item_field] = value
                    items_list.append(item_dict)
                invoice_detail[field] = items_list
            else:
                value = document_fields[field].get('content', '')
                invoice_detail[field] = value

        invoice_details.append(invoice_detail)

    return jsonify(invoice_details)

if __name__ == '__main__':
    app.run(debug=True)

我尝试了所有替代方法来解决问题,但它不接受文件/其内容并给出错误:“格式不正确,请输入正确的格式进行导入”。此外,我还面临以下问题:“TypeError:无法在类似字节的对象上使用字符串模式”

这是我仅针对该特定图像总是遇到的错误。 Key error for Subway invoice image Subway invoice image being used

flask postman postman-collection-runner postman-testcase microsoft-azure-documentdb
1个回答
0
投票

我意识到你的错误是 HTTP Status 500

表示

document_ai_client.begin_analyze_document()
在加工过程中有缺陷。

这不是

base64
解码或编码问题。

我制作了图像解码模拟服务器并提取文本(键/值)

不是您服务器问题的直接地址,但我想表明您的服务器有问题。

概述

enter image description here

第 1 步 conda
demo_env
环境

下载并安装Anaconda3

启动 Anaconda 提示

enter image description here

创建

demo_env
并安装python

conda create --name demo_env python=3.8

enter image description here

切换

demo_env
环境

conda activate demo_env
pip install flask easyocr

enter image description here

步骤 2
utility.py
app.py

文件树

enter image description here

utility.py

import easyocr
import re

def extract_invoice_details(image_path):
    reader = easyocr.Reader(['en'])
    result = reader.readtext(image_path)
    full_text = '\n'.join([detection[1] for detection in result])

    patterns = {
        'Amount': r'Amount\s*;\s*\$(\d+),(\d+)',
        'Application': r'Application:\s*(.*)',
        'AID': r'AID\s*:\s*(\w+)',
        'MiD': r'MiD:\s*(\d+)',
        'TID': r'TID:\s*(\d+)',
        'Date/Time': r'Date/T\s*ime;\s*(\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2})'
    }

    extracted_items = {}
    for key, pattern in patterns.items():
        match = re.search(pattern, full_text)
        if match:
            if key == 'Amount':
                extracted_items[key] = f"${match.group(1)}.{match.group(2)}"
            elif key == 'Date/Time':
                extracted_items[key] = match.group(1).replace(' ', '; ')
            else:
                extracted_items[key] = match.group(1)
    return extracted_items

app.py

import os
from flask import Flask, request, jsonify
from utility import extract_invoice_details

app = Flask(__name__)

@app.route('/extract_invoice', methods=['POST'])
def extract_invoice():
    # Get the file from the request
    file = request.files['file']

    # Create the 'temp' directory if it doesn't exist
    temp_dir = 'temp'
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)

    # Save the file to disk
    file_path = os.path.join(temp_dir, file.filename)
    file.save(file_path)

    # Use the utility function to process the image
    invoice_details = extract_invoice_details(file_path)

    # Clean up the temporary file
    os.remove(file_path)

    return jsonify(invoice_details)

if __name__ == '__main__':
    app.run(debug=True)

subway.jpg

您的图像保存在本地。

demo.py

import easyocr
reader = easyocr.Reader(['en'])  # 'en' is for English, you can add other languages as needed
result = reader.readtext('subway.jpg')
for detection in result:
    print(detection[1])  # Prints out extracted text

步骤 3 从
subway.jpg

中提取文本
python demo.py

enter image description here

demo_v2.py

from utility import extract_invoice_details

def main():
    # Specify the path to the image file
    image_path = 'subway.jpg'
    
    # Call the function from utility.py to extract invoice details
    invoice_details = extract_invoice_details(image_path)
    
    # Print the extracted details
    print("Extracted Invoice Details:")
    for key, value in invoice_details.items():
        print(f"{key}: {value}")

if __name__ == '__main__':
    main()

此代码仅提取六个键/值

使用正则表达式提取特定数据:脚本使用预定义的正则表达式在聚合文本中搜索特定信息,例如金额、应用程序、AID、MiD、TID 和日期/时间。在返回之前,它会格式化其中一些片段以保持一致性和清晰度。

Amount ; $12,36
Application: VISA CREDIT
AID : AO000000031010
MiD: 420429002208556
TID: 75467009
Date/T ime; 06/09/2021 12:54:29

并调整两个键

Amount ; $12,36 ->  Amount: $12.36
Date/T ime; 06/09/2021 12:54:29  -> Date/Time: 06/09/2021; 12:54:29
python demo_v2.py

enter image description here

第 4 步运行 Flask 服务器

python app.py

enter image description here

第 5 步 Postman 使用
subway.jpg

调用 API

网址

POST http://localhost:5000/extract_invoice

身体 选择表单数据

键是

file
,值是
subway.jpg

enter image description here

Send
按钮

回应正文

{
    "AID": "AO000000031010",
    "Amount": "$12.36",
    "Application": "VISA CREDIT",
    "Date/Time": "06/09/2021; 12:54:29",
    "MiD": "420429002208556",
    "TID": "75467009"
}

enter image description here

总结

我相信问题在于您服务器的内部配置,而不是base64编码或Postman。

© www.soinside.com 2019 - 2024. All rights reserved.