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

Fix cv2.imwrite on non-ASCII paths #7139

Merged
merged 10 commits into from
Mar 25, 2022
3 changes: 1 addition & 2 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import sys
from pathlib import Path

import cv2
import torch
import torch.backends.cudnn as cudnn

Expand All @@ -41,7 +40,7 @@

from models.common import DetectMultiBackend
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, time_sync
Expand Down
3 changes: 2 additions & 1 deletion hubconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=Tr
# Verify inference
from pathlib import Path

import cv2
import numpy as np
from PIL import Image

from utils.general import cv2

imgs = ['data/images/zidane.jpg', # filename
Path('data/images/zidane.jpg'), # Path
'https://ultralytics.com/images/zidane.jpg', # URI
Expand Down
6 changes: 1 addition & 5 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from urllib.parse import urlparse
from zipfile import ZipFile

import cv2
import numpy as np
import torch
import torch.nn.functional as F
Expand All @@ -29,12 +28,9 @@

from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
from utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, check_dataset, check_requirements, check_yaml, clean_str,
segments2boxes, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn)
cv2, segments2boxes, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn)
from utils.torch_utils import torch_distributed_zero_first

# Remap
cv2.imread = lambda x: cv2.imdecode(np.fromfile(x, np.uint8), cv2.IMREAD_COLOR) # for Chinese filenames

# Parameters
HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp' # include image suffixes
Expand Down
17 changes: 16 additions & 1 deletion utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,5 +904,20 @@ def increment_path(path, exist_ok=False, sep='', mkdir=False):
return path


# Variables
# OpenCV Chinese-friendly functions ------------------------------------------------------------------------------------
def imread(path):
return cv2.imdecode(np.fromfile(path, np.uint8), cv2.IMREAD_COLOR)


def imwrite(path, im):
try:
cv2.imencode(Path(path).suffix, im)[1].tofile(path)
return True
except Exception:
return False


cv2.imread, cv2.imwrite = imread, imwrite # redefine

# Variables ------------------------------------------------------------------------------------------------------------
NCOLS = 0 if is_docker() else shutil.get_terminal_size().columns # terminal window size for tqdm
6 changes: 1 addition & 5 deletions utils/loggers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import torch
from torch.utils.tensorboard import SummaryWriter

from utils.general import colorstr, emojis
from utils.general import colorstr, cv2, emojis
from utils.loggers.wandb.wandb_utils import WandbLogger
from utils.plots import plot_images, plot_results
from utils.torch_utils import de_parallel
Expand Down Expand Up @@ -147,10 +147,6 @@ def on_train_end(self, last, best, plots, epoch, results):
files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] # filter

if self.tb:
import cv2
import numpy as np

cv2.imread = lambda x: cv2.imdecode(np.fromfile(x, np.uint8), cv2.IMREAD_COLOR) # remap for Chinese files
for f in files:
self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC')

Expand Down