使用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
相关产品推荐
相关产品推荐

