株洲营销网站建设,跨境电商平台企业,中国建筑工程总公司招聘,萝岗微信网站建设异常一#xff1a;ModuleNotFoundError: No module named ‘tools.infer’
实验案例#xff1a; PaddleOCR #使用PaddleOCR进行光学字符识别#xff08;PP-OCR文本检测识别#xff09; 参考代码#xff1a; 图片文本检测实验时#xff0c;运行代码出现异常#xff1a;M…异常一ModuleNotFoundError: No module named ‘tools.infer’
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别PP-OCR文本检测识别 参考代码 图片文本检测实验时运行代码出现异常ModuleNotFoundError: No module named ‘tools.infer’
# Importing required libraries.
import cv2
import os
import numpy as np
import sys
import re
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as img
import time
import numpy# Importing functions and methods for OCR
from tools.infer.predict_rec import *
import tools.infer.utility as utility
from ppocr.postprocess import build_post_process
from ppocr.utils.logging import get_logger
from ppocr.utils.utility import get_image_file_list, check_and_read_gif异常信息 import tools.infer.utility as utility
ModuleNotFoundError: No module named tools.infer异常原因 这是由于 python 本来有个 tools和 paddleocr 内部的 tools 冲突导致。可能是 paddleocr 版本问题也可能是 python 环境问题。 解决方法 方法1找到 paddleocr 文件把所有导入 tools.infer 包的地方的前面加上 paddleocr. 即为 paddleocr.tools.infer 方法2把 paddleocr/tools 下面的 infer 文件夹移动到 python 本身的 tools 里面
试过方法1未能成功。 但按下面的方式将当前目录添加到 python 的模块搜索路径中可解决脚本方式出现这个异常
# Importing required libraries.
import cv2
import os
import numpy as np
import sys
import re
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as img
import time
import numpy# 获取当前脚本文件的绝对路径所在的目录路径并将其赋值给变量 __dir__。
__dir__ os.path.dirname(os.path.abspath(__file__))
# 将当前脚本文件的绝对路径所在的目录路径添加到Python的模块搜索路径中。
sys.path.append(__dir__)
# 将当前脚本文件的上一级目录路径添加到Python的模块搜索路径中。os.path.join(__dir__, ..) 用于获取上一级目录的路径os.path.abspath() 用于获取绝对路径。
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, ..)))import importlib
tools importlib.import_module(., tools)
ppocr importlib.import_module(., ppocr)# Importing functions and methods for OCR
from tools.infer.predict_rec import *
import tools.infer.utility as utility
from ppocr.postprocess import build_post_process
from ppocr.utils.logging import get_logger
from ppocr.utils.utility import get_image_file_list, check_and_read_gif方法2亲测可行 1CMD 通过命令确认本地 python 的 tools 包位置 2把 paddleocr/tools 下面的 infer 文件夹移动到 python 本身的 tools 文件夹中 注场景2CMD 方式的异常场景的原因可能各有不同主要是因为本地安装的 paddleocr 版本各异。但主要原因可归为版本不兼容可根据异常提示逐步补全依赖目录或文件解决但比较繁琐。 异常二ImportError: cannot import name ‘check_and_read_gif’ from ‘ppocr.utils.utility’
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别PP-OCR文本检测识别
Traceback (most recent call last):File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\runpy.py, line 197, in _run_module_as_mainreturn _run_code(code, main_globals, None,File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\runpy.py, line 87, in _run_codeexec(code, run_globals)File D:\Tp_Mylocal\20_Install\python-3.9.13\Scripts\paddleocr.exe\__main__.py, line 4, in moduleFile D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\paddleocr\__init__.py, line 14, in modulefrom .paddleocr import *File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\paddleocr\paddleocr.py, line 37, in modulefrom tools.infer import predict_systemFile D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\tools\infer\predict_system.py, line 32, in moduleimport tools.infer.predict_rec as predict_recFile D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\tools\infer\predict_rec.py, line 33, in modulefrom ppocr.utils.utility import get_image_file_list, check_and_read_gif
ImportError: cannot import name check_and_read_gif from ppocr.utils.utility (D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\paddleocr\ppocr\utils\utility.py)亦或者
Traceback (most recent call last):File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\runpy.py, line 197, in _run_module_as_mainreturn _run_code(code, main_globals, None,File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\runpy.py, line 87, in _run_codeexec(code, run_globals)File D:\Tp_Mylocal\20_Install\python-3.9.13\Scripts\paddleocr.exe\__main__.py, line 4, in moduleFile D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\paddleocr\__init__.py, line 14, in modulefrom .paddleocr import *File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\paddleocr\paddleocr.py, line 41, in modulefrom ppocr.utils.utility import check_and_read, get_image_file_list
ImportError: cannot import name check_and_read from ppocr.utils.utility (D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\ppocr\utils\utility.py)异常原因 此问题八成是你安装的 PaddleOCR 版本不兼容产生的问题比如可能你通过下面的命令成功安装了 paddleocr、paddlepaddle
pip install paddlepaddle paddleocrSuccessfully installed paddleocr-2.6.1.3 paddlepaddle-2.4.2
但通过 CMD 运行时总是有异常说 xxx 包找不到或者 xxx 方法引入不到。根本原因就是你执行的路径下的异常文件代码中比如上述 path\python-3.x.xx\lib\site-packages\paddleocr\paddleocr.py确实没有这些需要的目录或文件 解决方案 方案1 重新安装版本 1使用 CMD 命令 pip uninstall paddlepaddle paddleocr 卸载 paddleocr 2安装指定版本的 paddlepaddle
pip install paddlepaddle2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple3安装指定版本的 paddleocr
pip install paddleocr2.5.0.3注如果你本地下载过 opencv、paddleocr 的源码可通过 paddleocr.py 代码查看你应该需要安装的 paddleocr 版本号 方案2 补全依赖目录或文件 根据异常提示将缺省的文件或方法从源码中拷贝到 CMD 执行环境中逐步补全依赖目录或文件解决但比较繁琐。 比如在 paddleocr-2.6.1.3 版本中 paddleocr.py 代码的依赖是
from ppocr.utils.utility import check_and_read, get_image_file_list但在 ppocr.utils.utility 这个对象中提供的函数却是 check_and_read_gif自然是会执行异常。
通过方案1或方案2操作后可通过 paddleocr --help 校验环境是否OK。 异常三Please use PaddlePaddle with GPU version
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别PP-OCR文本检测识别
D:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCRpython ./tools/infer/predict_det.py --image_dir./doc/imgs/00018069.jpg --det_model_dir./inference/ch_PP-OCRv3_rec_infer/
E0608 16:27:13.135995 15300 analysis_config.cc:110] Please use PaddlePaddle with GPU version.异常原因 实验机器不支持GPU模式。 解决方案 确保你已安装了 CPU 版本的 PaddlePaddle。 通过将 --use_gpu 参数设置为 False您告诉 PaddleOCR 在 CPU 上运行不使用 GPU如
paddleocr --image_dir ./doc/imgs/japan_2.jpg --use_angle_cls true --use_gpu false异常四ModuleNotFoundError: No module named ‘ppocr’
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别PP-OCR文本检测识别 from ppocr.utils.logging import get_logger
ModuleNotFoundError: No module named ppocr异常信息 ppocr importlib.import_module(., ppocr)File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\importlib\__init__.py, line 127, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File frozen importlib._bootstrap, line 1030, in _gcd_importFile frozen importlib._bootstrap, line 1007, in _find_and_loadFile frozen importlib._bootstrap, line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named ppocr异常原因 ModuleNotFoundError: No module named ‘ppocr’ 错误表明您缺少了名为 ‘ppocr’ 的模块。这可能是由于以下原因之一导致的
缺少依赖库 ‘ppocr’ 模块可能依赖其他库或模块。请确保您已经安装了所有必需的依赖库。您可以通过运行 pip install -r requirements.txt 命令安装项目所需的依赖库。缺少 ‘ppocr’ 模块 请确保 ‘ppocr’ 模块已经正确地安装在您的环境中。您可以使用 pip list 命令查看已安装的模块列表确认 ‘ppocr’ 模块是否存在。模块路径问题 如果 ‘ppocr’ 模块不在默认的模块搜索路径中您需要将其路径添加到 Python 搜索路径中。可以通过在脚本中添加以下代码来添加模块路径
import sys
# 请确保将 /path/to/ppocr 替换为实际 ppocr 模块所在的路径
sys.path.append(/path/to/ppocr)注意请确保将 /path/to/ppocr 替换为实际 ‘ppocr’ 模块所在的路径。如果还是不行可参考下面的解决方案 解决方案
# 获取当前脚本文件的绝对路径所在的目录路径并将其赋值给变量 __dir__。
__dir__ os.path.dirname(os.path.abspath(__file__))
# 将当前脚本文件的绝对路径所在的目录路径添加到Python的模块搜索路径中。
sys.path.append(__dir__)
# 将当前脚本文件的上一级目录路径添加到Python的模块搜索路径中。os.path.join(__dir__, ..) 用于获取上一级目录的路径os.path.abspath() 用于获取绝对路径。
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, ..)))注与异常一类似。 异常五UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xae’ in position 2: illegal multibyte sequence
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别 - OCR模型对比 异常现象 在使用 PaddlePaddle-OCRv2 (PP-OCRv2) 进行实验时出现异常
[2023/06/09 19:06:19] ppocr INFO: Predicts of ../COCO-text/COCO_test\1087034.jpg:(皖S, 0.4052684009075165)
[2023/06/09 19:06:19] ppocr INFO: Predicts of ../COCO-text/COCO_test\1087141.jpg:(S AVe, 0.7147024273872375)
[2023/06/09 19:06:19] ppocr INFO: Predicts of ../COCO-text/COCO_test\1087170.jpg:(®, 0.054067403078079224)
Traceback (most recent call last):File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\pp_ocr_v2.py, line 156, in modulerec(utility.parse_args(), out_path, input_org, rec_model_dir, show False)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\pp_ocr_v2.py, line 106, in recf.write(str(rec_res[ino]))
UnicodeEncodeError: gbk codec cant encode character \xae in position 2: illegal multibyte sequence异常原因 这个错误是由于在写入文件时遇到了无法编码的字符导致的。根据错误信息似乎是在将结果写入文件时遇到了特殊字符 ‘\xae’导致无法使用 ‘gbk’ 编码进行写入。
解决方案 尝试修改文件编码方式将其设置为支持特殊字符的编码方式例如 encodingutf-8 。比如如果这里现在是写文件遇到特殊字符异常那么就在读文件时将特殊字符进行 UTF-8 读取。
for ino in range(len(img_list)):logger.info(Predicts of {}:{}.format(valid_image_file_list[ino], rec_res[ino]))if save:cv2.imwrite(os.path.join(out_path, valid_image_file_list[ino].split(/)[-1].split(.)[0] _rec .jpg), img_list[ino])with open(os.path.join(out_path, valid_image_file_list[ino].split(/)[-1].split(.)[0] .txt), w, encodingutf-8) as f:f.write(str(rec_res[ino]))异常六ValueError: not find model file path ./inference/rec_r50_vd_srn_train/inference.pdmodel
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别 - OCR模型对比
[2023/06/12 15:32:34] ppocr INFO: 开始 ...
yes
Traceback (most recent call last):File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\pp_ocr_srn.py, line 156, in modulerec(utility.parse_args(), out_path, input_org, rec_model_dir, rec_image_shape 1, 64, 256, rec_char_type en, rec_algorithm SRN, show False)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\pp_ocr_srn.py, line 68, in rectext_recognizer TextRecognizer(args)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\tools\infer\predict_rec.py, line 74, in __init__utility.create_predictor(args, rec, logger)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\tools\infer\utility.py, line 174, in create_predictorraise ValueError(not find model file path {}.format(
ValueError: not find model file path ./inference/rec_r50_vd_srn_train/inference.pdmodel异常原因 该错误提示表明代码无法在指定路径 ./inference/rec_r50_vd_srn_train/inference.pdmodel 中找到所需的模型文件。 解决方案 1检查模型文件路径是否正确确保模型文件 inference.pdmodel 存在于指定的路径 ./inference/rec_r50_vd_srn_train/ 下并且路径名称的大小写与实际文件系统匹配。如果文件在这个目录下那么就是相对路径不全导致找不到文件。 2重新生成模型文件放于当前目前下。 异常七TypeError: ‘’ not supported between instances of ‘tuple’ and ‘float’
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别 - OCR模型对比 异常原因 OCR 图片识别结果的数据结构与源码需要解析取值的数据结构不兼容 解决方案 移除 OCR 图片识别结果的外部一维
# 图片识别
result ocr.ocr(img_path)
print(OCR 图片识别结果, result)# 通过使用 result result[0] 移除外部的一维来解决 paddleocr\tools\infer\utility.py 文件中 draw_ocr 函数的 TypeError: not supported between instances of tuple and float
result result[0]
# print(移除一维后图片识别结果, result)# 保存可视化OCR检测识别结果
save_ocr(img_path, out_path, result, font)注异常现象的处理方式与异常八相反 异常八TypeError: ‘float’ object is not subscriptable
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别 - OCR模型对比 异常原因 OCR 图片识别结果的数据结构与源码需要解析取值的数据结构不兼容
Traceback (most recent call last):File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\ocr_img_apply.py, line 55, in moduleocr_img(img_path)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\ocr_img_util.py, line 49, in ocr_imgsave_ocr(img_path, out_path, result, font)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\ocr_img_util.py, line 65, in save_ocrtxts [line[1][0] for line in result]File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\ocr_img_util.py, line 65, in listcomptxts [line[1][0] for line in result]
TypeError: float object is not subscriptable异常原因 OCR 图片识别结果的数据结构与源码需要解析取值的数据结构不兼容 解决方案 直接使用 OCR 源码检测识别的结果进行可视化保存
def ocr_img(img_path):print(OCR 图片识别地址, img_path)# 图片识别result ocr.ocr(img_path)print(OCR 图片识别结果, result)# 通过使用 result result[0] 移除外部的一维来解决 paddleocr\tools\infer\utility.py 文件中 draw_ocr 函数的 TypeError: not supported between instances of tuple and float# result result[0]# print(移除一维后图片识别结果, result)# 保存可视化OCR检测识别结果save_ocr(img_path, out_path, result, font)注异常现象的处理方式与异常六相反 异常九AttributeError: module ‘numpy’ has no attribute ‘int’.
实验案例 PaddleOCR #使用PaddleOCR进行光学字符识别PP-OCR文本检测识别
Traceback (most recent call last):File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\ocr_img_apply.py, line 55, in moduleocr_img(img_path)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\applications\ocr_img_util.py, line 41, in ocr_imgresult ocr.ocr(img_path)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\paddleocr.py, line 474, in ocrdt_boxes, rec_res self.__call__(img, cls)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\tools\infer\predict_system.py, line 69, in __call__dt_boxes, elapse self.text_detector(img)File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\tools\infer\predict_det.py, line 242, in __call__File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\ppocr\postprocess\db_postprocess.py, line 188, in __call__boxes, scores self.boxes_from_bitmap(pred[batch_index], mask,File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\ppocr\postprocess\db_postprocess.py, line 82, in boxes_from_bitmapscore self.box_score_fast(pred, points.reshape(-1, 2))File d:\Ct_ iSpace\Tan\opencv\learnopencv-master\Optical-Character-Recognition-using-PaddleOCR\PaddleOCR\ppocr\postprocess\db_postprocess.py, line 140, in box_score_fastxmin np.clip(np.floor(box[:, 0].min()).astype(np.int), 0, w - 1)File D:\Tp_Mylocal\20_Install\python-3.9.13\lib\site-packages\numpy\__init__.py, line 305, in __getattr__raise AttributeError(__former_attrs__[attr])
AttributeError: module numpy has no attribute int.
np.int was a deprecated alias for the builtin int. To avoid this error in existing code, use int by itself. Doing this will not modify any behavior and is safe. When replacing np.int, you may wish to use e.g. np.int64 or np.int32 to specify the precision. If you
wish to review your current use, check the release note link for additional information.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations异常原因 在较旧的 NumPy 版本1.19及更早版本中np.int 是合法的别名。但是从 NumPy 1.20 版本开始np.int 被弃用并引发了警告。从 NumPy 1.21 版本开始np.int 完全被移除不再可用。
因此如果你使用的是 NumPy 1.20或更高版本将 np.int 替换为 int 是推荐的做法。对于较旧的 NumPy 版本np.int 仍然可用但不推荐使用建议迁移到使用 int 类型。 解决方案
在代码中将 np.int 替换为 int。如果代码中存在其他使用了 np.int 的地方也需要进行相应的替换。确保你正在使用最新版本的 NumPy 库。
可以使用以下命令升级到最新版本的 NumPy
pip install --upgrade numpy或者升级到指定版本的 NumPy
pip install numpy1.21.1注意如果你的项目有其他依赖项依赖于较新的 NumPy 版本降级 NumPy 可能会导致冲突。在执行降级操作之前请确保你的项目不会受到这种影响并仔细考虑可能的后果。
opencv源码参考文档 https://learnopencv.com/optical-character-recognition-using-paddleocr/