首页 python 正文
  • 本文约2727字,阅读需14分钟
  • 67
  • 0

Python实战——用中国自己的大模型做图片文字识别,准确度老高了

摘要

经过我的检索分析,发现paddle飞桨大模型是当前OCR计数领域识别较高的且开源的大模型。本文就来玩玩paddleocr文字识别模型。 前提条件 本文是为了让小伙伴可以在windows系统下使用,所以大佬们可以绕路了。 1)windows系统,10或11; 2)python是64位的,版本在3.8以上,最新的支持3.12; 3)pip是64位的,版本在20....

经过我的检索分析,发现paddle飞桨大模型是当前OCR计数领域识别较高的且开源的大模型。本文就来玩玩paddleocr文字识别模型。

前提条件

本文是为了让小伙伴可以在windows系统下使用,所以大佬们可以绕路了。

1)windows系统,10或11;

2)python是64位的,版本在3.8以上,最新的支持3.12;

3)pip是64位的,版本在20.2.2以上;

4)检查版本的方法。在命令提示符下输入如下命令。

C:\Users\willi>python -c "import platform;print(platform.architecture()[0]);print(platform.machine())"
64bit
AMD64

结果返回是第一个是64bit,第二行是 x64、Intel 64、AMD64之一。

5)这里我们仅安装CPU版本,也就不需要安装GPU、CUDA计算了。不建议大家安装GPU版本是因为它需要安装对应的GPU驱动和CUDA计算驱动,而且,初学者的你估计你也用不到,怎么简单怎么搞。

pip安装paddleocr

我们第一个要安装的是paddlepaddle,安装方法很简单。如下

python -m pip install paddlepaddle==3.0.0b1 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/

当出现Successfully installed paddlepaddle-3.0.0b1表示安装成功。现在可以进入python环境,看看安装的结果了。这里说明一下,官方文档给的windows系统CPU版本安装的是3.0.0b1,这是一个测试版本。我也尝试了稳定版本2.x.x,但我发现稳定版本安装总是报错,所以还是建议小伙伴安装这个测试版本。

>>> import paddle
>>> paddle.utils.run_check()
Running verify PaddlePaddle program ...
I0801 17:33:43.935122 56156 program_interpreter.cc:243] New Executor is Running.
I0801 17:33:43.962831 56156 interpreter_util.cc:648] Standalone Executor is Used.
PaddlePaddle works well on 1 CPU.
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.

当你可以看到上面的结果时,那么恭喜你,你已经安装成功了。现在我们继续安装paddleocr扩展包,这个包是专用于文字识别的。如下:

>>> pip install paddleocr

Successfully installed paddleocr-2.8.1

看到Successfully时,表示安装成功了。

使用paddleocr做文字识别

现在,我们来第一次使用paddleocr进行文字识别。先导入包,如下:

>>> from paddleocr import PaddleOCR, draw_ocr

接着,我们需要创建一个PaddleOCR对象,这个对象第一次创建时会下载对应的语言包,等待完成即可。如下:

>>> ocr = PaddleOCR(use_angle_cls=True, lang="ch")
download https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar to C:\Users\willi/.paddleocr/whl\det\ch\ch_PP-OCRv4_det_infer\ch_PP-OCRv4_det_infer.tar
100%|█████████████████████████████████████████████████████████████████████████████| 4.89M/4.89M [00:09<00:00, 504kiB/s]
download https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar to C:\Users\willi/.paddleocr/whl\rec\ch\ch_PP-OCRv4_rec_infer\ch_PP-OCRv4_rec_infer.tar
100%|█████████████████████████████████████████████████████████████████████████████| 11.0M/11.0M [00:21<00:00, 508kiB/s]
download https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar to C:\Users\willi/.paddleocr/whl\cls\ch_ppocr_mobile_v2.0_cls_infer\ch_ppocr_mobile_v2.0_cls_infer.tar
100%|█████████████████████████████████████████████████████████████████████████████| 2.19M/2.19M [00:04<00:00, 532kiB/s]
[2024/08/01 18:07:48] ppocr DEBUG: Namespace(help='==SUPPRESS==', use_gpu=False, use_xpu=False, use_npu=False, use_mlu=False, ir_optim=True, use_tensorrt=False, min_subgraph_size=15, precision='fp32', gpu_mem=500, gpu_id=0, image_dir=None, page_num=0, det_algorithm='DB', det_model_dir='C:\\Users\\willi/.paddleocr/whl\\det\\ch\\ch_PP-OCRv4_det_infer', det_limit_side_len=960, det_limit_type='max', det_box_type='quad', det_db_thresh=0.3, det_db_box_thresh=0.6, det_db_unclip_ratio=1.5, max_batch_size=10, use_dilation=False, det_db_score_mode='fast', det_east_score_thresh=0.8, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_sast_score_thresh=0.5, det_sast_nms_thresh=0.2, det_pse_thresh=0, det_pse_box_thresh=0.85, det_pse_min_area=16, det_pse_scale=1, scales=[8, 16, 32], alpha=1.0, beta=1.0, fourier_degree=5, rec_algorithm='SVTR_LCNet', rec_model_dir='C:\\Users\\willi/.paddleocr/whl\\rec\\ch\\ch_PP-OCRv4_rec_infer', rec_image_inverse=True, rec_image_shape='3, 48, 320', rec_batch_num=6, max_text_length=25, rec_char_dict_path='C:\\Users\\willi\\AppData\\Roaming\\Python\\Python312\\site-packages\\paddleocr\\ppocr\\utils\\ppocr_keys_v1.txt', use_space_char=True, vis_font_path='./doc/fonts/simfang.ttf', drop_score=0.5, e2e_algorithm='PGNet', e2e_model_dir=None, e2e_limit_side_len=768, e2e_limit_type='max', e2e_pgnet_score_thresh=0.5, e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_pgnet_valid_set='totaltext', e2e_pgnet_mode='fast', use_angle_cls=True, cls_model_dir='C:\\Users\\willi/.paddleocr/whl\\cls\\ch_ppocr_mobile_v2.0_cls_infer', cls_image_shape='3, 48, 192', label_list=['0', '180'], cls_batch_num=6, cls_thresh=0.9, enable_mkldnn=False, cpu_threads=10, use_pdserving=False, warmup=False, sr_model_dir=None, sr_image_shape='3, 32, 128', sr_batch_num=1, draw_img_save_dir='./inference_results', save_crop_res=False, crop_res_save_dir='./output', use_mp=False, total_process_num=1, process_id=0, benchmark=False, save_log_path='./log_output/', show_log=True, use_onnx=False, return_word_box=False, output='./output', table_max_len=488, table_algorithm='TableAttn', table_model_dir=None, merge_no_span_structure=True, table_char_dict_path=None, layout_model_dir=None, layout_dict_path=None, layout_score_threshold=0.5, layout_nms_threshold=0.5, kie_algorithm='LayoutXLM', ser_model_dir=None, re_model_dir=None, use_visual_backbone=True, ser_dict_path='../train_data/XFUND/class_list_xfun.txt', ocr_order_method=None, mode='structure', image_orientation=False, layout=True, table=True, ocr=True, recovery=False, use_pdf2docx_api=False, invert=False, binarize=False, alphacolor=(255, 255, 255), lang='ch', det=True, rec=True, type='ocr', savefile=False, ocr_version='PP-OCRv4', structure_version='PP-StructureV2')

现在,需要找一个图片来识别看看效果如何,我们还是使用上篇文章的图片。如下:

Python实战——用中国自己的大模型做图片文字识别,准确度老高了

识别代码如下:

>>> imp = r"D:\temp\17 视觉处理\carid.png"
>>> result = ocr.ocr(imp,cls=True)
[2024/08/01 18:18:07] ppocr DEBUG: dt_boxes num : 7, elapsed : 0.6315133571624756
[2024/08/01 18:18:07] ppocr DEBUG: cls num  : 7, elapsed : 0.11500215530395508
[2024/08/01 18:18:09] ppocr DEBUG: rec_res num  : 7, elapsed : 1.4291656017303467

识别结果已经保存在result里了,我们使用for循环来读取内容,如下:

>>> for idx in range(len(result)):
     res = result[idx]
     for line in res:
         print(line)

[[[65.0, 57.0], [316.0, 54.0], [316.0, 107.0], [66.0, 111.0]], ('岳阳大道', 0.9968570470809937)]
[[[450.0, 55.0], [700.0, 55.0], [700.0, 107.0], [450.0, 107.0]], ('青年东路', 0.994935154914856)]
[[[131.0, 119.0], [242.0, 116.0], [242.0, 141.0], [131.0, 144.0]], ('Yueyang Blvd', 0.9720354080200195)]
[[[500.0, 118.0], [654.0, 118.0], [654.0, 139.0], [500.0, 139.0]], ('Qingnian Middle Rd', 0.9771113991737366)]
[[[308.0, 179.0], [457.0, 179.0], [457.0, 214.0], [308.0, 214.0]], ('花板桥路', 0.9978734254837036)]
[[[525.0, 273.0], [708.0, 268.0], [710.0, 325.0], [526.0, 330.0]], ('建湘路', 0.9968564510345459)]
[[[569.0, 340.0], [673.0, 340.0], [673.0, 364.0], [569.0, 364.0]], ('Jianxiang Rd', 0.9541293978691101)]

可以看到它的识别能力是相当强的,所有的英文、中文均识别准确。


扫描二维码,在手机上阅读
    评论