RcINS pre-commit-ci[bot] glenn-jocher commited on
Commit
d115bbf
·
unverified ·
1 Parent(s): a0a4adf

Fix `cv2.imwrite` on non-ASCII paths (#7139)

Browse files

* Fix imwrite on non-ASCII paths

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update general.py

* Update __init__.py

* Update __init__.py

* Update datasets.py

* Update hubconf.py

* Update detect.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update general.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <[email protected]>

Files changed (5) hide show
  1. detect.py +1 -2
  2. hubconf.py +2 -1
  3. utils/datasets.py +1 -5
  4. utils/general.py +16 -1
  5. utils/loggers/__init__.py +1 -5
detect.py CHANGED
@@ -29,7 +29,6 @@ import os
29
  import sys
30
  from pathlib import Path
31
 
32
- import cv2
33
  import torch
34
  import torch.backends.cudnn as cudnn
35
 
@@ -41,7 +40,7 @@ ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
41
 
42
  from models.common import DetectMultiBackend
43
  from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
44
- from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,
45
  increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
46
  from utils.plots import Annotator, colors, save_one_box
47
  from utils.torch_utils import select_device, time_sync
 
29
  import sys
30
  from pathlib import Path
31
 
 
32
  import torch
33
  import torch.backends.cudnn as cudnn
34
 
 
40
 
41
  from models.common import DetectMultiBackend
42
  from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
43
+ from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
44
  increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
45
  from utils.plots import Annotator, colors, save_one_box
46
  from utils.torch_utils import select_device, time_sync
hubconf.py CHANGED
@@ -127,10 +127,11 @@ if __name__ == '__main__':
127
  # Verify inference
128
  from pathlib import Path
129
 
130
- import cv2
131
  import numpy as np
132
  from PIL import Image
133
 
 
 
134
  imgs = ['data/images/zidane.jpg', # filename
135
  Path('data/images/zidane.jpg'), # Path
136
  'https://ultralytics.com/images/zidane.jpg', # URI
 
127
  # Verify inference
128
  from pathlib import Path
129
 
 
130
  import numpy as np
131
  from PIL import Image
132
 
133
+ from utils.general import cv2
134
+
135
  imgs = ['data/images/zidane.jpg', # filename
136
  Path('data/images/zidane.jpg'), # Path
137
  'https://ultralytics.com/images/zidane.jpg', # URI
utils/datasets.py CHANGED
@@ -18,7 +18,6 @@ from threading import Thread
18
  from urllib.parse import urlparse
19
  from zipfile import ZipFile
20
 
21
- import cv2
22
  import numpy as np
23
  import torch
24
  import torch.nn.functional as F
@@ -29,12 +28,9 @@ from tqdm import tqdm
29
 
30
  from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
31
  from utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, check_dataset, check_requirements, check_yaml, clean_str,
32
- segments2boxes, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn)
33
  from utils.torch_utils import torch_distributed_zero_first
34
 
35
- # Remap
36
- cv2.imread = lambda x: cv2.imdecode(np.fromfile(x, np.uint8), cv2.IMREAD_COLOR) # for Chinese filenames
37
-
38
  # Parameters
39
  HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
40
  IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp' # include image suffixes
 
18
  from urllib.parse import urlparse
19
  from zipfile import ZipFile
20
 
 
21
  import numpy as np
22
  import torch
23
  import torch.nn.functional as F
 
28
 
29
  from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
30
  from utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, check_dataset, check_requirements, check_yaml, clean_str,
31
+ cv2, segments2boxes, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn)
32
  from utils.torch_utils import torch_distributed_zero_first
33
 
 
 
 
34
  # Parameters
35
  HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
36
  IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp' # include image suffixes
utils/general.py CHANGED
@@ -904,5 +904,20 @@ def increment_path(path, exist_ok=False, sep='', mkdir=False):
904
  return path
905
 
906
 
907
- # Variables
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
908
  NCOLS = 0 if is_docker() else shutil.get_terminal_size().columns # terminal window size for tqdm
 
904
  return path
905
 
906
 
907
+ # OpenCV Chinese-friendly functions ------------------------------------------------------------------------------------
908
+ def imread(path):
909
+ return cv2.imdecode(np.fromfile(path, np.uint8), cv2.IMREAD_COLOR)
910
+
911
+
912
+ def imwrite(path, im):
913
+ try:
914
+ cv2.imencode(Path(path).suffix, im)[1].tofile(path)
915
+ return True
916
+ except Exception:
917
+ return False
918
+
919
+
920
+ cv2.imread, cv2.imwrite = imread, imwrite # redefine
921
+
922
+ # Variables ------------------------------------------------------------------------------------------------------------
923
  NCOLS = 0 if is_docker() else shutil.get_terminal_size().columns # terminal window size for tqdm
utils/loggers/__init__.py CHANGED
@@ -11,7 +11,7 @@ import pkg_resources as pkg
11
  import torch
12
  from torch.utils.tensorboard import SummaryWriter
13
 
14
- from utils.general import colorstr, emojis
15
  from utils.loggers.wandb.wandb_utils import WandbLogger
16
  from utils.plots import plot_images, plot_results
17
  from utils.torch_utils import de_parallel
@@ -147,10 +147,6 @@ class Loggers():
147
  files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] # filter
148
 
149
  if self.tb:
150
- import cv2
151
- import numpy as np
152
-
153
- cv2.imread = lambda x: cv2.imdecode(np.fromfile(x, np.uint8), cv2.IMREAD_COLOR) # remap for Chinese files
154
  for f in files:
155
  self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC')
156
 
 
11
  import torch
12
  from torch.utils.tensorboard import SummaryWriter
13
 
14
+ from utils.general import colorstr, cv2, emojis
15
  from utils.loggers.wandb.wandb_utils import WandbLogger
16
  from utils.plots import plot_images, plot_results
17
  from utils.torch_utils import de_parallel
 
147
  files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] # filter
148
 
149
  if self.tb:
 
 
 
 
150
  for f in files:
151
  self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC')
152