整治PPOCRLabel中cv2文件读取问题(更新中)

PPOCRLabel

  • 使用PPOCRLabel对ocr预标注结果进行纠正
  • 由于PaddleOCR代码库十分混乱,路径经常乱掉pip和代码库的代码,经常报错,ocr也是使用paddleocr的pip包;
安装
shell 复制代码
pip install PPOCRLabel==2.1.3
启动
shell 复制代码
PPOCRLabel --lang ch

修改记录

修改1:
  • 报错:AttributeError: 'NoneType' object has no attribute 'shape'
angular2html 复制代码
'NoneType' object has no attribute 'shape'
Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\libs\autoDialog.py", line 41, in run
    h, w, _ = cv2.imdecode(np.fromfile(Imgpath, dtype=np.uint8), 1).shape
AttributeError: 'NoneType' object has no attribute 'shape'
  • 原因:cv2不支持读取带有中文字符路径文件
python 复制代码
  h, w, _ = cv2.imdecode(np.fromfile(Imgpath, dtype=np.uint8), 1).shape
  • 解决:使用PIL替换cv2, 将上述代码替换为
python 复制代码
# 修改文件:"D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\libs\autoDialog.py", line 41
# 增加PIL读取图片函数
from PIL import Image
def load_image(image_path: str, return_chw: bool = True, size: tuple = None):
    image = Image.open(image_path).convert("RGB")
    if size is not None:
        image = image.resize(size)  # resize image
    image = np.asarray(image)
    image = image[:, :, ::-1]  # flip color channels from RGB to BGR
    w, h = image.shape[1], image.shape[0]  # update size after resize
    if return_chw:
        image = image.transpose(2, 0, 1)
    return image, (w, h)

# 替换上面代码
try:
    image_data, (w, h) = load_image(Imgpath, return_chw=False)
except Exception as e:
    print(f"load file {Imgpath} fail!")
    continue
修改2
  • 报错:AttributeError: 'NoneType' object has no attribute 'shape'
python 复制代码
Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\PPOCRLabel.py", line 1889, in saveFile
    self._saveFile(imgidx, mode=mode)
  File "D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\PPOCRLabel.py", line 1934, in _saveFile
    self.openNextImg()
  File "D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\PPOCRLabel.py", line 1880, in openNextImg
    self.loadFile(filename)
  File "D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\PPOCRLabel.py", line 1550, in loadFile
    height, width, depth = cvimg.shape
AttributeError: 'NoneType' object has no attribute 'shape'
  • 原因还是cv2不能读取中文路径文件
  • 解决:
python 复制代码
# 修改: File "D:\ProgramData\Anaconda3\envs\paddle\lib\site-packages\PPOCRLabel\PPOCRLabel.py", line 1550, in loadFile
# cvimg = cv2.imdecode(np.fromfile(unicodeFilePath, dtype=np.uint8), 1)
cvimg, _ = load_image(unicodeFilePath, return_chw=False)
修改3
  • 报错:error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'

    • 报错描述:在对PPOCRLABEL的框进行重新识别是,发生如下报错:

      复制代码
      Can not recognise the detection box in xxxx,png. Please change manually'

      unicodeFilePath is J:\data\mllm-data\xxxxxxxxx\wKh2CWERPJOAY2x-AAE62o598k0620.png
      OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:3143: error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'

    • 原因是我们的ocr预标注数据Label.txt是使用PIL读取图片数据,调用ppocr进行生产的(并不是在PPOCRLabel工具内部生产的), 当我们修改数据框后, PPOCRLabel尝试再次使用cv2进行读取原图,此时由于cv2对路径较为敏感,经常会读取文件失败,才会出现如上情况

  • 解决:

    依旧是修改PPOCRLabel源码, 把cv2读取改为PIL读取,就不惯着cv2的臭毛病...

python 复制代码
# 修改如下代码
 def reRecognition(self):
    #img = cv2.imdecode(np.fromfile(self.filePath,dtype=np.uint8),1)
    img, _ = load_image(self.filePath, return_chw=False)
相关推荐
yzx991013几秒前
AI心理助手开发文档
人工智能·深度学习·机器学习
图灵学术计算机论文辅导16 分钟前
论文推荐|迁移学习+多模态特征融合
论文阅读·人工智能·深度学习·计算机网络·算法·计算机视觉·目标跟踪
CallZhang21023 分钟前
Vision Master的C#脚本与opencv联合编程
opencv·计算机视觉·c#·视觉检测
一百天成为python专家25 分钟前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian29 分钟前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
轻松Ai享生活1 小时前
GitHub Repo 骨架:Makefile + CUDA 入门程序
人工智能
三年呀1 小时前
标题:移动端安全加固:发散创新,筑牢安全防线引言:随着移动互联网
网络·python·安全
用户5191495848451 小时前
对抗性工程实践:利用AI自动化构建GitHub仓库的虚假提交历史
人工智能·aigc
riveting1 小时前
重塑工业设备制造格局:明远智睿 T113-i 的破局之道
人工智能·物联网·制造·t113·明远智睿
zzywxc7872 小时前
详细探讨AI在金融、医疗、教育和制造业四大领域的具体落地案例,并通过代码、流程图、Prompt示例和图表等方式展示这些应用的实际效果。
开发语言·javascript·人工智能·深度学习·金融·prompt·流程图