python - 根据国家重新排列车牌字符

标签 python python-3.x algorithm

我正在做一个车牌/车牌识别项目,我正在完成阶段但有一个小问题,我已经成功识别了字符,请考虑以下示例:
enter image description here
这是一个输入图像,我得到的预测为 2791 2g rj14尽你所能,ocr 做得很好,但安排被破坏了(破坏了整个目的)。 有时它会以正确的顺序输出,但有时它不会 ,所以当它没有以正确的顺序输出时,我正在尝试开发一种算法,该算法将采用预测的 num_plate字符串作为输入并根据我的国家(印度)重新排列。
下面是一些图片,它们告诉我们印度号码/车牌的格式。
enter image description here
此外,我已经收集了所有州,但现在,我只想为 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 ,我得到输入图像的以下输出:
enter image description here
[
  ('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
我设法获得了字符的边界框,如下图所示:
enter image description here
使用函数 cv2.polylines(box) ,其中 box是我之前粘贴输出的相同坐标。现在我怎样才能按照从左到右的顺序打印它们……使用评论中人们建议的 y/x。

最佳答案

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

  • 旋转坐标,使框与 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
    

    关于python - 根据国家重新排列车牌字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68757237/

    相关文章:

    python - pandas 按 3 个变量分组,但对其中 2 个变量求和

    python - 我如何通过 Python 中的多个函数 "double return"?

    algorithm - 如何计算循环图的密度?

    c# - 使用递归方法的数独生成器算法

    python - 检测单个字符串分数(例如 : ½ ) and change it to longer string?

    python - 如何在python中更新全局变量

    python - 通过调用另一个函数作为子函数来更改函数中的变量值

    python - 如何在Python3中同时移动2只 turtle

    algorithm - 二叉索引树(Fenwick Tree)——关于更新

    python - 获取 datetime.datetime.fromtimestamp() 使用的时区