From 99eeef0706c014704b2eefb55b6ed7692e46892a Mon Sep 17 00:00:00 2001 From: yeshanliu <41566254+yeshanliu@users.noreply.github.com> Date: Wed, 23 Mar 2022 22:35:15 +0800 Subject: [PATCH] `np.fromfile()` Chinese image paths fix (#6979) * :tada: :new: now can read Chinese image path. use "cv2.imdecode(np.fromfile(f, np.uint8), cv2.IMREAD_COLOR)" instead of "cv2.imread(f)" for Chinese image path. * Update datasets.py * Update __init__.py Co-authored-by: Glenn Jocher --- utils/datasets.py | 3 +++ utils/loggers/__init__.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/utils/datasets.py b/utils/datasets.py index 8627344af7b4..f212e54633be 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -32,6 +32,9 @@ 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 diff --git a/utils/loggers/__init__.py b/utils/loggers/__init__.py index 866bdc4be2f5..ff6722ecd48a 100644 --- a/utils/loggers/__init__.py +++ b/utils/loggers/__init__.py @@ -148,6 +148,9 @@ def on_train_end(self, last, best, plots, epoch, results): 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')