Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXIF orientation support #4529

Merged
merged 21 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- some AI Tools were not sending responses properly (<https://github.com/openvinotoolkit/cvat/issues/4432>)
- Unable to upload annotations (<https://github.com/openvinotoolkit/cvat/pull/4513>)
- Fix build dependencies for Siammask (<https://github.com/openvinotoolkit/cvat/pull/4486>)
- Bug: Exif orientation information handled incorrectly (<https://github.com/openvinotoolkit/cvat/pull/4529>)

### Security
- TDB
Expand Down
8 changes: 8 additions & 0 deletions cvat-core/src/frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
value: height,
writable: false,
},
/**
* task ID
* @name tid
* @type {integer}
* @memberof module:API.cvat.classes.FrameData
* @readonly
* @instance
*/
tid: {
value: taskID,
writable: false,
Expand Down
43 changes: 41 additions & 2 deletions cvat/apps/engine/media_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io
import itertools
import struct
from enum import IntEnum
from abc import ABC, abstractmethod
from contextlib import closing

Expand All @@ -29,6 +30,20 @@
from cvat.apps.engine.mime_types import mimetypes
from utils.dataset_manifest import VideoManifestManager, ImageManifestManager

ORIENTATION_EXIF_TAG = 274


class ORIENTATION(IntEnum):
NORMAL_HORIZONTAL=1
MIRROR_HORIZONTAL=2
NORAMAL_180_ROTATED=3
ActiveChooN marked this conversation as resolved.
Show resolved Hide resolved
MIRROR_VERTICAL=4
MIRROR_HORIZONTAL_270_ROTATED=5
NORAMAL_90_ROTATED=6
ActiveChooN marked this conversation as resolved.
Show resolved Hide resolved
MIRROR_HORIZONTAL_90_ROTATED=7
NORAMAL_270_ROTATED=8
ActiveChooN marked this conversation as resolved.
Show resolved Hide resolved


def get_mime(name):
for type_name, type_def in MEDIA_TYPES.items():
if type_def['has_mime_type'](name):
Expand Down Expand Up @@ -62,6 +77,27 @@ def sort(images, sorting_method=SortingMethod.LEXICOGRAPHICAL, func=None):
else:
raise NotImplementedError()

def image_size_within_orientation(img: Image):
orientation = img.getexif().get(ORIENTATION_EXIF_TAG, 1)
ActiveChooN marked this conversation as resolved.
Show resolved Hide resolved
nmanovic marked this conversation as resolved.
Show resolved Hide resolved
if orientation > 4:
return img.height, img.width
return img.width, img.height

def rotate_within_exif(img: Image):
orientation = img.getexif().get(ORIENTATION_EXIF_TAG, ORIENTATION.NORMAL_HORIZONTAL)
if orientation in [ORIENTATION.NORAMAL_180_ROTATED, ORIENTATION.MIRROR_VERTICAL]:
ActiveChooN marked this conversation as resolved.
Show resolved Hide resolved
img = img.rotate(180, expand=True)
elif orientation in [ORIENTATION.MIRROR_HORIZONTAL_270_ROTATED, ORIENTATION.NORAMAL_270_ROTATED]:
img = img.rotate(90, expand=True)
elif orientation in [ORIENTATION.NORAMAL_90_ROTATED, ORIENTATION.MIRROR_HORIZONTAL_90_ROTATED]:
img = img.rotate(270, expand=True)
if orientation in [
ORIENTATION.MIRROR_HORIZONTAL, ORIENTATION.MIRROR_VERTICAL,
ORIENTATION.MIRROR_HORIZONTAL_270_ROTATED ,ORIENTATION.MIRROR_HORIZONTAL_90_ROTATED,
]:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
return img

class IMediaReader(ABC):
def __init__(self, source_path, step, start, stop, dimension):
self._source_path = source_path
Expand All @@ -85,11 +121,13 @@ def get_progress(self, pos):
@staticmethod
def _get_preview(obj):
PREVIEW_SIZE = (256, 256)

if isinstance(obj, io.IOBase):
preview = Image.open(obj)
else:
preview = obj
preview.thumbnail(PREVIEW_SIZE)
preview = rotate_within_exif(preview)

return preview.convert('RGB')

Expand Down Expand Up @@ -173,7 +211,7 @@ def get_image_size(self, i):
properties = ValidateDimension.get_pcd_properties(f)
return int(properties["WIDTH"]), int(properties["HEIGHT"])
img = Image.open(self._source_path[i])
return img.width, img.height
return image_size_within_orientation(img)

def reconcile(self, source_files, step=1, start=0, stop=None, dimension=DimensionType.DIM_2D, sorting_method=None):
# FIXME
Expand Down Expand Up @@ -314,7 +352,7 @@ def get_image_size(self, i):
properties = ValidateDimension.get_pcd_properties(f)
return int(properties["WIDTH"]), int(properties["HEIGHT"])
img = Image.open(io.BytesIO(self._zip_source.read(self._source_path[i])))
return img.width, img.height
return image_size_within_orientation(img)

def get_image(self, i):
if self._dimension == DimensionType.DIM_3D:
Expand Down Expand Up @@ -538,6 +576,7 @@ def __init__(self, quality, dimension=DimensionType.DIM_2D):
@staticmethod
def _compress_image(image_path, quality):
image = image_path.to_image() if isinstance(image_path, av.VideoFrame) else Image.open(image_path)
image = rotate_within_exif(image)
# Ensure image data fits into 8bit per pixel before RGB conversion as PIL clips values on conversion
if image.mode == "I":
# Image mode is 32bit integer pixels.
Expand Down
8 changes: 6 additions & 2 deletions utils/dataset_manifest/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,18 @@ def __iter__(self):
if idx in self.range_:
image = next(sources)
img = Image.open(image, mode='r')
orientation = img.getexif().get(274, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marishka17 , probably you want to look at these changes and test them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nmanovic, Everything works fine.

img_name = os.path.relpath(image, self._data_dir) if self._data_dir \
else os.path.basename(image)
name, extension = os.path.splitext(img_name)
width, height = img.width, img.height
if orientation > 4:
width, height = height, width
image_properties = {
'name': name.replace('\\', '/'),
'extension': extension,
'width': img.width,
'height': img.height,
'width': width,
'height': height,
}
if self._meta and img_name in self._meta:
image_properties['meta'] = self._meta[img_name]
Expand Down