From 8f61583efdd2c9d67b7a86d8e9069832130638c9 Mon Sep 17 00:00:00 2001 From: yeshanliu <41566254+yeshanliu@users.noreply.github.com> Date: Tue, 15 Mar 2022 10:39:28 +0800 Subject: [PATCH 1/3] :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. --- utils/datasets.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index 00d0d94e0847..862f2fe7094a 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -216,7 +216,7 @@ def __next__(self): else: # Read image self.count += 1 - img0 = cv2.imread(path) # BGR + img0 = cv2.imdecode(np.fromfile(path, np.uint8), cv2.IMREAD_COLOR) # BGR assert img0 is not None, f'Image Not Found {path}' s = f'image {self.count}/{self.nf} {path}: ' @@ -627,7 +627,7 @@ def load_image(self, i): if fn.exists(): # load npy im = np.load(fn) else: # read image - im = cv2.imread(f) # BGR + im = cv2.imdecode(np.fromfile(f, np.uint8), cv2.IMREAD_COLOR) # BGR assert im is not None, f'Image Not Found {f}' h0, w0 = im.shape[:2] # orig hw r = self.img_size / max(h0, w0) # ratio @@ -643,7 +643,7 @@ def cache_images_to_disk(self, i): # Saves an image as an *.npy file for faster loading f = self.npy_files[i] if not f.exists(): - np.save(f.as_posix(), cv2.imread(self.im_files[i])) + np.save(f.as_posix(), cv2.imdecode(np.fromfile(self.im_files[i], np.uint8), cv2.IMREAD_COLOR)) def load_mosaic(self, index): # YOLOv5 4-mosaic loader. Loads 1 image + 3 random images into a 4-image mosaic @@ -834,7 +834,7 @@ def extract_boxes(path=DATASETS_DIR / 'coco128'): # from utils.datasets import for im_file in tqdm(files, total=n): if im_file.suffix[1:] in IMG_FORMATS: # image - im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB + im = cv2.imdecode(np.fromfile(str(im_file), np.uint8), cv2.IMREAD_COLOR)[..., ::-1] # BGR to RGB h, w = im.shape[:2] # labels @@ -971,7 +971,7 @@ def hub_ops(f, max_dim=1920): im.save(f_new, 'JPEG', quality=75, optimize=True) # save except Exception as e: # use OpenCV print(f'WARNING: HUB ops PIL failure {f}: {e}') - im = cv2.imread(f) + im = cv2.imdecode(np.fromfile(f, np.uint8), cv2.IMREAD_COLOR) im_height, im_width = im.shape[:2] r = max_dim / max(im_height, im_width) # ratio if r < 1.0: # image too large From 354c3c85e46b61be0be7a248eab1000573944226 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 23 Mar 2022 15:15:54 +0100 Subject: [PATCH 2/3] Update datasets.py --- utils/datasets.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index 9f07a208eb5b..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 @@ -218,7 +221,7 @@ def __next__(self): else: # Read image self.count += 1 - img0 = cv2.imdecode(np.fromfile(path, np.uint8), cv2.IMREAD_COLOR) # BGR + img0 = cv2.imread(path) # BGR assert img0 is not None, f'Image Not Found {path}' s = f'image {self.count}/{self.nf} {path}: ' @@ -629,7 +632,7 @@ def load_image(self, i): if fn.exists(): # load npy im = np.load(fn) else: # read image - im = cv2.imdecode(np.fromfile(f, np.uint8), cv2.IMREAD_COLOR) # BGR + im = cv2.imread(f) # BGR assert im is not None, f'Image Not Found {f}' h0, w0 = im.shape[:2] # orig hw r = self.img_size / max(h0, w0) # ratio @@ -645,7 +648,7 @@ def cache_images_to_disk(self, i): # Saves an image as an *.npy file for faster loading f = self.npy_files[i] if not f.exists(): - np.save(f.as_posix(), cv2.imdecode(np.fromfile(self.im_files[i], np.uint8), cv2.IMREAD_COLOR)) + np.save(f.as_posix(), cv2.imread(self.im_files[i])) def load_mosaic(self, index): # YOLOv5 4-mosaic loader. Loads 1 image + 3 random images into a 4-image mosaic @@ -836,7 +839,7 @@ def extract_boxes(path=DATASETS_DIR / 'coco128'): # from utils.datasets import for im_file in tqdm(files, total=n): if im_file.suffix[1:] in IMG_FORMATS: # image - im = cv2.imdecode(np.fromfile(str(im_file), np.uint8), cv2.IMREAD_COLOR)[..., ::-1] # BGR to RGB + im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB h, w = im.shape[:2] # labels @@ -973,7 +976,7 @@ def hub_ops(f, max_dim=1920): im.save(f_new, 'JPEG', quality=75, optimize=True) # save except Exception as e: # use OpenCV print(f'WARNING: HUB ops PIL failure {f}: {e}') - im = cv2.imdecode(np.fromfile(f, np.uint8), cv2.IMREAD_COLOR) + im = cv2.imread(f) im_height, im_width = im.shape[:2] r = max_dim / max(im_height, im_width) # ratio if r < 1.0: # image too large From 524b9bba8687b271b9708020a2946cd05bf4db65 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 23 Mar 2022 15:20:01 +0100 Subject: [PATCH 3/3] Update __init__.py --- utils/loggers/__init__.py | 3 +++ 1 file changed, 3 insertions(+) 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')