You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用keras-ocr识别图片文本时遇AttributeError错误求助

解决keras-ocr报错AttributeError: 'function' object has no attribute 'pipeline'

这个错误的核心原因是你的自定义函数名和keras-ocr库的模块名重名了。你定义了一个名为keras_ocr的函数,导致在函数内部访问keras_ocr.pipeline时,Python会把keras_ocr解析成你自己写的函数,而不是keras-ocr库的模块,自然找不到pipeline属性。

解决步骤:

  • 重命名你的自定义函数:把def keras_ocr(image):改成其他名字,比如def recognize_image_text(image):,同时调用该函数的地方也要同步修改。
  • 确保keras-ocr库的导入语句正确:使用import keras_ocr,不要和自定义函数名混淆。

修改后的代码示例:

import keras_ocr

# 重命名自定义函数,避免和库名冲突
def recognize_image_text(image_path):
    # 初始化pipeline,此时keras_ocr指向库模块
    pipeline = keras_ocr.pipeline.Pipeline()
    # 读取图片
    images = [keras_ocr.tools.read(image_path)]
    # 识别文本
    predictions = pipeline.recognize(images)
    # 输出识别结果
    for text, _ in predictions[0]:
        print(text)

# 调用修改后的函数
recognize_image_text(image_path='sampl.png')

内容的提问来源于stack exchange,提问作者Arpit Sengar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 20:39:51