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

np.fromfile() Chinese image paths fix #6979

Merged
merged 5 commits into from
Mar 23, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}: '

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down