google-cloud-platform - Google Vision OCR,将 90、180、270 个文档中的单词坐标旋转到 0 度

标签 google-cloud-platform ocr google-cloud-vision

问题

鉴于我们有以下指南,取自 Google Vision OCR 文档 https://developers.google.com/resources/api-libraries/documentation/vision/v1p1beta1/python/latest/vision_v1p1beta1.files.html

                  "boundingBox": { # A bounding polygon for the detected image annotation. # The bounding box for the paragraph.
                      # The vertices are in the order of top-left, top-right, bottom-right,
                      # bottom-left. When a rotation of the bounding box is detected the rotation
                      # is represented as around the top-left corner as defined when the text is
                      # read in the 'natural' orientation.
                      # For example:
                      #   * when the text is horizontal it might look like:
                      #      0----1
                      #      |    |
                      #      3----2
                      #   * when it's rotated 180 degrees around the top-left corner it becomes:
                      #      2----3
                      #      |    |
                      #      1----0
                      #   and the vertex order will still be (0, 1, 2, 3).

作为一项实验,我以四个不同的方向扫描了同一份文档,并通过 Google 的 Vision OCR (DOCUMENT_TEXT_DETECTION) 运行它。即 0、90、180 和 270 度。 Google 的 OCR 输出给出以下结果。

方向为 0 度的文档。这是水平文本的默认设置。它有 0 度的文本旋转。它的四个角是:

0----1
|    |
3----2
Document height 3508
Document width 2479

输出文本示例

LEGO - {'vertices': [{'x': 755, 'y': 172}, {'x': 877, 'y': 173}, {'x': 876, 'y': 237}, {'x': 754, 'y': 236}]}
LEGOLAND - {'vertices': [{'x': 1994, 'y': 189}, {'x': 2269, 'y': 192}, {'x': 2268, 'y': 244}, {'x': 1993, 'y': 241}]}

90 度方向的文档。

1----2
|    |
0----3
*vertex order will still be (0, 1, 2, 3)
Document height 2479
Document width 3508

输出文本示例

LEGO - {'vertices': [{'x': 170, 'y': 1730}, {'x': 171, 'y': 1604}, {'x': 241, 'y': 1604}, {'x': 240, 'y': 1730}]}
LEGOLAND - {'vertices': [{'x': 188, 'y': 486}, {'x': 192, 'y': 213}, {'x': 245, 'y': 214}, {'x': 241, 'y': 487}]}

180 度方向的文档。

2----3
|    |
1----0
*vertex order will still be (0, 1, 2, 3)
Document height 3508
Document width 2479

输出文本示例

LEGO - {'vertices': [{'x': 1740, 'y': 3337}, {'x': 1584, 'y': 3336}, {'x': 1585, 'y': 3259}, {'x': 1741, 'y': 3260}]}
LEGOLAND - {'vertices': [{'x': 485, 'y': 3315}, {'x': 212, 'y': 3311}, {'x': 213, 'y': 3261}, {'x': 486, 'y': 3265}]}

270 度方向的文档。

3----0
|    |
2----1
*vertex order will still be (0, 1, 2, 3)
Document height 2479
Document width 3508

输出文本示例

LEGO - {'vertices': [{'x': 3335, 'y': 738}, {'x': 3333, 'y': 893}, {'x': 3269, 'y': 892}, {'x': 3271, 'y': 737}]}
LEGOLAND - {'vertices': [{'x': 3318, 'y': 1994}, {'x': 3313, 'y': 2266}, {'x': 3261, 'y': 2265}, {'x': 3266, 'y': 1993}]}

现在是问题

鉴于我们有一个以 90、180 和 270 度扫描的文档,如何以数学方式转换坐标,以便无论以哪个方向扫描,它们都给出与默认 0 度文档相同的结果。或者换句话说,如何像0度扫描一样校正90度、180度和270度的坐标?

这个问题对某些人来说可能看起来很简单,但过去几天我一直在尝试各种方法,但我似乎无法弄清楚。

因此,输入参数是扫描页面方向度数 (0,90,180,270)、Google OCR 输出的文本顶点以及同样来自 Google OCR 的页面尺寸(高度和宽度)。

输出必须是校正后的文本顶点,页面方向为 0 度

最佳答案

我会给你数学答案。请记住,数学是一门精确的科学,而 Vision OCR 扫描是一种经验技术,即不是精确的科学。

请允许我举一个简单的例子,以便您可以看到其行为。想象一个高度为 10、宽度为 4 的文档,其中有一个点位于坐标 (1,9) 处。当您将其旋转 90° 时,该点的坐标变为 (9,3),然后变为 (3,1),最后变为 (1,1)。

原因是,对于高度 H 和宽度 W 的通用矩形,点 (a,b) 旋转 90 度会产生:
(a,b) -> (b,W-a)W' = H ,H' = W

重复此变换会产生 180°、270° 变换。
即序列(a,b) -> (b,W-a) -> (W-a,H-b) -> (H-b, a) -> (a,b)

因此,只要您知道所有参数,就可以从序列中的任意点将其返回到(a,b),这只是一个简单的方程式。

例如,对于 180° 度边界框:

LEGO - {'vertices': [{'x': 1740, 'y': 3337}, {'x': 1584, 'y': 3336}, {'x': 1585, 'y': 3259}, {'x': 1741, 'y': 3260}]}
  • 每个 x 值均遵循 x = width-x0 -> x0 = width - x
  • 每个 y 值均遵循 y = height-y0 -> y0 = height - y

这给出:

LEGO - {'vertices': [{'x': 739, 'y': 171}, {'x': 895, 'y': 172}, {'x': 894, 'y': 249}, {'x': 738, 'y': 248}]}

当然,这与您原来的值略有不同。如果您对所有旋转执行简单的变换,您会发现它们在所有旋转中都略有不同。请记住,这是经验“边界框”,它们有一个相关的错误,并且不可能像“数学”问题那样使它们相同。

关于google-cloud-platform - Google Vision OCR,将 90、180、270 个文档中的单词坐标旋转到 0 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64802889/

相关文章:

google-cloud-platform - (Terraform、云运行)错误 : Forbidden Your client does not have permission to get URL/from this server

google-cloud-platform - 如何授予对 Google Cloud Dataprep 的访问权限?

python - 移除背景

image-processing - 使图像中的文本更薄以用于 OCR

javascript - 无法在 JavaScript 中访问内部 json 数组,但可以通过控制台记录完整的 json

google-cloud-storage - 在谷歌云计算中保存VM实例

pyspark - 从 pyspark 读取时,Google Cloud Storage 需要 storage.objects.create 权限

android - 在我的应用中使用 Zxing 和 Google Goggles

c# - 如何在 Google Cloud AutoML 中将数据导入数据集并重新训练自定义模型

google-cloud-vision - Google Cloud Vision 可以通过其 API 生成西类牙语标签吗?