@ -138,9 +138,8 @@ class CocoImporterTest(TestCase):
self . assertFalse ( ann_2_mask is None )
self . assertFalse ( ann_2_mask is None )
class CocoConverterTest ( TestCase ) :
class CocoConverterTest ( TestCase ) :
def _test_save_and_load ( self , source_dataset , converter_type , test_dir ,
def _test_save_and_load ( self , source_dataset , converter , test_dir ,
importer_params = None ) :
importer_params = None , target_dataset = None ) :
converter = converter_type ( )
converter ( source_dataset , test_dir . path )
converter ( source_dataset , test_dir . path )
if not importer_params :
if not importer_params :
@ -149,6 +148,8 @@ class CocoConverterTest(TestCase):
* * importer_params )
* * importer_params )
parsed_dataset = project . make_dataset ( )
parsed_dataset = project . make_dataset ( )
if target_dataset is not None :
source_dataset = target_dataset
source_subsets = [ s if s else DEFAULT_SUBSET_NAME
source_subsets = [ s if s else DEFAULT_SUBSET_NAME
for s in source_dataset . subsets ( ) ]
for s in source_dataset . subsets ( ) ]
self . assertListEqual (
self . assertListEqual (
@ -195,7 +196,7 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoCaptionsConverter , test_dir )
CocoCaptionsConverter () , test_dir )
def test_can_save_and_load_instances ( self ) :
def test_can_save_and_load_instances ( self ) :
class TestExtractor ( Extractor ) :
class TestExtractor ( Extractor ) :
@ -249,7 +250,7 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoInstancesConverter , test_dir )
CocoInstancesConverter () , test_dir )
def test_can_save_and_load_instances_with_mask_conversion ( self ) :
def test_can_save_and_load_instances_with_mask_conversion ( self ) :
class TestExtractor ( Extractor ) :
class TestExtractor ( Extractor ) :
@ -291,9 +292,64 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoInstancesConverter , test_dir ,
CocoInstancesConverter () , test_dir ,
{ ' merge_instance_polygons ' : True } )
{ ' merge_instance_polygons ' : True } )
def test_can_merge_instance_polygons_to_mask_in_coverter ( self ) :
label_categories = LabelCategories ( )
for i in range ( 10 ) :
label_categories . add ( str ( i ) )
class SrcTestExtractor ( Extractor ) :
def __iter__ ( self ) :
items = [
DatasetItem ( id = 0 , image = np . zeros ( ( 5 , 10 , 3 ) ) ,
annotations = [
PolygonObject ( [ 0 , 0 , 4 , 0 , 4 , 4 ] ,
label = 3 , id = 4 , group = 4 ,
attributes = { ' is_crowd ' : False } ) ,
PolygonObject ( [ 5 , 0 , 9 , 0 , 5 , 5 ] ,
label = 3 , id = 4 , group = 4 ,
attributes = { ' is_crowd ' : False } ) ,
]
) ,
]
return iter ( items )
def categories ( self ) :
return { AnnotationType . label : label_categories }
class DstTestExtractor ( Extractor ) :
def __iter__ ( self ) :
items = [
DatasetItem ( id = 0 , image = np . zeros ( ( 5 , 10 , 3 ) ) ,
annotations = [
BboxObject ( 1 , 0 , 8 , 4 , label = 3 , id = 4 , group = 4 ,
attributes = { ' is_crowd ' : True } ) ,
MaskObject ( np . array ( [
[ 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 ] ,
[ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 ] ,
[ 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 0 ] ,
[ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 ] ,
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ] ,
# only internal fragment (without the border),
# but not everywhere...
dtype = np . bool ) ,
attributes = { ' is_crowd ' : True } ,
label = 3 , id = 4 , group = 4 ) ,
]
) ,
]
return iter ( items )
def categories ( self ) :
return { AnnotationType . label : label_categories }
with TestDir ( ) as test_dir :
self . _test_save_and_load ( SrcTestExtractor ( ) ,
CocoInstancesConverter ( merge_polygons = True ) , test_dir ,
target_dataset = DstTestExtractor ( ) )
def test_can_save_and_load_images ( self ) :
def test_can_save_and_load_images ( self ) :
class TestExtractor ( Extractor ) :
class TestExtractor ( Extractor ) :
def __iter__ ( self ) :
def __iter__ ( self ) :
@ -314,7 +370,7 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoImageInfoConverter , test_dir )
CocoImageInfoConverter () , test_dir )
def test_can_save_and_load_labels ( self ) :
def test_can_save_and_load_labels ( self ) :
class TestExtractor ( Extractor ) :
class TestExtractor ( Extractor ) :
@ -350,7 +406,7 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoLabelsConverter , test_dir )
CocoLabelsConverter () , test_dir )
def test_can_save_and_load_keypoints ( self ) :
def test_can_save_and_load_keypoints ( self ) :
class TestExtractor ( Extractor ) :
class TestExtractor ( Extractor ) :
@ -397,7 +453,7 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoPersonKeypointsConverter , test_dir )
CocoPersonKeypointsConverter () , test_dir )
def test_can_save_dataset_with_no_subsets ( self ) :
def test_can_save_dataset_with_no_subsets ( self ) :
class TestExtractor ( Extractor ) :
class TestExtractor ( Extractor ) :
@ -429,4 +485,4 @@ class CocoConverterTest(TestCase):
with TestDir ( ) as test_dir :
with TestDir ( ) as test_dir :
self . _test_save_and_load ( TestExtractor ( ) ,
self . _test_save_and_load ( TestExtractor ( ) ,
CocoConverter , test_dir )
CocoConverter () , test_dir )