python - Lambda 函数返回 Failed loading language 'eng' Tesseract 无法加载任何语言!无法初始化 tesseract
问题描述
我正在研究 AWS - Lambda (Python)。
我正在研究一个已经存在的使用tesseract
包的代码。我的 main 中有一个函数调用它:
def lambda_ocr(ze_path, step):
if step == 1:
ocr_options = "--oem 1 -l eng --psm 6"
elif step == 2:
ocr_options = "--oem 0 -l eng --psm 6"
elif step == 3:
ocr_options = "--oem 1 -l fra --psm 3"
elif step == 4 :
ocr_options = "--oem 0 -l fra --psm 11"
else:
print("WARNING invalid step given for ocr. default option --oem 1 -l fra --psm 3.")
ocr_options = "--oem 1 -l fra --psm 3"
res = ocr(ze_path, config=ocr_options)
def ocr(img_path, config="--oem 1 -l fra --psm 3"):
""" This function is called by get_text_OCR_Parallel
we can modify the tesseract config here
"""
raw_text = pytesseract.image_to_string(img_path, config=config)
return raw_text
def image_to_string(image,
lang=None,
config='',
nice=0,
output_type=Output.STRING):
'''
Returns the result of a Tesseract OCR run on the provided image to string
'''
args = [image, 'txt', lang, config, nice]
return {
Output.BYTES: lambda: run_and_get_output(*(args + [True])),
Output.DICT: lambda: {'text': run_and_get_output(*args)},
Output.STRING: lambda: run_and_get_output(*args),
}[output_type]()
当我lambda_ocr
使用 step=1 调用函数时,一切正常。但是当 step=2、3 或 4 时,它会抛出错误。
我对tesseract
包了解不多,但据此,我应该安装缺少的包。
我不明白的是,如果包没有安装好,当 step=1 时它是如何工作的?它不应该也抛出错误吗?
任何帮助表示赞赏。谢谢
解决方案
解决方案是使用 Lambda 层来安装缺少的包。我从 git 下载了所需的文件,然后将 .zip 上传到 AWS 并用它创建了一个层。
推荐阅读
- pandas - 使用 seaborn 联合图绘制 2D numpy 数组
- ios - 同时 UIViewController 过渡动画
- mysql - 带有 id 的 MYSQL SELECT 与 FLOOR RAND 比较返回一、多于一且无行
- facebook-comments - 如何修复来自 facebook 未捕获错误的评论插件?
- python - 由 cron 作业运行时仅将第一行记录到文件的脚本
- java - 为什么我的迭代器不能正确地迭代列表?
- java - java:'.class'预期返回数组时出错
- c++ - C++ 虚函数传递和工厂函数
- php - 左连接多次使用同一列的 2 个表 ON
- python - 为什么我们要在 sqlalchemy 中设置 local_infile=1 来加载本地文件?sqlalchemy 中不允许加载文件的问题