mmocr/mmocr/utils/img_util.py
lizz 44ca9c2a61
Remove usage of \ (#49)
* Remove usage of \

Signed-off-by: lizz <lizz@sensetime.com>

* rebase

Signed-off-by: lizz <lizz@sensetime.com>

* typos

Signed-off-by: lizz <lizz@sensetime.com>

* Remove test dependency on tools/

Signed-off-by: lizz <lizz@sensetime.com>

* Remove usage of \

Signed-off-by: lizz <lizz@sensetime.com>

* rebase

Signed-off-by: lizz <lizz@sensetime.com>

* typos

Signed-off-by: lizz <lizz@sensetime.com>

* Remove test dependency on tools/

Signed-off-by: lizz <lizz@sensetime.com>

* typo

Signed-off-by: lizz <lizz@sensetime.com>

* KIE in keywords

Signed-off-by: lizz <lizz@sensetime.com>

* some renames

Signed-off-by: lizz <lizz@sensetime.com>

* kill isort skip

Signed-off-by: lizz <lizz@sensetime.com>

* aggregation discrimination

Signed-off-by: lizz <lizz@sensetime.com>

* aggregation discrimination

Signed-off-by: lizz <lizz@sensetime.com>

* tiny

Signed-off-by: lizz <lizz@sensetime.com>

* fix bug: model infer on cpu

Co-authored-by: Hongbin Sun <hongbin306@gmail.com>
2021-04-06 12:16:46 +00:00

35 lines
1.1 KiB
Python

import os
import mmcv
def drop_orientation(img_file):
"""Check if the image has orientation information. If yes, ignore it by
converting the image format to png, and return new filename, otherwise
return the original filename.
Args:
img_file(str): The image path
Returns:
The converted image filename with proper postfix
"""
assert isinstance(img_file, str)
assert img_file
# read imgs with ignoring orientations
img = mmcv.imread(img_file, 'unchanged')
# read imgs with orientations as dataloader does when training and testing
img_color = mmcv.imread(img_file, 'color')
# make sure imgs have no orientation info, or annotation gt is wrong.
if img.shape[:2] == img_color.shape[:2]:
return img_file
target_file = os.path.splitext(img_file)[0] + '.png'
# read img with ignoring orientation information
img = mmcv.imread(img_file, 'unchanged')
mmcv.imwrite(img, target_file)
os.remove(img_file)
print(f'{img_file} has orientation info. Ignore it by converting to png')
return target_file