PaddleOCR with Python 3.12 on Ubuntu 24.04
目录
问题
Illegal instruction
的问题
根据官方的QuickStart使用pip3 install paddleocr
后,运行发现出现No module named paddle
, 于是使用 python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
安装,再次运行测试代码发现会出现Illegal instruction
的问题, 相关log,可以参考这个链接, 和这个很类似。 但是按照里面的提示安装了numpy对应的版本后,依然有问题。
再次查找,发现issue里面提到说是需要安装2.5.2版本的,于是:
python3 -m pip install paddlepaddle==2.5.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
结果发现,出现类似[这个issue里面](https://github.com/PaddlePaddle/Paddle/issues/55050)的问题:
ERROR: No matching distribution found for paddlepaddle==2.5.2
ERROR: Could not find a version that satisfies the requirement paddlepaddle (from versions: none)
ERROR: No matching distribution found for paddlepaddle
发现是Ubuntu 24.04带的Python 3.12太高,无法安装低版本2.5.2的paddlepaddle.
想了想,干脆自己编译一个低版本的,于是选择了Python 3.10.14版本,然后开始编译。 先安装依赖:
sudo apt-get build-dep openssh-server
sudo apt-get build-dep python3
sudo apt install libsqlite3-dev
sudo apt install libopenssl-dev
然后开始编译
mkdir -p ~/.python310
./configure --prefix=$HOME/.python310 --enable-optimizations --enable-loadable-sqlite-extensions
make -j6
接下来开始使用:
export PATH=$HOME/.python310/bin:$PATH
cd $HOME/.python310/bin
ln -s python3.10 python3
ln -s python3.10 python
ln -s pip3.10 pip3
ln -s pip3.10 pip
安装paddleocr及其依赖whl:
pip3 install paddlepaddle==2.5.2 -i https://mirror.baidu.com/pypi/simple
pip3 install paddlepaddle==2.5.2 -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip3 install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip3 install requests -i https://pypi.tuna.tsinghua.edu.cn/simple/
这样终于可以跑起来了。
测试
使用下面的代码:
from paddleocr import PaddleOCR, draw_ocr
# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
img_path = 't1.jpg'
result = ocr.ocr(img_path, cls=True)
for idx in range(len(result)):
res = result[idx]
for line in res:
print(line)
# 显示结果
from PIL import Image
result = result[0]
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
#im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf')
im_show = draw_ocr(image, boxes, txts, scores,font_path='CanErShuYuanW01.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
从paddleocr的demo中获取ttf,自己找一个图片改名字为t1.png, 然后运行就可以得到结果:
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='/home/hexiongjun/.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='/home/hexiongjun/.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='/home/hexiongjun/.python310/lib/python3.10/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='/home/hexiongjun/.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')
[2024/07/05 14:30:03] ppocr DEBUG: dt_boxes num : 19, elapsed : 0.44461607933044434
[2024/07/05 14:30:03] ppocr DEBUG: cls num : 19, elapsed : 0.05456256866455078
[2024/07/05 14:30:04] ppocr DEBUG: rec_res num : 19, elapsed : 0.8953077793121338
[[[370.0, 102.0], [474.0, 102.0], [474.0, 142.0], [370.0, 142.0]], ('36℃', 0.9455528259277344)]
[[[583.0, 100.0], [729.0, 100.0], [729.0, 144.0], [583.0, 144.0]], ('24.9℃', 0.9253147840499878)]
[[[348.0, 168.0], [493.0, 164.0], [494.0, 207.0], [349.0, 211.0]], ('室外温度', 0.9998395442962646)]
[[[584.0, 169.0], [727.0, 169.0], [727.0, 208.0], [584.0, 208.0]], ('室内温度', 0.9992950558662415)]
[[[408.0, 306.0], [677.0, 306.0], [677.0, 391.0], [408.0, 391.0]], ('24.5c', 0.9866175651550293)]
[[[867.0, 325.0], [902.0, 325.0], [902.0, 362.0], [867.0, 362.0]], ('十', 0.5207378268241882)]
[[[89.0, 641.0], [187.0, 641.0], [187.0, 694.0], [89.0, 694.0]], ('模式', 0.9996239542961121)]
[[[595.0, 642.0], [689.0, 642.0], [689.0, 691.0], [595.0, 691.0]], ('风速', 0.9997482299804688)]
[[[91.0, 716.0], [156.0, 716.0], [156.0, 753.0], [91.0, 753.0]], ('制冷', 0.999926745891571)]
[[[592.0, 716.0], [640.0, 716.0], [640.0, 753.0], [592.0, 753.0]], ('1%', 0.9995734691619873)]
[[[98.0, 894.0], [305.0, 894.0], [305.0, 948.0], [98.0, 948.0]], ('常用功能', 0.9998921155929565)]
[[[592.0, 898.0], [677.0, 898.0], [677.0, 945.0], [592.0, 945.0]], ('排序', 0.9999576210975647)]
[[[779.0, 902.0], [930.0, 902.0], [930.0, 941.0], [779.0, 941.0]], ('功能说明', 0.9998066425323486)]
[[[118.0, 1186.0], [214.0, 1184.0], [215.0, 1212.0], [118.0, 1214.0]], ('环绕风', 0.9038755893707275)]
[[[359.0, 1186.0], [479.0, 1186.0], [479.0, 1212.0], [359.0, 1212.0]], ('下下风', 0.8255085349082947)]
[[[598.0, 1186.0], [724.0, 1186.0], [724.0, 1212.0], [598.0, 1212.0]], ('左右理风', 0.810370147228241)]
[[[845.0, 1186.0], [974.0, 1186.0], [974.0, 1212.0], [845.0, 1212.0]], ('出风方向', 0.9972140192985535)]
[[[268.0, 1395.0], [345.0, 1395.0], [345.0, 1441.0], [268.0, 1441.0]], ('关机', 0.9999555349349976)]
[[[738.0, 1395.0], [810.0, 1395.0], [810.0, 1438.0], [738.0, 1438.0]], ('定时', 0.9996543526649475)]
对应的result.png: