EN
/news/show.php/video/99114931.html

Yolo11 OCR 营业执照识别 信息提取(预计后续更容易使用其他ocr,推理预计使用onxruntim加速,分为cc 以两种方式部署python)

2025-06-24 12:00:53 来源: 新华社
字号:默认 超大 | 打印 |

目录。

一 制作数据集。

1 安装使用labelimg。

2 标注方式。

3 制作数据集。

二 模型训练 

三 使用Yolo11 OCR 实现完整的“营业执照”信息分析方案。

1 cutLinesforcode.py。

2 getBusinessLicenseContentPart.py。

3 getPartWords.py。

4 pdfTojpg.py。

5 main.py。


本项目可用于。毕业设计参考。、。实验。营业执照分为。横版。和。竖版。整体检测 识别效果。如下:

描述:图片来自网络,如有侵权,请联系作者删除。

系统:Ubuntu 20.04。

依赖:

  • pdf2image。
pip install pdf2image -i https://pypi.tuna.tsinghua.edu.cn/simple。
  • yolo11。
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple。
  • pytesseract。
pip install pytesseract -i https://pypi.tuna.tsinghua.edu.cn/simple。

sudo apt update sudo apt install tesseract-ocr # 中文字库在线安装 sudo apt-get install tesseract-ocr-chi-sim。

一 制作数据集。


使用。labelimg。数据标注任务的工具。


1 安装使用labelimg。


安装方法:。 pip install labelimg。
用法如下:。
从cd到labellmg的路径。

python labellmg.py。


标记后生成的标记文件是xml文件。


2 标注方式。


本项目的目标是分析。营业执照。的。统一社会信用代码、名称、类型、法定代表人、经营范围、注册资本、成立日期、经营期限。和。住所。等信息。。
标注方法如下:。

 

您可以根据自己的需要制作数据集,类比。即可。


3 制作数据集。


如下图所示,原始数据集格式。

  • Annotations 存储标签xml文件。
  • JPEGImage 原始图片存储在里面。
  • labels 标签txt文件存储在其中。。文件夹中的文件是通过脚本进行的。xmI_txt.py。生成的。

xmI_txt.py。代码如下。

import xml.etree.ElementTree as ETimport osimport random # TODO 这里根据类别修改classes = ['code', 'specialcode', 'name', 'type', 'representative', 'range', 'registered', 'date', 'limit', 'address']# TODO xml___filepath = 'data/Annotations/'# TODO 根据txt文件夹的实际保存路径修改labels_savepath = 'data/labels/'abs_path = os.getcwd()  def convert(size, box):    dw = 1. / (size[0])    dh = 1. / (size[1])    x = (box[0]   box[1]) / 2.0 - 1    y = (box[2]   box[3]) / 2.0 - 1    w = box[1] - box[0]    h = box[3] - box[2]    x = x * dw    w = w * dw    y = y * dh    h = h * dh    return x, y, w, h  def convert_annotation(image_id):    in_file = open(xml_filepath   '%s.xml' % (image_id), encoding='UTF-8')    out_file = open(labels_savepath   '%s.txt' % (image_id), 'w')    tree = ET.parse(in_file)    root = tree.getroot()    size = root.find('size')    w = int(size.find('width').text)    h = int(size.find('height').text)    for obj in root.iter('object'):        difficult = obj.find('difficult').text        cls = obj.find('name').text        if cls not in classes or int(difficult) == 1:            continue        cls_id = classes.index(cls)        xmlbox = obj.find('bndbox')        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),             float(xmlbox.find('ymax').text))        b1, b2, b3, b4 = b        # 标注越界修正        if b2 > w:            b2 = w        if b4 > h:            b4 = h        b = (b1, b2, b3, b4)        bb = convert((w, h), b)        out_file.write(str(cls_id)   " "   " ".join([str(a) for a in bb])   '\n')  def run():    total_xml = os.listdir(xml_filepath)    num = len(total_xml)    names = []    for xml in total_xml:        names.append(xml[:-4])    for name in names:        convert_annotation(name)    pass  if __name__ == '__main__':    run()    pass。

然后,根据JPEGlmage, 文件夹和labels文件夹通过脚本。然后,根据JPEGlmage 文件夹和labels文件夹通过脚本。deal_dataset.py。

将数据集分为以下结构。deal_dataset.py。

代码如下:
import osimport randomimport shutil # root_原始数据集目录dir = 'data/'# 划分比例train_ratio = 0.8valid_ratio = 0.1test_ratio = 0.1 # 随机种子randomm设置.seed(42) # TODo 根据实际数据集路径修改split_dir = 'data_new/'os.makedirs(os.path.join(split_dir, 'train/images'), exist_ok=True)os.makedirs(os.path.join(split_dir, 'train/labels'), exist_ok=True)os.makedirs(os.path.join(split_dir, 'valid/images'), exist_ok=True)os.makedirs(os.path.join(split_dir, 'valid/labels'), exist_ok=True)os.makedirs(os.path.join(split_dir, 'test/images'), exist_ok=True)os.makedirs(os.path.join(split_dir, 'test/labels'), exist_ok=True) # TODo imgpathth按实际数据集路径进行修改 = "JPEGImage"labelpath = "labels"image_files = os.listdir(os.path.join(root_dir, imgpath))image_files.sort(key=lambda x: int(x.split('.(0))))print(image_files)label_files = os.listdir(os.path.join(root_dir, labelpath))label_files.sort(key=lambda x: int(x.split('.(0))))print(label_files)# combined_随机打乱文件列表files = list(zip(image_files, label_files))random.shuffle(combined_files)image_files_shuffled, label_files_shuffled = zip(*combined_files)print(image_files_shuffled)print(label_files_shuffled)# 根据比例计算划分的边界索引train_bound = int(train_ratio * len(image_files_shuffle。

【我要纠错】责任编辑:新华社