当前位置:   article > 正文

【深度学习】yolov 图片训练的时候的遇到的warning: corrupt JPEG restored and saved

corrupt jpeg restored and saved

报错原因

是图片在dataset.py 走验证时报的错误。


if im.format.lower() in ('jpg', 'jpeg'):
            with open(im_file, 'rb') as f:
                f.seek(-2, 2)
                if f.read() != b'\xff\xd9':  # corrupt JPEG
                    ImageOps.exif_transpose(Image.open(im_file)).save(im_file, 'JPEG', subsampling=0, quality=100)
                    msg = f'{prefix}WARNING: {im_file}: corrupt JPEG restored and saved'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

ref:https://huggingface.co/spaces/nakamura196/yolov5-ndl-layout/blob/447b47ec77e6ea46fef0abba2594b11de7874676/ultralytics/yolov5/utils/datasets.py

在这里插入图片描述
f.seek(-2, 2) 是2是从尾部开始,offset=2个,也就是找到文件结尾的最后两个字符。
在Python中,f.seek(offset, from_what)是一个文件对象的方法,用于在文件中移动读取/写入位置。其中,offset表示要移动的字节偏移量,from_what表示起始位置。
具体地,from_what可以取以下三个值:
0:从文件开头开始计算偏移量(默认值)。
1:从当前位置开始计算偏移量。
2:从文件末尾开始计算偏移量。
在给定的代码示例中,f.seek(-2, 2)表示从文件末尾开始向前移动2个字节的位置。也就是说,读取/写入操作将在文件的倒数第二个字节处进行。

ref:https://github.com/ultralytics/yolov5/issues/916

这里也有人提bug了,但作者说是告诉的info不是个bug,他说的也对,又不是人家的问题造成的。

那么,其实就需要我们在数据集处理的时候,提早发现,并修复它。

可以看到这个不是错误,当

 if f.read() != b'\xff\xd9':  # corrupt JPEG
                    ImageOps.exif_transpose(Image.open(im_file)).save(im_file, 'JPEG', subsampling=0, quality=100)
                    msg = f'{prefix}WARNING: {im_file}: corrupt JPEG restored and saved'

        
  • 1
  • 2
  • 3
  • 4
  • 5

读取的文件结尾不是预期的时候,会

如何检查和修复

--------待定--------------------------------

import os
import re
from pathlib import Path
import shutil
from PIL import Image

source_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"images")
dst_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"res_images")

def image_to_jpg():
    """使用Pillow将图片文件转换为.jpg文件"""
    img_name_list = os.listdir(dst_path)
    for img_name in img_name_list:
        image_path =  os.path.join(dst_path,img_name)
        path = Path(image_path)
        print(path.parent, path.stem)
        exit("-==---")
        # Image.open(image_path).convert('RGB').save(image_path)

def mv_pic():
    img_name_list = os.listdir(source_path)
    for img_name in img_name_list:
        shutil.copy(os.path.join(source_path,img_name), os.path.join(dst_path,img_name))
def check_pic(im_file):
    try:
        # verify images
        im = Image.open(im_file)
        im.verify()  # PIL verify
        if im.format.lower() in ('jpg', 'jpeg'):
            with open(im_file, 'rb') as f:
                f.seek(-2, 2)
                if f.read() != b'\xff\xd9':  # corrupt JPEG
                    # ImageOps.exif_transpose(Image.open(im_file)).save(im_file, 'JPEG', subsampling=0, quality=100)
                    msg = f'WARNING: {im_file}: corrupt JPEG restored and saved'
                else:
                    print(im_file,"okkkk----------")
    except Exception as e:
        msg = f'WARNING: {im_file}: ignoring corrupt image/label: {e}'
        print(msg)
        
if __name__ == "__main__":
    # mv_pic()
    # image_to_jpg()
    img_name_list = os.listdir(dst_path)
    for img_name in img_name_list:
        image_path =  os.path.join(dst_path,img_name)
        check_pic(image_path)
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

这是解决方案,其实解不解决都行,训练过程中,它会自己给你解决。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/219437
推荐阅读
相关标签
  

闽ICP备14008679号