2021-07-17 19:27:51 +08:00
|
|
|
'''
|
|
|
|
@ Date: 2021-04-15 16:57:53
|
|
|
|
@ Author: Qing Shuai
|
|
|
|
@ LastEditors: Qing Shuai
|
|
|
|
@ LastEditTime: 2021-07-14 22:15:26
|
|
|
|
@ FilePath: /EasyMocap/easymocap/annotator/basic_dataset.py
|
|
|
|
'''
|
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:
|
2021-07-17 19:27:51 +08:00
|
|
|
def __init__(self, path, sub=None, image='images', annot='annots', no_annot=False, ext='.jpg', remove_tmp=True) -> 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)
|
2021-07-17 19:27:51 +08:00
|
|
|
if not os.path.exists(self.annot_root):
|
|
|
|
no_annot = True
|
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)
|
2021-04-14 15:22:51 +08:00
|
|
|
if sub is None:
|
2021-07-17 19:27:51 +08:00
|
|
|
self.imgnames = getFileList(self.image_root, ext=ext)
|
|
|
|
if not no_annot:
|
|
|
|
self.annnames = getFileList(self.annot_root, ext='.json')
|
2021-04-14 15:22:51 +08:00
|
|
|
else:
|
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]
|
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]
|
|
|
|
length = min(len(self.imgnames), len(self.annnames))
|
|
|
|
self.imgnames = self.imgnames[:length]
|
|
|
|
self.annnames = self.annnames[:length]
|
|
|
|
# assert len(self.imgnames) == len(self.annnames)
|
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):
|
|
|
|
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]])
|