Call Huawei API to realize ID card identification

1. Introduction of the author

Lei Qianlong, male, 2022 graduate student, School of Electronic Information, Xi'an Polytechnic University
Research direction: machine vision and artificial intelligence
Email: [email protected]

Zhang Siyi, female, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student, Zhang Hongwei Artificial Intelligence Research Group
Research direction: machine vision and artificial intelligence
Email: [email protected]

2. Call Huawei API to realize ID card identification

2.1 Algorithm introduction

2.1.1 Introduction to OCR

OCR (Optical Character Recognition, Optical Character Recognition) means that an electronic device (such as a scanner or a digital camera) checks characters printed on paper, determines its shape by detecting dark and light patterns, and then uses character recognition to translate the shape into a computer text process.

2.1.2 ID card identification principle

The recognition principle is text recognition, by recognizing the text content in the ID card picture, and returning the recognition result to the user in JSON format. Support ID card remake detection function, and can judge whether it is a copy or original.

2.1.3 ID card identification application scenarios

With the rapid development of smart technology information, many apps and small programs need to fill in various personal identity information, such as name, household registration address, ID number, etc. ID card OCR recognition can quickly realize user information input.

2.2 Call Huawei API process

(1) Baidu search for HUAWEI CLOUD or enter huaweiicloud.com to visit HUAWEI CLOUD official website, enter and search for "text recognition"
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
(2) Obtain access to AK/SK
, click Add Access, and fill in ID card identification in the description. A csv file will be generated and downloaded, and the Access Key Id and Secret Access Key can be obtained by opening the csv file.
insert image description here
insert image description here
(3) API debugging
Click API debugging in the development tool to enter. And click ID card recognition to debug.
insert image description here
insert image description here
(4) Parameter setting
insert image description here
Input the base64 encoding of the image at image,
input front
return_verification, select true
return_text_locaton, select true
insert image description here
insert image description here
(5) acquire base64 encoding,
drag the image into the browser and open it, press F12, click source and click {}, then you can Get the base64 encoding of the image, select all of them and return to the API Explorer and input them into the image to debug successfully.
insert image description here
insert image description here
insert image description here
insert image description here

3. Code implementation

3.1 Install related packages

pip install huaweicloudsdkcore
pip install huaweicloudsdkocr

3.2 Code reproduction

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkocr.v1 import *
import base64

#### base64编码格式
def image_to_base64(self):
    with open(self, 'rb') as f:
        base64_data = base64.b64encode(f.read())      #  将图像转换为 base64数据格式
        return base64_data

if __name__ == "__main__":
    ak = "你的ak"
    sk = "你的sk"

    credentials = BasicCredentials(ak, sk) \

    client = OcrClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(OcrRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = RecognizeIdCardRequest()
        request.body = IdCardRequestBody(
            return_text_location=True,
            return_verification=True,
            side="front",
            image= image_to_base64('path')  ##  调用之前定义的 base64 编码 并且输入自己的图片路径
        )
        response = client.recognize_id_card(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

3.3 Experimental results

![Insert picture description here](https://img-blog.csdnimg.cn/e96e627eb1de404e98252973a7f2e1c0.png

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/130891561