Update dataloaders.py (#8714)
Browse files* Update dataloaders.py
* Update dataloaders.py
- utils/dataloaders.py +14 -22
utils/dataloaders.py
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
Dataloaders and dataset utils
|
4 |
"""
|
5 |
|
|
|
6 |
import glob
|
7 |
import hashlib
|
8 |
import json
|
@@ -55,13 +56,10 @@ def get_hash(paths):
|
|
55 |
def exif_size(img):
|
56 |
# Returns exif-corrected PIL size
|
57 |
s = img.size # (width, height)
|
58 |
-
|
59 |
rotation = dict(img._getexif().items())[orientation]
|
60 |
if rotation in [6, 8]: # rotation 270 or 90
|
61 |
s = (s[1], s[0])
|
62 |
-
except Exception:
|
63 |
-
pass
|
64 |
-
|
65 |
return s
|
66 |
|
67 |
|
@@ -859,18 +857,13 @@ class LoadImagesAndLabels(Dataset):
|
|
859 |
|
860 |
|
861 |
# Ancillary functions --------------------------------------------------------------------------------------------------
|
862 |
-
def create_folder(path='./new'):
|
863 |
-
# Create folder
|
864 |
-
if os.path.exists(path):
|
865 |
-
shutil.rmtree(path) # delete output folder
|
866 |
-
os.makedirs(path) # make new output folder
|
867 |
-
|
868 |
-
|
869 |
def flatten_recursive(path=DATASETS_DIR / 'coco128'):
|
870 |
# Flatten a recursive directory by bringing all files to top level
|
871 |
-
new_path = Path(str(path)
|
872 |
-
|
873 |
-
|
|
|
|
|
874 |
shutil.copyfile(file, new_path / Path(file).name)
|
875 |
|
876 |
|
@@ -929,7 +922,7 @@ def autosplit(path=DATASETS_DIR / 'coco128/images', weights=(0.9, 0.1, 0.0), ann
|
|
929 |
for i, img in tqdm(zip(indices, files), total=n):
|
930 |
if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
|
931 |
with open(path.parent / txt[i], 'a') as f:
|
932 |
-
f.write('./
|
933 |
|
934 |
|
935 |
def verify_image_label(args):
|
@@ -1011,14 +1004,13 @@ def dataset_stats(path='coco128.yaml', autodownload=False, verbose=False, profil
|
|
1011 |
|
1012 |
def _unzip(path):
|
1013 |
# Unzip data.zip
|
1014 |
-
if str(path).endswith('.zip'): # path is data.
|
1015 |
-
assert Path(path).is_file(), f'Error unzipping {path}, file not found'
|
1016 |
-
ZipFile(path).extractall(path=path.parent) # unzip
|
1017 |
-
dir = path.with_suffix('') # dataset directory == zip name
|
1018 |
-
assert dir.is_dir(), f'Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/'
|
1019 |
-
return True, str(dir), _find_yaml(dir) # zipped, data_dir, yaml_path
|
1020 |
-
else: # path is data.yaml
|
1021 |
return False, None, path
|
|
|
|
|
|
|
|
|
|
|
1022 |
|
1023 |
def _hub_ops(f, max_dim=1920):
|
1024 |
# HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing
|
|
|
3 |
Dataloaders and dataset utils
|
4 |
"""
|
5 |
|
6 |
+
import contextlib
|
7 |
import glob
|
8 |
import hashlib
|
9 |
import json
|
|
|
56 |
def exif_size(img):
|
57 |
# Returns exif-corrected PIL size
|
58 |
s = img.size # (width, height)
|
59 |
+
with contextlib.suppress(Exception):
|
60 |
rotation = dict(img._getexif().items())[orientation]
|
61 |
if rotation in [6, 8]: # rotation 270 or 90
|
62 |
s = (s[1], s[0])
|
|
|
|
|
|
|
63 |
return s
|
64 |
|
65 |
|
|
|
857 |
|
858 |
|
859 |
# Ancillary functions --------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
860 |
def flatten_recursive(path=DATASETS_DIR / 'coco128'):
|
861 |
# Flatten a recursive directory by bringing all files to top level
|
862 |
+
new_path = Path(f'{str(path)}_flat')
|
863 |
+
if os.path.exists(new_path):
|
864 |
+
shutil.rmtree(new_path) # delete output folder
|
865 |
+
os.makedirs(new_path) # make new output folder
|
866 |
+
for file in tqdm(glob.glob(f'{str(Path(path))}/**/*.*', recursive=True)):
|
867 |
shutil.copyfile(file, new_path / Path(file).name)
|
868 |
|
869 |
|
|
|
922 |
for i, img in tqdm(zip(indices, files), total=n):
|
923 |
if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
|
924 |
with open(path.parent / txt[i], 'a') as f:
|
925 |
+
f.write(f'./{img.relative_to(path.parent).as_posix()}' + '\n') # add image to txt file
|
926 |
|
927 |
|
928 |
def verify_image_label(args):
|
|
|
1004 |
|
1005 |
def _unzip(path):
|
1006 |
# Unzip data.zip
|
1007 |
+
if not str(path).endswith('.zip'): # path is data.yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
1008 |
return False, None, path
|
1009 |
+
assert Path(path).is_file(), f'Error unzipping {path}, file not found'
|
1010 |
+
ZipFile(path).extractall(path=path.parent) # unzip
|
1011 |
+
dir = path.with_suffix('') # dataset directory == zip name
|
1012 |
+
assert dir.is_dir(), f'Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/'
|
1013 |
+
return True, str(dir), _find_yaml(dir) # zipped, data_dir, yaml_path
|
1014 |
|
1015 |
def _hub_ops(f, max_dim=1920):
|
1016 |
# HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing
|