Bu sayfa henüz yerel dilinizde mevcut değildir. Daha fazla dil seçeneği eklemek için yoğun bir şekilde çalışıyoruz. Desteğiniz için teşekkür ederiz.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Post-Processing the Recognition Results

Updated on 2023-10-26 GMT+08:00

Extracting Specific Fields and Importing Them to an Excel File

In this example, the API of ID Card OCR is called, the required fields are extracted from the obtained JSON results, and the fields are filled in an Excel file.

  • Prerequisites
    • You have subscribed to Passport OCR by referring to Subscribing to an OCR Service.
    • You have installed the OCR Python SDK by referring to Calling an OCR API Locally and have installed dependency packages by running the pip install xlsxwriter command.
    • You have obtained the AK and SK by accessing the Access Keys page. On this page, you can create an access key or use an existing one. The AK and SK are contained in the credentials.csv file.

  • Sample code
    # -*- coding: utf-8 -*-
    import base64
    import xlsxwriter
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkocr.v1 import *
    from huaweicloudsdkcore.http.http_config import HttpConfig
    
    
    def recognize_id_card_request():
        try:
            request = RecognizeIdCardRequest()
            request.body = IdCardRequestBody(
                image=image_base64
            )
            response = client.recognize_id_card(request)
            return response
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    
    def get_credential():
        return BasicCredentials(ak, sk)
    
    
    def get_client():
        config = HttpConfig.get_default_config()
        config.ignore_ssl_verification = True
        return OcrClient.new_builder(OcrClient) \
            .with_credentials(credentials) \
            .with_region(OcrRegion.CN_NORTH_4) \
            .with_http_config(config) \
            .build()
    
    
    def image_to_base64(imagepath):
        """
        Convert a local image into Base64 code.
        """
        with open(imagepath, "rb") as bin_data:
            image_data = bin_data.read()
        base64_data = base64.b64encode(image_data).decode("utf-8")
        return base64_data
    
    
    def response_to_execl(save_file, data):
        """
        :param save_file: File name
        :param data: result data
        """
    
        # Process the result data returned after the API is called.
        keys_list = list(data["result"].keys())
        values_list = list(data["result"].values())
    
        options = {'in_memory': True}
        with xlsxwriter.Workbook(save_file, options) as workbook:
            worksheet = workbook.add_worksheet()
            worksheet.set_column('A1:A20', 23)
            worksheet.set_column('B1:B20', 100)
            worksheet.write_column('A1', keys_list)
            worksheet.write_column('B1', values_list)
        workbook.close()
    
    
    if __name__ == '__main__':
        # Enter the AK and SK.
        ak = "Enter the AK."
        sk = "Enter the SK."
    
        # Init Auth Info
        credentials = get_credential()
    
        # Create OcrClient
        client = get_client()
    
        image_base64 = image_to_base64 (r"Image path, for example, D:\local\test.png")
    
        # request id card service
        response = recognize_id_card_request().to_dict()
    
        # Store the data in the Excel file.
        response_to_execl(r"Excel file path, for example, D:\local\test.xlsx", response)
    

Extracting Text from a PDF File

In this example, a PDF file is converted into images and the Web Image OCR API is called to obtain the recognition result.

  • Prerequisites
    • You have subscribed to Passport OCR by referring to Subscribing to an OCR Service.
    • You have installed the OCR Python SDK by referring to Calling an OCR API Locally and have installed dependency packages by running the pip install fitz and pip install PyMuPDF==1.18.0 commands.
    • You have obtained the AK and SK by accessing the Access Keys page. On this page, you can create an access key or use an existing one. The AK and SK are contained in the credentials.csv file.

  • Sample code
    # -*- coding: utf-8 -*-
    import os
    import base64
    import fitz
    import io
    from PIL import Image
    from glob import glob
    
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkocr.v1 import *
    from huaweicloudsdkcore.http.http_config import HttpConfig
    
    
    class CovertPdfToJpg:
        def __init__(self, file_path, save_root):
            self.file_path = file_path
            self.save_root = save_root
    
        @staticmethod
        def open_pdf(file):
            return fitz.open(file)
    
        @staticmethod
        def get_trans(doc, page, min_side=0, max_side=0, rotate=0.0):
            """ Create a scale object. """
            region = doc[page].rect
            scale = 1
            if max_side > min_side > 0:
                scale = min_side / min(region.width, region.height)
                if max(region.width, region.height) * scale > max_side:
                    scale = max_side / max(region.width, region.height)
            trans = fitz.Matrix(scale, scale).preRotate(rotate)
            return trans
    
        def page2pix(self, doc, page, trans):
            """ Parse the current page as image data based on given parameters."""
            return doc[page].getPixmap(matrix=trans, alpha=False)
    
        def pdf_to_jpg(self, width=1024, height=1400):
            """ Convert PDF images to JPG images."""
            doc = self.open_pdf(self.file_path)
            save_dir = os.path.join(self.save_root)
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            print("document", len(doc), doc.pageCount)
            for i in range(len(doc)):
                trans = self.get_trans(doc, i, width, height, rotate=0)
                try:
                    pdf = self.page2pix(doc, i, trans)
                except:
                    continue
                image = pdf.getPNGData()
                image = Image.open(io.BytesIO(image))
                print(os.path.join(
                    save_dir, os.path.basename(self.file_path).replace('.pdf', '') + '_' + str(i + 1) + '.jpg'))
                image.save(
                    os.path.join(save_dir, os.path.basename(self.file_path).replace('.pdf', '') + '_' + str(i + 1) + '.jpg'))
            return
    
    
    def recognize_general_text_request():
        try:
            request = RecognizeGeneralTextRequest()
            request.body = GeneralTextRequestBody(
                image=image_base64
            )
            response = client.recognize_general_text(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
    
    def get_credential():
        return BasicCredentials(ak, sk)
    
    
    def get_client():
        config = HttpConfig.get_default_config()
        config.ignore_ssl_verification = True
        return OcrClient.new_builder(OcrClient) \
            .with_credentials(credentials) \
            .with_region(OcrRegion.CN_NORTH_4) \
            .with_http_config(config) \
            .build()
    
    
    def url_to_base64(imagepath):
        with open(imagepath, "rb") as bin_data:
            image_data = bin_data.read()
        base64_data = base64.b64encode(image_data).decode("utf-8")
        return base64_data
    
    
    if __name__ == '__main__':
        # Enter the AK and SK.
        ak = "Enter the AK."
        sk = "Enter the SK."
    
        # Init Auth Info
        credentials = get_credential()
    
        # Create OcrClient
        client = get_client()
    
        df_path = r"Local PDF file path, for example, D:\local\test.pdf"
        save_path = r"Path of the converted image, for example, D:\local"
    
        covert_pdf_to_jpg = CovertPdfToJpg(df_path, save_path)
        covert_pdf_to_jpg.pdf_to_jpg()
    
        jpgs = glob(os.path.join(save_path, "*.jpg"))
        for jpg in jpgs:
            image_base64 = url_to_base64(jpg)
            recognize_general_text_request()
    NOTE:

    If the error message "AttributeError: 'Document' object has no attribute 'pageCount'" is displayed, uninstall the PyMuPDF dependency package and install version 1.18.0 instead.

Sitemizi ve deneyiminizi iyileştirmek için çerezleri kullanırız. Sitemizde tarama yapmaya devam ederek çerez politikamızı kabul etmiş olursunuz. Daha fazla bilgi edinin

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback