Paddle OCR报‘device id must be less than GPU count’错误求解决
解决PaddleOCR GPU设备不存在的错误
错误原因
报错明确提示Device id must be less than GPU count, but received id is: 0. GPU count is: 0,说明你的运行环境没有可用GPU,而PaddleOCR默认优先调用GPU运行,导致初始化失败。
解决方案
1. 强制使用CPU运行(最快解决)
在初始化PaddleOCR时添加use_gpu=False参数,明确指定用CPU执行:
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)
2. 匹配PaddlePaddle版本(彻底修复环境问题)
如果你的电脑没有NVIDIA GPU,或未配置CUDA环境,建议安装CPU版PaddlePaddle:
- 先卸载现有GPU版(如果已安装):
pip uninstall paddlepaddle-gpu -y - 安装CPU版PaddlePaddle(以Python3.8+为例,使用国内源加速):
pip install paddlepaddle==2.6.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
3. 修复代码缺失的导入
原代码中draw_ocr未导入,运行绘图部分会报错,需在代码开头补充导入:
from paddleocr import draw_ocr
修改后的完整可运行代码
from paddleocr import PaddleOCR, draw_ocr # Paddleocr supports Chinese, English, French, German, Korean and Japanese. # You can set the parameter `lang` as `ch`, `en`, `french`, `german`, `korean`, `japan` # to switch the language model in order. ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False) # 强制使用CPU img_path = 'capture.png' result = ocr.ocr(img_path, cls=True) for idx in range(len(result)): res = result[idx] for line in res: print(line) # draw result 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='/path/to/PaddleOCR/doc/fonts/simfang.ttf') im_show = Image.fromarray(im_show) im_show.save('result.jpg')
内容的提问来源于stack exchange,提问作者sgl
相关产品推荐
相关产品推荐

