Custom OCR templates

更新时间:
复制 MD 格式

The Content Moderation API supports custom Optical Character Recognition (OCR) templates for extracting structured text from images. You can create these templates in the Content Moderation console and configure them by specifying the image layout and the text to recognize. This lets you recognize text from various types of images, such as receipts and certificates.

Background information

If the type of image you need to recognize is not in the list of supported structured OCR types, you can use a custom OCR template.

Before you start, review the following concepts:
  • Template: A standard layout for a group of images that share the same format and information. Before you can recognize text from an image, you must create a template in the Content Moderation console. Each template is assigned a unique ID. When you call the OCR detection API, you must pass the template ID as a request parameter.
    When you create a template, you must upload a sample image for recognition. The sample image must meet the following requirements:
    • The image must be in PNG, JPG, JPEG, BMP, or GIF format.
    • The image size must be between 1 KB and 10 MB. The resolution must be between 320 × 320 pixels and 4096 × 4096 pixels.
    • The image must be straight and flat. Avoid blur, overexposure, shadows, or other issues.
    • To improve recognition accuracy, highlight the parts to be recognized and manually trim any unnecessary parts.
    • You must include at least four reference fields. For accurate template positioning, spread the fields out near the edges of the image. Greater separation between the fields results in more accurate positioning.
    • The height of reference fields and recognition fields must be at least 20 pixels.
  • Reference field: A fixed field used to locate the template's position. The selection of reference fields affects the accuracy of image recognition. Reference fields must contain text whose position and content are fixed. The text within a single reference field cannot contain a line break. You must select at least four reference fields.
  • Recognition field: A field that contains content to be recognized. When you set a recognition field, you must assign a key value to it. The final result is returned in the key: recognized content format.

Procedure

  1. Log in to the Content Moderation console.
  2. In the navigation pane on the left, choose Settings > Machine Review.
  3. On the Machine Review page, click the OCR Templates tab.
  4. On the OCR Templates tab, click Create Template.
  5. In the Create Template panel, set a Template Name, click Select Image, and upload a sample image for recognition.
    The template is created and appears in the Template List on the left. Select the new template to continue the configuration.
  6. Set the reference fields.
    1. Click Set Reference Fields.
    2. On the Template Editor page, click Add Field and use the green rectangle to select a single-line reference field with a fixed position on the image.
      Note Ensure the selected area for the reference field contains only a single line of text and encloses the text completely.
    3. Repeat the previous step to set at least four different reference fields, and then click Save.
  7. Set the recognition fields.
    1. Click Set Recognition Fields.
    2. On the Template Editor page, click Add Field. Use the green rectangle to select a single line of text to recognize. Set a Key value for the selected content to use as an identifier for the recognition result.
      Note If a field to be recognized spans multiple lines, you must select each line separately and set the same Key value for them. The algorithm combines and returns fields with the same Key value in the order in which they were selected.
    3. Repeat the previous step to add all the recognition fields, and then click Save.
  8. After the template is created, select the desired template and click Copy Template ID.
  9. Call the detection API to perform image OCR. To apply the custom OCR template, pass the copied template ID as the value for the templateId parameter in the extras struct. For more information, see Synchronous OCR detection