5

我正在做一个车牌/车牌识别项目,我正处于完成阶段,但有一个小问题,我已经成功识别了字符,考虑下面的例子: 在此处输入图像描述

这是输入图像,我得到的预测为2791 2g rj14

尽你所能,ocr 做得很好,但安排被破坏了(破坏了整个目的)。有时它会以正确的顺序输出,但有时却不会,所以当它没有以正确的顺序输出时,我正在尝试开发一种算法,它将预测的num_plate字符串作为输入并根据我的国家重新排列(印度)。

下面是一些图片,告诉我们印度号码/车牌的格式。

在此处输入图像描述

此外,我收集了所有州,但现在,我只想为 3 个州做:德里 (DL)、哈里亚纳邦 (HR)、北方邦 (UP)。更多信息:https ://en.wikipedia.org/wiki/List_of_Regional_Transport_Office_districts_in_India

total_states_list = [
    'AN','AP','AR','AS','BR','CG','CH','DD','DL','DN','GA','GJ','HR','HP','JH','JK','KA','KL',
    'LD','MH','ML','MN','MP','MZ','NL','OD','PB','PY','RJ','SK','TN','TR','TS','UK','UP','WB'
]

district_codes = {
    'DL': ['1','2','3','4','5','6','7','8','9','10','11','12','13'],
    'HR': [01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
            40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,
            71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99
    ]
}

因此,我一直在尝试,但无法提出一种算法,如果不是,则将序列重新排列为所需的序列。任何帮助将非常感激。

关于 OCR 的详细信息

使用keras-ocr,我得到输入图像的以下输出: 在此处输入图像描述

[
  ('hrlz',  array([[ 68.343796,  42.088367],
                   [196.68803 ,  26.907867],
                   [203.00832 ,  80.343094],
                   [ 74.66408 ,  95.5236  ]], dtype=float32)), 
  ('c1044', array([[ 50.215836, 113.09602 ],
                   [217.72466 ,  92.58473 ],
                   [224.3968  , 147.07387 ],
                   [ 56.887985, 167.58516 ]], dtype=float32))
]

来源:https ://keras-ocr.readthedocs.io/en/latest/examples/using_pretrained_models.html

keras_ocr.tools.drawAnnotations他们里面,我想得到预测框。所以我找到了这个文件并找到了drawAnnotations函数的实现,它是:

def drawAnnotations(image, predictions, ax=None):
  if ax is None:
        _, ax = plt.subplots()
    ax.imshow(drawBoxes(image=image, boxes=predictions, boxes_format='predictions'))
    predictions = sorted(predictions, key=lambda p: p[1][:, 1].min())
    left = []
    right = []
    for word, box in predictions:
        if box[:, 0].min() < image.shape[1] / 2:
            left.append((word, box))
        else:
            right.append((word, box))
    ax.set_yticks([])
    ax.set_xticks([])
    for side, group in zip(['left', 'right'], [left, right]):
        for index, (text, box) in enumerate(group):
            y = 1 - (index / len(group))
            xy = box[0] / np.array([image.shape[1], image.shape[0]])
            xy[1] = 1 - xy[1]
            ax.annotate(s=text,
                        xy=xy,
                        xytext=(-0.05 if side == 'left' else 1.05, y),
                        xycoords='axes fraction',
                        arrowprops={
                            'arrowstyle': '->',
                            'color': 'r'
                        },
                        color='r',
                        fontsize=14,
                        horizontalalignment='right' if side == 'left' else 'left')
    return ax

我应该如何获取(x,y,w,h),然后根据 number_plate bbox 的 y/x 以某种方式排序/打印?

编辑 - 2

我设法获得了字符的边界框,如下图所示:

在此处输入图像描述

使用函数cv2.polylines(box)box我之前粘贴输出的坐标在哪里。现在我如何按照人们在评论中建议的顺序打印它们,从左到右...使用 y/x。

4

1 回答 1

1

如果您可以获取每个已识别文本框的坐标,则:

  • 旋转坐标,使框与 X 轴平行
  • 缩放 Y 坐标,以便它们可以四舍五入为整数,这样并排的框将获得相同的整数 Y 坐标(如行号)
  • 按 Y 对数据排序,然后按 X 坐标
  • 按该顺序提取文本

以下是此类序列的示例:

data = [
  ('hrlz', [[ 68.343796,  42.088367],
            [196.68803 ,  26.907867],
            [203.00832 ,  80.343094],
            [ 74.66408 ,  95.5236  ]]), 
  ('c1044',[[ 50.215836, 113.09602 ],
            [217.72466 ,  92.58473 ],
            [224.3968  , 147.07387 ],
            [ 56.887985, 167.58516 ]])
]

# rotate data to align with X-axis
a, b = data[0][1][:2]
dist = ((b[1] - a[1]) ** 2 + (b[0] - a[0]) ** 2) ** 0.5
sin = (b[1] - a[1]) / dist
cos = (b[0] - a[0]) / dist
data = [
    (text, [(x * cos + y * sin, y * cos - x * sin) for x, y in box]) for text, box in data
]

# scale Y coordinate to integers
a, b = data[0][1][1:3]
height = b[1] - a[1]
data = [
    (round(box[0][1] / height), box[0][0], text) 
        for text, box in data
]

# sort by Y, then X
data.sort()

# Get text in the right order
print("".join(text for _, _, text in data))

这假设框的点按以下顺时针顺序给出:

top-left, top-right, bottom-right, bottom-left
于 2021-08-12T21:00:46.990 回答