2021-07-17 19:27:51 +08:00
|
|
|
'''
|
|
|
|
@ Date: 2021-04-15 16:57:53
|
|
|
|
@ Author: Qing Shuai
|
|
|
|
@ LastEditors: Qing Shuai
|
2022-10-25 20:57:27 +08:00
|
|
|
@ LastEditTime: 2022-09-15 21:58:57
|
|
|
|
@ FilePath: /EasyMocapPublic/easymocap/annotator/basic_dataset.py
|
2021-07-17 19:27:51 +08:00
|
|
|
'''
|
2021-04-14 15:22:51 +08:00
|
|
|
from os.path import join
|
2021-07-17 19:27:51 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
2021-04-14 15:22:51 +08:00
|
|
|
from .file_utils import getFileList
|
|
|
|
|
|
|
|
class ImageFolder:
|
2022-08-21 16:13:47 +08:00
|
|
|
def __init__(self, path, sub=None, image='images', annot='annots',
|
|
|
|
no_annot=False, share_annot=False, ext='.jpg', remove_tmp=True,
|
|
|
|
max_per_folder=-1) -> None:
|
2021-04-14 15:22:51 +08:00
|
|
|
self.root = path
|
2021-07-17 19:27:51 +08:00
|
|
|
self.image = image
|
2021-04-14 15:22:51 +08:00
|
|
|
self.annot = annot
|
|
|
|
self.image_root = join(path, self.image)
|
|
|
|
self.annot_root = join(path, self.annot)
|
2022-08-21 16:13:47 +08:00
|
|
|
if not os.path.exists(self.annot_root) or no_annot:
|
2021-07-17 19:27:51 +08:00
|
|
|
no_annot = True
|
2022-08-21 16:13:47 +08:00
|
|
|
self.share_annot = share_annot
|
2021-04-14 15:22:51 +08:00
|
|
|
self.annot_root_tmp = join(path, self.annot + '_tmp')
|
2021-07-17 19:27:51 +08:00
|
|
|
if os.path.exists(self.annot_root_tmp) and remove_tmp:
|
|
|
|
shutil.rmtree(self.annot_root_tmp)
|
2022-10-25 20:57:27 +08:00
|
|
|
print('- Load data from {}'.format(path))
|
2021-04-14 15:22:51 +08:00
|
|
|
if sub is None:
|
2022-10-25 20:57:27 +08:00
|
|
|
print('- Try to find image names...')
|
2022-08-21 16:13:47 +08:00
|
|
|
self.imgnames = getFileList(self.image_root, ext=ext, max=max_per_folder)
|
2022-10-25 20:57:27 +08:00
|
|
|
print(' -> find {} images'.format(len(self.imgnames)))
|
2021-07-17 19:27:51 +08:00
|
|
|
if not no_annot:
|
2022-10-25 20:57:27 +08:00
|
|
|
print('- Try to find annot names...')
|
2021-07-17 19:27:51 +08:00
|
|
|
self.annnames = getFileList(self.annot_root, ext='.json')
|
2022-10-25 20:57:27 +08:00
|
|
|
print(' -> find {} annots'.format(len(self.annnames)))
|
2021-04-14 15:22:51 +08:00
|
|
|
else:
|
2022-10-25 20:57:27 +08:00
|
|
|
print('- Try to find image names of camera {}...'.format(sub))
|
2021-07-17 19:27:51 +08:00
|
|
|
self.imgnames = getFileList(join(self.image_root, sub), ext=ext)
|
2021-04-14 15:22:51 +08:00
|
|
|
self.imgnames = [join(sub, name) for name in self.imgnames]
|
2022-10-25 20:57:27 +08:00
|
|
|
print(' -> find {} images'.format(len(self.imgnames)))
|
2021-07-17 19:27:51 +08:00
|
|
|
if not no_annot:
|
|
|
|
self.annnames = getFileList(join(self.annot_root, sub), ext='.json')
|
|
|
|
self.annnames = [join(sub, name) for name in self.annnames]
|
2022-08-21 16:13:47 +08:00
|
|
|
if len(self.annnames) != len(self.imgnames) and share_annot:
|
|
|
|
if len(self.annnames) == 1:
|
|
|
|
self.annnames = [self.annnames[0] for _ in range(len(self.imgnames))]
|
|
|
|
else:
|
|
|
|
length = min(len(self.imgnames), len(self.annnames))
|
|
|
|
self.imgnames = self.imgnames[:length]
|
|
|
|
self.annnames = self.annnames[:length]
|
2021-04-14 15:22:51 +08:00
|
|
|
self.isTmp = True
|
2021-07-17 19:27:51 +08:00
|
|
|
self.no_annot = no_annot
|
2021-04-14 15:22:51 +08:00
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2022-10-25 20:57:27 +08:00
|
|
|
if index > len(self.imgnames):
|
|
|
|
print('!!! You are try to read {} image from {} images'.format(index, len(self.imgnames)))
|
|
|
|
print('!!! Please check image path: {}'.format(self.image_root))
|
2021-04-14 15:22:51 +08:00
|
|
|
imgname = join(self.image_root, self.imgnames[index])
|
2021-07-17 19:27:51 +08:00
|
|
|
if self.no_annot:
|
|
|
|
annname = None
|
2021-04-14 15:22:51 +08:00
|
|
|
else:
|
2021-07-17 19:27:51 +08:00
|
|
|
if self.isTmp:
|
|
|
|
annname = join(self.annot_root_tmp, self.annnames[index])
|
|
|
|
else:
|
|
|
|
annname = join(self.annot_root, self.annnames[index])
|
2021-04-14 15:22:51 +08:00
|
|
|
return imgname, annname
|
|
|
|
|
|
|
|
def __len__(self):
|
2021-07-17 19:27:51 +08:00
|
|
|
return len(self.imgnames)
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return '{}: {} images'.format(self.root, len(self))
|
|
|
|
|
|
|
|
class MVBase:
|
|
|
|
def __init__(self, path, subs, annot='annots') -> None:
|
|
|
|
self.root = path
|
|
|
|
self.subs = subs
|
|
|
|
self.image = 'images'
|
|
|
|
self.annot = annot
|
|
|
|
self.image_root = join(path, self.image)
|
|
|
|
self.annot_root = join(path, self.annot)
|
|
|
|
self.annot_root_tmp = join(path, self.annot + '_tmp')
|
|
|
|
assert len(subs) > 0, subs
|
|
|
|
self.imgnames, self.annnames = {}, {}
|
|
|
|
for sub in subs:
|
|
|
|
imgnames = getFileList(join(self.image_root, sub), ext='.jpg')
|
|
|
|
annnames = getFileList(join(self.annot_root, sub), ext='.json')
|
|
|
|
imgnames = [join(sub, name) for name in imgnames]
|
|
|
|
annnames = [join(sub, name) for name in annnames]
|
|
|
|
self.imgnames[sub] = imgnames
|
|
|
|
self.annnames[sub] = annnames
|
|
|
|
self.isTmp = True
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
imgnames, annnames = {}, {}
|
|
|
|
for sub in self.subs:
|
|
|
|
imgnames[sub] = join(self.image_root, self.imgnames[sub][index])
|
|
|
|
if self.isTmp:
|
|
|
|
annname = join(self.annot_root_tmp, self.annnames[sub][index])
|
|
|
|
else:
|
|
|
|
annname = join(self.annot_root, self.annnames[sub][index])
|
|
|
|
annnames[sub] = annname
|
|
|
|
return imgnames, annnames
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.imgnames[self.subs[0]])
|