1212
1313import boto3
1414import lib .core as constances
15+ from lib import controller
1516from lib .app .annotation_helpers import add_annotation_bbox_to_json
1617from lib .app .annotation_helpers import add_annotation_comment_to_json
1718from lib .app .annotation_helpers import add_annotation_point_to_json
2122from lib .app .interface .types import AnnotationStatuses
2223from lib .app .interface .types import AnnotationType
2324from lib .app .interface .types import AnnotatorRole
25+ from lib .app .interface .types import ClassType
2426from lib .app .interface .types import EmailStr
2527from lib .app .interface .types import ImageQualityChoices
2628from lib .app .interface .types import NotEmptyStr
3335from lib .app .serializers import SettingsSerializer
3436from lib .app .serializers import TeamSerializer
3537from lib .core import LIMITED_FUNCTIONS
38+ from lib .core .entities .project_entities import AnnotationClassEntity
3639from lib .core .enums import ImageQuality
3740from lib .core .exceptions import AppException
3841from lib .core .types import AttributeGroup
39- from lib .core .types import ClassesJson
4042from lib .core .types import MLModel
4143from lib .core .types import Project
42- from lib .infrastructure .controller import Controller
4344from pydantic import conlist
4445from pydantic import parse_obj_as
4546from pydantic import StrictBool
4647from superannotate .logger import get_default_logger
4748from tqdm import tqdm
4849
49- controller = Controller . get_instance ()
50+
5051logger = get_default_logger ()
5152
5253
5354@validate_arguments
54- def init (path_to_config_json : Optional [str ] = None ):
55+ def init (path_to_config_json : Optional [str ] = None , token : str = None ):
5556 """
5657 Initializes and authenticates to SuperAnnotate platform using the config file.
5758 If not initialized then $HOME/.superannotate/config.json
5859 will be used.
5960
6061 :param path_to_config_json: Location to config JSON file
6162 :type path_to_config_json: str or Path
63+
64+ :param token: Team token
65+ :type token: str
6266 """
6367 global controller
64- controller .init (path_to_config_json )
68+ controller .init (config_path = path_to_config_json , token = token )
6569
6670
6771@validate_arguments
@@ -1525,6 +1529,7 @@ def create_annotation_class(
15251529 name : NotEmptyStr ,
15261530 color : NotEmptyStr ,
15271531 attribute_groups : Optional [List [AttributeGroup ]] = None ,
1532+ type : ClassType = "object" ,
15281533):
15291534 """Create annotation class in project
15301535
@@ -1538,19 +1543,25 @@ def create_annotation_class(
15381543 [ { "name": "tall", "is_multiselect": 0, "attributes": [ { "name": "yes" }, { "name": "no" } ] },
15391544 { "name": "age", "is_multiselect": 0, "attributes": [ { "name": "young" }, { "name": "old" } ] } ]
15401545 :type attribute_groups: list of dicts
1546+ :param type: class type
1547+ :type type: str
15411548
15421549 :return: new class metadata
15431550 :rtype: dict
15441551 """
15451552 if isinstance (project , Project ):
15461553 project = project .dict ()
15471554 attribute_groups = (
1548- list (map (lambda x : x .dict (), attribute_groups )) if attribute_groups else None
1555+ list (map (lambda x : x .dict (), attribute_groups )) if attribute_groups else []
15491556 )
15501557 response = controller .create_annotation_class (
1551- project_name = project , name = name , color = color , attribute_groups = attribute_groups
1558+ project_name = project ,
1559+ name = name ,
1560+ color = color ,
1561+ attribute_groups = attribute_groups ,
1562+ class_type = type ,
15521563 )
1553- return response .data .to_dict ()
1564+ return response .data .dict ()
15541565
15551566
15561567@Trackable
@@ -1595,7 +1606,7 @@ def download_annotation_classes_json(project: NotEmptyStr, folder: Union[str, Pa
15951606@validate_arguments
15961607def create_annotation_classes_from_classes_json (
15971608 project : Union [NotEmptyStr , dict ],
1598- classes_json : Union [List [ClassesJson ], str , Path ],
1609+ classes_json : Union [List [AnnotationClassEntity ], str , Path ],
15991610 from_s3_bucket = False ,
16001611):
16011612 """Creates annotation classes in project from a SuperAnnotate format
@@ -1611,33 +1622,32 @@ def create_annotation_classes_from_classes_json(
16111622 :return: list of created annotation class metadatas
16121623 :rtype: list of dicts
16131624 """
1614- if not isinstance (classes_json , list ):
1615- logger .info (
1616- "Creating annotation classes in project %s from %s." , project , classes_json ,
1617- )
1625+ classes_json_initial = classes_json
1626+ if isinstance (classes_json , str ) or isinstance (classes_json , Path ):
16181627 if from_s3_bucket :
16191628 from_session = boto3 .Session ()
16201629 from_s3 = from_session .resource ("s3" )
16211630 file = io .BytesIO ()
16221631 from_s3_object = from_s3 .Object (from_s3_bucket , classes_json )
16231632 from_s3_object .download_fileobj (file )
16241633 file .seek (0 )
1625- annotation_classes = parse_obj_as ( List [ ClassesJson ], json . load ( file ))
1634+ data = file
16261635 else :
1627- annotation_classes = parse_obj_as (
1628- List [ClassesJson ], json .load (open (classes_json ))
1629- )
1630-
1631- else :
1632- annotation_classes = classes_json
1633-
1634- annotation_classes = list (
1635- map (lambda annotation_class : annotation_class .dict (), annotation_classes )
1636+ data = open (classes_json )
1637+ classes_json = json .load (data )
1638+ annotation_classes = parse_obj_as (List [AnnotationClassEntity ], classes_json )
1639+ logger .info (
1640+ "Creating annotation classes in project %s from %s." ,
1641+ project ,
1642+ classes_json_initial ,
16361643 )
16371644 response = controller .create_annotation_classes (
16381645 project_name = project , annotation_classes = annotation_classes ,
16391646 )
1640- return response .data
1647+ if response .errors :
1648+ raise AppException (response .errors )
1649+
1650+ return [i .dict () for i in response .data ]
16411651
16421652
16431653@Trackable
0 commit comments