|
|
|
|
|
|
|
|
import torch |
|
|
import torch |
|
|
import torch.nn.functional as F |
|
|
import torch.nn.functional as F |
|
|
import yaml |
|
|
import yaml |
|
|
from PIL import Image, ExifTags |
|
|
|
|
|
|
|
|
from PIL import Image, ImageOps, ExifTags |
|
|
from torch.utils.data import Dataset |
|
|
from torch.utils.data import Dataset |
|
|
from tqdm import tqdm |
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exif_transpose(image): |
|
|
def exif_transpose(image): |
|
|
""" |
|
|
""" |
|
|
Transpose a PIL image accordingly if it has an EXIF Orientation tag. |
|
|
Transpose a PIL image accordingly if it has an EXIF Orientation tag. |
|
|
From https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py |
|
|
|
|
|
|
|
|
Inplace version of https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py exif_transpose() |
|
|
|
|
|
|
|
|
:param image: The image to transpose. |
|
|
:param image: The image to transpose. |
|
|
:return: An image. |
|
|
:return: An image. |
|
|
|
|
|
|
|
|
with open(im_file, 'rb') as f: |
|
|
with open(im_file, 'rb') as f: |
|
|
f.seek(-2, 2) |
|
|
f.seek(-2, 2) |
|
|
if f.read() != b'\xff\xd9': # corrupt JPEG |
|
|
if f.read() != b'\xff\xd9': # corrupt JPEG |
|
|
Image.open(im_file).save(im_file, format='JPEG', subsampling=0, quality=100) # re-save image |
|
|
|
|
|
|
|
|
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' |
|
|
msg = f'{prefix}WARNING: {im_file}: corrupt JPEG restored and saved' |
|
|
|
|
|
|
|
|
# verify labels |
|
|
# verify labels |