Fix SKU-110K HUB: `OSError` (#5106)
Browse files- utils/datasets.py +16 -6
utils/datasets.py
CHANGED
@@ -943,12 +943,22 @@ def dataset_stats(path='coco128.yaml', autodownload=False, verbose=False, profil
|
|
943 |
return False, None, path
|
944 |
|
945 |
def hub_ops(f, max_dim=1920):
|
946 |
-
# HUB ops for 1 image 'f'
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
952 |
|
953 |
zipped, data_dir, yaml_path = unzip(Path(path))
|
954 |
with open(check_yaml(yaml_path), errors='ignore') as f:
|
|
|
943 |
return False, None, path
|
944 |
|
945 |
def hub_ops(f, max_dim=1920):
|
946 |
+
# HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing
|
947 |
+
f_new = im_dir / Path(f).name # dataset-hub image filename
|
948 |
+
try: # use PIL
|
949 |
+
im = Image.open(f)
|
950 |
+
r = max_dim / max(im.height, im.width) # ratio
|
951 |
+
if r < 1.0: # image too large
|
952 |
+
im = im.resize((int(im.width * r), int(im.height * r)))
|
953 |
+
im.save(f_new, quality=75) # save
|
954 |
+
except Exception as e: # use OpenCV
|
955 |
+
print(f'WARNING: HUB ops PIL failure {f}: {e}')
|
956 |
+
im = cv2.imread(f)
|
957 |
+
im_height, im_width = im.shape[:2]
|
958 |
+
r = max_dim / max(im_height, im_width) # ratio
|
959 |
+
if r < 1.0: # image too large
|
960 |
+
im = cv2.resize(im, (int(im_width * r), int(im_height * r)), interpolation=cv2.INTER_LINEAR)
|
961 |
+
cv2.imwrite(str(f_new), im)
|
962 |
|
963 |
zipped, data_dir, yaml_path = unzip(Path(path))
|
964 |
with open(check_yaml(yaml_path), errors='ignore') as f:
|