Google Vision OCR,将单词坐标从90、180、270个文档旋转到0度

问题描述

问题

鉴于我们有以下指南,摘自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,3)
Document height 2479
Document width 3508

输出文字示例

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

文档方向为180度。

2----3
|    |
1----0
*vertex order will still be (0,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,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度的校正文本顶点

解决方法

我会给你数学上的答案。请记住,数学是一门精确的科学,而视觉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 =高度-y0-> y0 =高度-y

哪个给:

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

当然,这与您的原始值略有不同。如果对所有旋转执行简单转换,则可以看到它们在所有旋转中都略有不同。请记住,这是经验性的“边界框”,它们具有关联的错误,不可能像对待“数学”问题一样使它们相同。