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

Post-Processing the Recognition Results

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()

    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.