SDK Reference Optical Character Recognition - HUAWEI TECHNOLOGIES CO., LTD - Issue Date

Page created by Rachel Robertson
 
CONTINUE READING
SDK Reference Optical Character Recognition - HUAWEI TECHNOLOGIES CO., LTD - Issue Date
Optical Character Recognition

SDK Reference

Issue           01
Date            2021-02-24

HUAWEI TECHNOLOGIES CO., LTD.
Copyright © Huawei Technologies Co., Ltd. 2021. All rights reserved.
No part of this document may be reproduced or transmitted in any form or by any means without prior
written consent of Huawei Technologies Co., Ltd.

Trademarks and Permissions

      and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks and trade names mentioned in this document are the property of their respective
holders.

Notice
The purchased products, services and features are stipulated by the contract made between Huawei and
the customer. All or part of the products, services and features described in this document may not be
within the purchase scope or the usage scope. Unless otherwise specified in the contract, all statements,
information, and recommendations in this document are provided "AS IS" without warranties, guarantees
or representations of any kind, either express or implied.

The information in this document is subject to change without notice. Every effort has been made in the
preparation of this document to ensure accuracy of the contents, but all statements, information, and
recommendations in this document do not constitute a warranty of any kind, express or implied.

Huawei Technologies Co., Ltd.
Address:       Huawei Industrial Base
               Bantian, Longgang
               Shenzhen 518129
               People's Republic of China

Website:       https://www.huawei.com
Email:         support@huawei.com

Issue 01 (2021-02-24)          Copyright © Huawei Technologies Co., Ltd.                                  i
Optical Character Recognition
SDK Reference                                                                                                                                   Contents

                                                                                                                               Contents

1 Introduction to OCR SDK....................................................................................................... 1
2 SDK Environment Setup......................................................................................................... 2
2.1 Preparing a Java Development Environment................................................................................................................ 2
2.2 Preparing a Python Development Environment........................................................................................................... 4
2.3 Preparing an iOS Development Environment............................................................................................................... 5
2.4 Preparing an Android Development Environment....................................................................................................... 6
2.5 Preparing a Node.js Development Environment.......................................................................................................... 7

3 Using Java SDK......................................................................................................................... 8
4 Using Python SDK................................................................................................................. 11
5 Using iOS SDK........................................................................................................................ 14
6 Using Android SDK................................................................................................................ 17
7 Using Node.js SDK................................................................................................................. 20
A Change History...................................................................................................................... 23

Issue 01 (2021-02-24)                          Copyright © Huawei Technologies Co., Ltd.                                                                    ii
Optical Character Recognition
SDK Reference                                                                  1 Introduction to OCR SDK

                                     1         Introduction to OCR SDK

OCR Overview
                  Optical Character Recognition (OCR) can convert characters in images or scanned
                  copies into editable text, eliminating the need for manual input to improve service
                  efficiency. It can recognize characters on ID cards, driving licenses, vehicle licenses,
                  receipts, general tables, and general text.

                  OCR provides services through open application programming interfaces (APIs).
                  You can obtain the inference result by accessing and invoking APIs in real time,
                  greatly improving business efficiency.

SDK Overview
                  Optical Character Recognition Software Development Kit (OCR SDK for short)
                  encapsulates the RESTful APIs provided by OCR to simplify application
                  development. You can directly call API functions provided by OCR SDKs to use
                  OCR.

Mappings Between Services and APIs
                  Table 1-1 lists the mappings between the OCR sub-services and APIs.

                  Table 1-1 Mappings between services and APIs

                   Service                                     API

                   Passport OCR                                POST /v2/{project_id}/ocr/passport

                   Thai ID Card OCR                            POST /v2/{project_id}/ocr/thailand-id-
                                                               card

                   Myanmar ID Card OCR                         POST /v2/{project_id}/myanmar-id-
                                                               card

                   Myanmar Driver License OCR                  POST /v2/{project_id}/ocr/myanmar-
                                                               driver-license

                   Chile ID Card OCR                           POST /v2/{project_id}/ocr/chile-id-card

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                                1
Optical Character Recognition
SDK Reference                                                                 2 SDK Environment Setup

                                       2          SDK Environment Setup

2.1 Preparing a Java Development Environment
Scenario
                  OCR JAVA SDK supports Windows, Linux, and Mac operating systems. This section
                  uses Windows as an example to describe how to configure the environment. Table
                  2-1 describes the required operating environment.

                  Table 2-1 Environment preparation

                   Item                               Description

                   Operating system (OS)              Windows OS. Windows 7 or later is
                                                      recommended.

                   Installing JDK                     Basic configuration for the development
                                                      environment. The version must be JDK 8 or
                                                      later.

                   Installing and configuring         Tool used for developing OCR applications
                   Eclipse

Procedure
                  The following describes how to configure the JDK 8 (64-bit) in the Windows 7
                  environment. If you have downloaded JDK and configured the environment, skip
                  steps 1 to 4. The following uses Eclipse as an example to describe how to import
                  the SDK project. Operations of other IDEs are similar to those of Eclipse.

                  1.    Download the JDK file.
                  2.    After the JDK file is downloaded, install the JDK as prompted. For example,
                        install the JDK to the C:\Program Files\Java\jdk1.8.0_131 directory on the
                        local PC.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                             2
Optical Character Recognition
SDK Reference                                                                 2 SDK Environment Setup

                  3.    Right-click Computer, choose Properties > Advanced System Settings >
                        Environment Variables, and perform the following operations to configure
                        Java environment variables:
                        a.   Create system variable JAVA_HOME whose value is the JDK installation
                             path.
                        b.   Add %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin to Path. Separate
                             multiple values with semicolons (;).
                  4.    Open the command line interface (CLI) and run java -version. If the
                        information shown in Figure 2-1 is displayed, the configuration is successful.

                        Figure 2-1 Java version information

                  5.    Install Eclipse
                        Download Eclipse, for example, eclipse-jee-mars-R-win32-x86_64.zip, from
                        https://www.eclipse.org/downloads/packages/. Extract and open Eclipse.
                        Configure the correct JRE path in Window > Preferences > Java > Installed
                        JREs.
                  6.    Import the Java SDK project.
                        a.   Right-click on the Package Explorer page on the left and choose Import
                             from the shortcut menu.
                        b.   Choose General > Existing Projects into Workspace.
                        c.   Click Next, select the Maven dependency mode, and select the path
                             where the OCR SDK demo package is stored.
                        d.   Click Finish to import the demo project. After the demo project is
                             imported, open it. The project directory is as follows:

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                                3
Optical Character Recognition
SDK Reference                                                                2 SDK Environment Setup

                           Figure 2-2 Project directory

2.2 Preparing a Python Development Environment
Scenario
                  OCR Python SDK supports Windows, Linux, and Mac operating systems. This
                  section uses Windows as an example to describe how to configure the
                  environment. Table 2-2 describes the required operating environment.

                  Table 2-2 Environment preparation
                   Item                               Description

                   OS                                 Windows OS. Windows 7 or later is
                                                      recommended.

                   Installing Python                  Python 2.7 or later is recommended.

                   Installing Python library          OCR Python SDK depends on the third-party
                   dependency                         requests 2.18.2 library package.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                         4
Optical Character Recognition
SDK Reference                                                                                     2 SDK Environment Setup

Procedure
                  The following describes how to install and configure Python 2.7 in the Windows 7
                  environment. If you have installed Python 2.7 or later and requests 2.18.2, skip
                  this section.
                  1.    Download your desired Python installation package from https://
                        www.python.org/downloads/release/python-2711/
                  2.    After the Python installation package is downloaded, install it as prompted.
                        The default path is C:\Python27.
                  3.    Right-click Computer, choose Properties > Advanced System Settings >
                        Environment Variables. Add C:\Python27;C:\Python27\Scripts to Path.
                  4.    Open the command line interface (CLI) and run python –v. If the information
                        shown in Figure 2-3 is displayed, the configuration is successful.

                        Figure 2-3 Python version information

                  5.    Run the following command in the new CMD window of PIP to install the
                        requests package:
                        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests --trusted-host https://
                        pypi.tuna.tsinghua.edu.cn/simple

                  6.    After the installation is complete, run the PIP command to check whether the
                        requests package is correctly installed. See Figure 2-4.

                        Figure 2-4 Installation confirmation

2.3 Preparing an iOS Development Environment
Scenario
                  Table 2-3 describes the environment to be prepared for using OCR iOS SDK.

Issue 01 (2021-02-24)             Copyright © Huawei Technologies Co., Ltd.                                            5
Optical Character Recognition
SDK Reference                                                                2 SDK Environment Setup

                  Table 2-3 Environment preparation
                   Item                               Description

                   OS                                 iOS 9.0 or later running on macOS

                   Installing Xcode                   Xcode 10.0 or later is recommended.

Procedure
                  Xcode is an integrated development environment (IDE) for iOS-based
                  development. The following describes how to use Xcode 10.0 to create an iOS
                  development environment. If you have installed and configured Xcode, skip this
                  section.
                  1.    Register an Apple ID.
                  2.    Search for Xcode in App Store and click GET.
                  3.    After Xcode is downloaded, click Lanchpad. Xcode is displayed.

2.4 Preparing an Android Development Environment
Scenario
                  OCR Android SDK supports Windows, Linux, and Mac operating systems. This
                  section uses Windows as an example to describe how to configure the
                  environment. Table 2-4 describes the required operating environment.

                  Table 2-4 Environment preparation
                   Item                               Description

                   OS                                 Windows OS. Windows 7 or later is
                                                      recommended.

                   Installing Android Studio          Android Studio 3.1.3 or later is recommended.

Procedure
                  OCR Android SDK uses the Android Studio IDE for development. The following
                  describes how to install and configure Android Studio 3.1.3 in the Windows 7
                  environment. If you have installed and configured Android Studio, skip this section.
                  1.    Prepare a Java development environment by referring to the instructions in
                        Preparing a Java Development Environment.
                  2.    Configure Android Studio.
                        Go to https://developer.android.com/studio#downloads to download
                        Android Studio 3.1.3 for the Windows environment.
                  3.    Start Android Studio, select Open an existing Android Studio project, and
                        then select the path of the OCR Android SDK package on the local PC.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                             6
Optical Character Recognition
SDK Reference                                                                              2 SDK Environment Setup

                  4.    The project targets API level 26. If the SDK does not have this version, install
                        Android API 26.

2.5 Preparing a Node.js Development Environment
Scenario
                  OCR Node.js SDK supports Windows, Linux, and Mac operating systems. This
                  section uses Windows as an example to describe how to configure the
                  environment. Table 2-5 describes the required operating environment.

                  Table 2-5 Environment preparation

                   Item                                        Description

                   OS                                          Windows OS. Windows 7 or later is
                                                               recommended.

                   Installing Node.js                          Node.js v10.13.0 or later is recommended.

                   Installing Node.js library                  OCR Node.js SDK depends on the third-party
                   dependency                                  request, moment, and moment-timezone
                                                               library packages.

Procedure
                  The following describes how to install and configure Node.js v10.13.0 in the
                  Windows 7 environment. If you have installed Node.js v10.13.0 or later and the
                  required library packages, skip this section.

                  1.    Configure Node.js.
                        Go to https://nodejs.org/en/download/ to download the latest Node.js.
                        Right-click Computer, choose Properties > Advanced System Settings >
                        Environment Variables, and add C:\Program Files\nodejs (default
                        installation path of node.exe) to Path.
                  2.    Verify Node.js.
                        Run the node –v command in the system CLI to check the Node.js version. If
                        the current version number is displayed, the configuration is successful.
                  3.    Run the following command to configure cnpm and the software library.
                        $ npm install -g cnpm --registry=https://registry.npm.taobao.org

                        If you have configured them, go to 4.
                  4.    Install a dependency package.
                        Go to the root directory of Node.js. Press and hold down Shift, right-click in
                        the window, and choose Open command window here from the shortcut
                        menu. Then, run the following commands:
                        cnpm install request –save
                        cnpm install moment --save
                        cnpm install moment-timezone --save

Issue 01 (2021-02-24)             Copyright © Huawei Technologies Co., Ltd.                                     7
Optical Character Recognition
SDK Reference                                                                                   3 Using Java SDK

                                                                3           Using Java SDK

                  Users can use SDK in token- or AK/SK-based authentication mode. For details
                  about how to obtain authentication messages, see Authentication. This section
                  describes how to use SDK in token- and AK/SK-based modes.

                  Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/
                  sdk?OCR, and decompress it.

                         NOTE

                        ● All images in this document are composite images and used for reference only.
                        ● A user needs to apply for enabling the service before using it for the first time. For
                          details about how to enable the service, click Subscribe. You only need to subscribe to
                          OCR once.

AK/SK-based Authentication
                  This section uses Passport OCR as an example to describe how to use SDK in
                  AK/SK-based authentication mode.

                  1.    Obtain AK/SK. For details, see Authentication > AK/SK-based
                        Authentication.
                  2.    Configure AK/SK of the Java SDK.
                        Change the values of AK and SK of the AKSKDemo function in the
                        OCRDemo.java file of the demo project to the obtained AK/SK.

                        Figure 3-1 Modifying parameters in the OCRDemo.java file

Issue 01 (2021-02-24)            Copyright © Huawei Technologies Co., Ltd.                                          8
Optical Character Recognition
SDK Reference                                                                                 3 Using Java SDK

                  3.    Modify parameters.
                        If you use a local image file for recognition, change the image path in
                        OCRDemo.java to the path of the file. If you use the default image of the
                        SDK, you do not need to modify the image path.
                  4.    Execute the OCRDemo.java file. If 200 is displayed on the console, the
                        program is successfully executed. You can view the recognition result of
                        Passport OCR on the console.
                        {
                            "result": {
                               "country_code": "IND",
                               "surname": "xxxx",
                               "given_name": "ASHISH",
                               "passport_number": "G0080xxx",
                               "date_of_birth": "1988-xx-xx",
                               "sex": "M",
                               "date_of_expiry": "2017-11-25",
                               "machine_code": "P
Optical Character Recognition
SDK Reference                                                                         3 Using Java SDK

                        Figure 3-2 Changing the username, password, and domain name in the
                        OCRDemo.java file

                  2.    Execute the code. The recognition result of Passport OCR is displayed.

Status Code
                  For details about status code, see Status Code.

Error Code
                  For details about error code, see Error Code.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                          10
Optical Character Recognition
SDK Reference                                                                                   4 Using Python SDK

                                                           4           Using Python SDK

                  Users can use SDK in token- or AK/SK-based authentication mode. For details
                  about how to obtain authentication messages, see Authentication. This section
                  describes how to use SDK in token- and AK/SK-based modes.
                  Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/
                  sdk?OCR, and decompress it.

                         NOTE

                        A user needs to apply for enabling the service before using it for the first time. For details
                        about how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based Authentication
                  This section uses Passport OCR as an example to describe how to use SDK in
                  AK/SK-based authentication mode.
                  1.    Obtain AK/SK. For details, see Authentication > AK/SK-based
                        Authentication.
                  2.    Configure AK/SK of the Python SDK.
                        Change the values of AK and SK of the aksk_request function in the
                        OCRDemo.py file of the demo project to the obtained AK/SK.

Figure 4-1 Modifying parameters in the OCRDemo.py file

                  3.    Modify parameters.
                        If you use a local image file for recognition, change the image path in
                        OCRDemo.py to the path of the file. If you use the default image of the SDK,
                        you do not need to modify the image path.

Issue 01 (2021-02-24)             Copyright © Huawei Technologies Co., Ltd.                                          11
Optical Character Recognition
SDK Reference                                                                                4 Using Python SDK

                  4.    Execute the OCRDemo.py file. If 200 is displayed on the console, the program
                        is successfully executed. You can view the recognition result of Passport OCR
                        on the console.
                        {
                            "result": {
                               "country_code": "IND",
                               "surname": "xxxx",
                               "given_name": "ASHISH",
                               "passport_number": "G0080xxx",
                               "date_of_birth": "1988-xx-xx",
                               "sex": "M",
                               "date_of_expiry": "2017-11-25",
                               "machine_code": "P
Optical Character Recognition
SDK Reference                                                                      4 Using Python SDK

                  2.    Execute the code. The recognition result of Passport OCR is displayed.

Status Code
                  For details about status code, see Status Code.

Error Code
                  For details about error code, see Error Code.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                         13
Optical Character Recognition
SDK Reference                                                                                        5 Using iOS SDK

                                                                     5           Using iOS SDK

                  Users can use SDK in token- or AK/SK-based authentication mode. For details
                  about how to obtain authentication messages, see Authentication. This section
                  describes how to use SDK in token- and AK/SK-based modes.
                  Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/
                  sdk?OCR, and decompress it.

                         NOTE

                        A user needs to apply for enabling the service before using it for the first time. For details
                        about how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based Authentication
                  This section uses Passport OCR as an example to describe how to use SDK in
                  AK/SK-based authentication mode.
                  1.    Obtain AK/SK. For details, see Authentication > AK/SK-based
                        Authentication.
                  2.    Configure AK/SK of the iOS SDK.
                        Change the values of AK and SK of the akskOcrService function in the
                        ViewController.m file of the demo project to the obtained AK/SK.

                        Figure 5-1 Modifying parameters in the ViewController.m file

                  3.    If you use a local image file for recognition, modify the image parameter in
                        the akskOcrService function of the ViewController.m file. If you use the
                        default image of the SDK, you do not need to modify the image path.
                  4.    Execute the akskOcrService function. If 200 is displayed on the console, the
                        program is successfully executed. You can view the of Passport OCR on the
                        console.

Issue 01 (2021-02-24)             Copyright © Huawei Technologies Co., Ltd.                                          14
Optical Character Recognition
SDK Reference                                                                                     5 Using iOS SDK

                        {
                            "result": {
                               "country_code": "IND",
                               "surname": "xxxx",
                               "given_name": "ASHISH",
                               "passport_number": "G008xxxx",
                               "date_of_birth": "1988-xx-xx",
                               "sex": "M",
                               "date_of_expiry": "2017-11-25",
                               "machine_code": "P
Optical Character Recognition
SDK Reference                                                               5 Using iOS SDK

Error Code
                  For details about error code, see Error Code.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.               16
Optical Character Recognition
SDK Reference                                                                                  6 Using Android SDK

                                                         6           Using Android SDK

                  Users can use SDK in token- or AK/SK-based authentication mode. For details
                  about how to obtain authentication messages, see Authentication. This section
                  describes how to use SDK in token- and AK/SK-based modes.
                  Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/
                  sdk?OCR, and decompress it.

                         NOTE

                        A user needs to apply for enabling the service before using it for the first time. For details
                        about how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based Authentication
                  This section uses Passport OCR as an example to describe how to use SDK in
                  AK/SK-based authentication mode.
                  1.    Obtain AK/SK. For details, see Authentication > AK/SK-based
                        Authentication.
                  2.    Configure AK/SK of the Android SDK.
                        Change the values of AK and SK of the akskOcrService function in the
                        MainActivity.java file of the demo project to the obtained AK/SK.

                        Figure 6-1 Modifying parameters in the MainActivity.java file

                  3.    Modify parameters.
                        If you use a local image file for recognition, modify the Bitmap parameter in
                        the akskOcrService function of the MainActivity.java file. If you use the
                        default image of the SDK, you do not need to modify the image path.

Issue 01 (2021-02-24)             Copyright © Huawei Technologies Co., Ltd.                                          17
Optical Character Recognition
SDK Reference                                                                               6 Using Android SDK

                  4.    Execute the akskOcrService function. If 200 is displayed on the console, the
                        program is successfully executed. You can view the recognition result on the
                        console.
                        {
                            "result": {
                               "country_code": "IND",
                               "surname": "xxxx",
                               "given_name": "ASHISH",
                               "passport_number": "G008xxxx",
                               "date_of_birth": "1988-xx-xx",
                               "sex": "M",
                               "date_of_expiry": "2017-11-25",
                               "machine_code": "P
Optical Character Recognition
SDK Reference                                                               6 Using Android SDK

Status Code
                  For details about status code, see Status Code.

Error Code
                  For details about error code, see Error Code.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                   19
Optical Character Recognition
SDK Reference                                                                                   7 Using Node.js SDK

                                                          7           Using Node.js SDK

                  Users can use SDK in token- or AK/SK-based authentication mode. For details
                  about how to obtain authentication messages, see Authentication. This section
                  describes how to use SDK in token- and AK/SK-based modes.

                  Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/
                  sdk?OCR, and decompress it.

                         NOTE

                        A user needs to apply for enabling the service before using it for the first time. For details
                        about how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based Authentication
                  This section uses Passport OCR as an example to describe how to use SDK in
                  AK/SK-based authentication mode.

                  1.    Obtain AK/SK. For details, see Authentication > AK/SK-based
                        Authentication.
                  2.    Configure AK/SK of the Node.js SDK.
                        Change the values of appKey and appSecret in the demo.js file of the demo
                        project to the obtained AK/SK.

                        Figure 7-1 Modifying parameters in the demo.js file

                  3.    If you use a local image file for recognition, change the image path in
                        demo.js to the path of the file. If you use the default image of the SDK, you
                        do not need to modify the image path.

Issue 01 (2021-02-24)             Copyright © Huawei Technologies Co., Ltd.                                          20
Optical Character Recognition
SDK Reference                                                                               7 Using Node.js SDK

                  4.    Execute the demo.js file. If 200 is displayed on the console, the program is
                        successfully executed. You can view the recognition result of Passport OCR on
                        the console.
                        {
                            "result": {
                               "country_code": "IND",
                               "surname": "xxxx",
                               "given_name": "ASHISH",
                               "passport_number": "G008xxxx",
                               "date_of_birth": "1988-xx-xx",
                               "sex": "M",
                               "date_of_expiry": "2017-11-25",
                               "machine_code": "P
Optical Character Recognition
SDK Reference                                                               7 Using Node.js SDK

Error Code
                  For details about error code, see Error Code.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                   22
Optical Character Recognition
SDK Reference                                                                    A Change History

                                                          A            Change History

                   Release Date            What's New

                   2020-03-25              This is the first official release.

Issue 01 (2021-02-24)           Copyright © Huawei Technologies Co., Ltd.                     23
You can also read