你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Recognize Printed Text In Stream - Recognize Printed Text In Stream

光学字符识别(OCR)检测图像中的文本,并将识别的字符提取到计算机可用字符流中。 成功后,将返回 OCR 结果。 失败后,将返回错误代码和错误消息。 错误代码可以是 InvalidImageUrl、InvalidImageFormat、InvalidImageSize、NotSupportedImage、NotSupportedLanguage 或 InternalServerError 之一。

POST {Endpoint}/vision/v3.2/ocr?overload=stream&detectOrientation={detectOrientation}
POST {Endpoint}/vision/v3.2/ocr?overload=stream&detectOrientation={detectOrientation}&language={language}&model-version={model-version}

URI 参数

名称 必需 类型 说明
Endpoint
path True

string

支持的认知服务终结点。

detectOrientation
query True

boolean

是否检测图像中的文本方向。 借助 detectOrientation=true,OCR 服务会尝试检测图像方向,并在进一步处理之前对其进行更正(例如,如果它倒置)。

language
query

OcrLanguages

图像中要检测到的文本的 BCP-47 语言代码。 默认值为“unk”。

model-version
query

string

pattern: ^(latest|\d{4}-\d{2}-\d{2})(-preview)?$

用于指定 AI 模型的版本的可选参数。 接受的值为:“latest”、“2021-04-01”、“2021-05-01”。 默认值为“latest”。

请求头

Media Types: "application/octet-stream", "multipart/form-data"

名称 必需 类型 说明
Ocp-Apim-Subscription-Key True

string

请求正文

Media Types: "application/octet-stream", "multipart/form-data"

名称 类型 说明
Image

object (file)

图像流。

响应

名称 类型 说明
200 OK

OcrResult

OCR 将生成区域/行/字的层次结构。 结果包括文本、区域边界框、行和单词。 检测到的文本相对于最接近的水平或垂直方向的角度(以弧度为单位)。 按此角度顺时针旋转输入图像后,识别的文本行将变为水平或垂直。 结合方向属性,它可用于在原始图像上正确覆盖识别结果,方法是旋转原始图像,或者通过围绕原始图像中心的适当角度旋转识别结果。 如果无法自信地检测到角度,则此属性不存在。 如果图像包含不同角度的文本,则只能正确识别部分文本。

Other Status Codes

ComputerVisionErrorResponse

错误响应。

安全性

Ocp-Apim-Subscription-Key

类型: apiKey
在: header

示例

Successful RecognizePrintedText request

示例请求

POST https://westus.api.cognitive.microsoft.com/vision/v3.2/ocr?overload=stream&detectOrientation=true&language=en


"{binary}"

示例响应

{
  "language": "en",
  "textAngle": -2.0000000000000338,
  "orientation": "Up",
  "regions": [
    {
      "boundingBox": "462,379,497,258",
      "lines": [
        {
          "boundingBox": "462,379,497,74",
          "words": [
            {
              "boundingBox": "462,379,41,73",
              "text": "A"
            },
            {
              "boundingBox": "523,379,153,73",
              "text": "GOAL"
            },
            {
              "boundingBox": "694,379,265,74",
              "text": "WITHOUT"
            }
          ]
        },
        {
          "boundingBox": "565,471,289,74",
          "words": [
            {
              "boundingBox": "565,471,41,73",
              "text": "A"
            },
            {
              "boundingBox": "626,471,150,73",
              "text": "PLAN"
            },
            {
              "boundingBox": "801,472,53,73",
              "text": "IS"
            }
          ]
        },
        {
          "boundingBox": "519,563,375,74",
          "words": [
            {
              "boundingBox": "519,563,149,74",
              "text": "JUST"
            },
            {
              "boundingBox": "683,564,41,72",
              "text": "A"
            },
            {
              "boundingBox": "741,564,153,73",
              "text": "WISH"
            }
          ]
        }
      ]
    }
  ],
  "modelVersion": "2021-04-01"
}

定义

名称 说明
ComputerVisionError

API 请求错误。

ComputerVisionErrorCodes

错误代码。

ComputerVisionErrorResponse

API 错误响应。

ComputerVisionInnerError

有关 API 请求错误的详细信息。

ComputerVisionInnerErrorCodeValue

错误代码。

OcrLanguages

图像中要检测到的文本的 BCP-47 语言代码。 默认值为“unk”。

OcrLine

描述单个识别文本行的对象。

OcrRegion

区域由多行组成(例如多列文档中的文本列)。

OcrResult
OcrWord

有关已识别单词的信息。

ComputerVisionError

API 请求错误。

名称 类型 说明
code

ComputerVisionErrorCodes

错误代码。

innererror

ComputerVisionInnerError

内部错误包含更具体的信息。

message

string

说明服务报告的错误的消息。

ComputerVisionErrorCodes

错误代码。

说明
InternalServerError
InvalidArgument
InvalidRequest
ServiceUnavailable

ComputerVisionErrorResponse

API 错误响应。

名称 类型 说明
error

ComputerVisionError

错误内容。

ComputerVisionInnerError

有关 API 请求错误的详细信息。

名称 类型 说明
code

ComputerVisionInnerErrorCodeValue

错误代码。

message

string

错误信息。

ComputerVisionInnerErrorCodeValue

错误代码。

说明
BadArgument
CancelledRequest
DetectFaceError
FailedToProcess
InternalServerError
InvalidDetails
InvalidImageFormat
InvalidImageSize
InvalidImageUrl
InvalidModel
InvalidThumbnailSize
NotSupportedFeature
NotSupportedImage
NotSupportedLanguage
NotSupportedVisualFeature
StorageException
Timeout
Unspecified
UnsupportedMediaType

OcrLanguages

图像中要检测到的文本的 BCP-47 语言代码。 默认值为“unk”。

说明
ar
cs
da
de
el
en
es
fi
fr
hu
it
ja
ko
nb
nl
pl
pt
ro
ru
sk
sr-Cyrl
sr-Latn
sv
tr
unk
zh-Hans
zh-Hant

OcrLine

描述单个识别文本行的对象。

名称 类型 说明
boundingBox

string

已识别行的边界框。 四个整数表示左边缘的 x 坐标、上边缘的 y 坐标、宽度和边界框的高度,在输入图像的坐标系中,根据检测到的文本角度(见 textAngle 属性)围绕中心旋转后,其原点位于左上角,y 轴指向下。

words

OcrWord[]

对象的数组,其中每个对象表示已识别的单词。

OcrRegion

区域由多行组成(例如多列文档中的文本列)。

名称 类型 说明
boundingBox

string

已识别区域的边界框。 四个整数表示左边缘的 x 坐标、上边缘的 y 坐标、宽度和边界框的高度,在输入图像的坐标系中,根据检测到的文本角度(见 textAngle 属性)围绕中心旋转后,其原点位于左上角,y 轴指向下。

lines

OcrLine[]

已识别的文本行数组。

OcrResult

名称 类型 说明
language

string

图像中文本的 BCP-47 语言代码。

modelVersion

string

pattern: ^(latest|\d{4}-\d{2}-\d{2})(-preview)?$

AI 模型的版本。

orientation

string

图像中识别的文本的方向(如果已请求)。 根据检测到的文本角度(见 textAngle 属性),值(向上、向下、左或向右)是指在图像中心旋转后所识别文本的顶部所面对的方向。 如果未请求检测方向或未检测到文本,则值为“NotDetected”。

regions

OcrRegion[]

对象的数组,其中每个对象表示已识别文本的区域。

textAngle

number (double)

检测到的文本相对于最接近的水平或垂直方向的角度(以弧度为单位)。 按此角度顺时针旋转输入图像后,识别的文本行将变为水平或垂直。 结合方向属性,它可用于在原始图像上正确覆盖识别结果,方法是旋转原始图像,或者通过围绕原始图像中心的适当角度旋转识别结果。 如果无法自信地检测到角度,则此属性不存在。 如果图像包含不同角度的文本,则只能正确识别部分文本。

OcrWord

有关已识别单词的信息。

名称 类型 说明
boundingBox

string

已识别单词的边界框。 四个整数表示左边缘的 x 坐标、上边缘的 y 坐标、宽度和边界框的高度,在输入图像的坐标系中,根据检测到的文本角度(见 textAngle 属性)围绕中心旋转后,其原点位于左上角,y 轴指向下。

text

string

已识别单词的字符串值。