• OCR开源工具箱MMOCR安装及使用示例(英文识别)


          MMOCR是一个基于PyTorch和MMDetection的开源工具箱,专注于文本检测、文本识别以及相应的下游任务,如关键信息提取,是OpenMMLab项目的一部分,源码在https://github.com/open-mmlab/mmocr,最新发布版本为v0.6.1,License为Apache-2.0。它支持在Windows、Linux和Mac上运行。
          1.安装:使用conda安装
          (1).创建openmmlab虚拟环境:

    1. conda create -n openmmlab python=3.8
    2. conda activate openmmlab

          (2).安装PyTorch:这里PyTorch使用1.11.0版本,CUDA使用10.2版本,此CUDA版本对PyTorch各版本都支持

    conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=10.2 -c pytorch

          (3).安装MMCV:MMCV有两个版本,这里安装带CUDA的mmcv-full
          1).mmcv-full: 完整版,包含所有的特性以及丰富的开箱即用的CUDA算子,安装此版本需要较长时间。
          2).mmcv:精简版,不包含CUDA算子但包含其余所有特性和功能,类似MMCV 1.0之前的版本。
          不要在同一个环境中安装两个版本,否则可能会遇到类似ModuleNotFound的错误。在安装一个版本之前,需要先卸载另一个:

    1. pip uninstall mmcv-full
    2. pip uninstall mmcv

          注意:这里mmcv-full使用1.5.3版本。CUDA版本和PyTorch版本与安装PyTorch时保持一致

    pip install mmcv-full==1.5.3 -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.11.0/index.html

          (4).安装MMDetection:

    pip install mmdet==2.25.1

          (5).安装tesseract

    conda install -c conda-forge tesserocr

          (6).安装MMOCR:没有通过源码安装,要求GCC版本为5.4.0及以上版本

    pip install mmocr==0.6.1

          2.测试:论文:《TextSnake: A Flexible Representation for Detecting Text of Arbitrary Shapes》

         (1).准备测试图像:原始图像来自网络

    1. image_path = "../../data/image/"
    2. image_name = "ocr_english.png"

         (2).下载检测模型(checkpoint):

    1. def download_checkpoint(path, name, url):
    2. if os.path.isfile(path+name) == False:
    3. print("checkpoint(model) file does not exist, now download ...")
    4. subprocess.run(["wget", "-P", path, url])
    5. path = "../../data/model/"
    6. checkpoint = "textsnake_r50_fpn_unet_1200e_ctw1500-27f65b64.pth"
    7. url = "https://download.openmmlab.com/mmocr/textdet/textsnake/textsnake_r50_fpn_unet_1200e_ctw1500-27f65b64.pth"
    8. download_checkpoint(path, checkpoint, url)

         (3).根据配置文件和checkpoint文件构建模型:

    1. config = "../../src/mmocr/configs/textdet/textsnake/textsnake_r50_fpn_unet_1200e_ctw1500.py"
    2. ocr = MMOCR(det="TextSnake", det_config=config, det_ckpt=path+checkpoint, recog=None, device=device)

         (4).进行检测推理:

    results = ocr.readtext(image, output="../../data/result_mmocr_text_detection.png", export="../../data/", export_format="json")

         (5).下载识别模型(checkpoint):

    1. checkpoint2 = "seg_r31_1by16_fpnocr_academic-72235b11.pth"
    2. url = "https://download.openmmlab.com/mmocr/textrecog/seg/seg_r31_1by16_fpnocr_academic-72235b11.pth"
    3. download_checkpoint(path, checkpoint2, url)

         (6).根据配置文件和checkpoint文件构建模型,包括检测和识别:

    1. config2 = "../../src/mmocr/configs/textrecog/seg/seg_r31_1by16_fpnocr_toy_dataset.py"
    2. ocr2 = MMOCR(det="TextSnake", det_config=config, det_ckpt=path+checkpoint, recog="SEG", recog_config=config2, recog_ckpt=path+checkpoint2, device=device)

          (7).进行检测及识别推理:

    1. results2 = ocr2.readtext(image, output="../../data/result_mmocr_text_recognition.png")
    2. print("recognition result:", results2)

          执行结果如下图所示:

          GitHub: https://github.com/fengbingchun/PyTorch_Test

  • 相关阅读:
    java技术:nacos
    Linux进程基础(一)
    线性筛和埃氏筛
    利用Nextcloud搭建企业私有云盘系统
    ubuntu20.04 vins-fusion 运行记录
    LLM微调方法(Efficient-Tuning)六大主流方法:思路讲解&优缺点对比[P-tuning、Lora、Prefix tuing等]
    Java数学工具类Math
    Nginx监控与告警:确保服务稳定运行
    【Docker】设置容器系统字符集zh_CN.UTF-8退出失效:关于Docker容器配置环境变量,再次进入失效问题
    angular项目启动报错
  • 原文地址:https://blog.csdn.net/fengbingchun/article/details/126805622