11import copy
22import json
33import logging
4+ from dataclasses import dataclass
45from pathlib import Path
56from typing import List
67from typing import Optional
78from typing import Union
89
910import lib .core as constances
1011import pandas as pd
11- from dataclasses import dataclass
1212from lib .app .exceptions import AppException
1313from lib .core import ATTACHED_VIDEO_ANNOTATION_POSTFIX
1414from lib .core import PIXEL_ANNOTATION_POSTFIX
@@ -58,10 +58,10 @@ class VideoRawData:
5858
5959class DataAggregator :
6060 def __init__ (
61- self ,
62- project_type : str ,
63- project_root : Union [str , Path ],
64- folder_names : Optional [List [Union [Path , str ]]] = None
61+ self ,
62+ project_type : str ,
63+ project_root : Union [str , Path ],
64+ folder_names : Optional [List [Union [Path , str ]]] = None ,
6565 ):
6666 self .project_type = project_type
6767 self .project_root = Path (project_root )
@@ -87,10 +87,18 @@ def get_annotation_paths(self):
8787 if path .is_file () and path .suffix == self .annotation_suffix :
8888 annotations_paths .append (path )
8989 elif path .is_dir () and path .name != "classes" :
90- annotations_paths .extend (list (path .rglob (f"*{ self .annotation_suffix } " )))
90+ annotations_paths .extend (
91+ list (path .rglob (f"*{ self .annotation_suffix } " ))
92+ )
9193 else :
9294 for folder_name in self .folder_names :
93- annotations_paths .extend (list ((self .project_root / folder_name ).rglob (f"*{ self .annotation_suffix :} " )))
95+ annotations_paths .extend (
96+ list (
97+ (self .project_root / folder_name ).rglob (
98+ f"*{ self .annotation_suffix :} "
99+ )
100+ )
101+ )
94102
95103 if not annotations_paths :
96104 logger .warning (f"Could not find annotations in { self .project_root } ." )
@@ -103,11 +111,16 @@ def check_classes_path(self):
103111 )
104112
105113 def aggregate_annotations_as_df (self ):
106- logger .info (f"Aggregating annotations from { self .project_root } as pandas DataFrame" )
114+ logger .info (
115+ f"Aggregating annotations from { self .project_root } as pandas DataFrame"
116+ )
107117 self .check_classes_path ()
108118 annotation_paths = self .get_annotation_paths ()
109119
110- if self .project_type in (constances .ProjectType .VECTOR .name , constances .ProjectType .PIXEL .name ):
120+ if self .project_type in (
121+ constances .ProjectType .VECTOR .name ,
122+ constances .ProjectType .PIXEL .name ,
123+ ):
111124 return self .aggregate_image_annotations_as_df (annotation_paths )
112125 elif self .project_type == constances .ProjectType .VIDEO .name :
113126 return self .aggregate_video_annotations_as_df (annotation_paths )
@@ -120,7 +133,11 @@ def aggregate_video_annotations_as_df(self, annotation_paths: List[str]):
120133 raw_data = VideoRawData ()
121134 # metadata
122135 raw_data .videoName = annotation_data ["metadata" ]["name" ]
123- raw_data .folderName = annotation_path .parent .name if annotation_path .parent != self .project_root else None
136+ raw_data .folderName = (
137+ annotation_path .parent .name
138+ if annotation_path .parent != self .project_root
139+ else None
140+ )
124141 raw_data .videoHeight = annotation_data ["metadata" ].get ("height" )
125142 raw_data .videoWidth = annotation_data ["metadata" ].get ("width" )
126143 raw_data .videoStatus = annotation_data ["metadata" ].get ("status" )
@@ -146,11 +163,19 @@ def aggregate_video_annotations_as_df(self, annotation_paths: List[str]):
146163 instance_raw .type = instance ["meta" ].get ("type" )
147164 instance_raw .className = instance ["meta" ].get ("className" )
148165 instance_raw .createdAt = instance ["meta" ].get ("createdAt" )
149- instance_raw .createdBy = instance ["meta" ].get ("createdBy" , {}).get ("email" )
150- instance_raw .creatorRole = instance ["meta" ].get ("createdBy" , {}).get ("role" )
166+ instance_raw .createdBy = (
167+ instance ["meta" ].get ("createdBy" , {}).get ("email" )
168+ )
169+ instance_raw .creatorRole = (
170+ instance ["meta" ].get ("createdBy" , {}).get ("role" )
171+ )
151172 instance_raw .updatedAt = instance ["meta" ].get ("updatedAt" )
152- instance_raw .updatedBy = instance ["meta" ].get ("updatedBy" , {}).get ("email" )
153- instance_raw .updatorRole = instance ["meta" ].get ("updatedBy" , {}).get ("role" )
173+ instance_raw .updatedBy = (
174+ instance ["meta" ].get ("updatedBy" , {}).get ("email" )
175+ )
176+ instance_raw .updatorRole = (
177+ instance ["meta" ].get ("updatedBy" , {}).get ("role" )
178+ )
154179 instance_raw .pointLabels = instance ["meta" ].get ("pointLabels" )
155180 parameters = instance .get ("parameters" , [])
156181 for parameter_id , parameter in enumerate (parameters ):
@@ -167,7 +192,9 @@ def aggregate_video_annotations_as_df(self, annotation_paths: List[str]):
167192 for attribute_id , attribute in enumerate (attributes ):
168193 attribute_raw = copy .copy (timestamp_raw )
169194 attribute_raw .attributeId = attribute_id
170- attribute_raw .attributeGroupName = attribute .get ("groupName" )
195+ attribute_raw .attributeGroupName = attribute .get (
196+ "groupName"
197+ )
171198 attribute_raw .attributeName = attribute .get ("name" )
172199 raws .append (attribute_raw )
173200 if not attributes :
@@ -212,7 +239,7 @@ def aggregate_image_annotations_as_df(self, annotations_paths: List[str]):
212239 "imageAnnotator" : [],
213240 "imageQA" : [],
214241 "commentResolved" : [],
215- "tag" : []
242+ "tag" : [],
216243 }
217244
218245 classes_json = json .load (open (self .classes_path ))
@@ -233,7 +260,9 @@ def aggregate_image_annotations_as_df(self, annotations_paths: List[str]):
233260 def __append_annotation (annotation_dict ):
234261 for annotation_key in annotation_data :
235262 if annotation_key in annotation_dict :
236- annotation_data [annotation_key ].append (annotation_dict [annotation_key ])
263+ annotation_data [annotation_key ].append (
264+ annotation_dict [annotation_key ]
265+ )
237266 else :
238267 annotation_data [annotation_key ].append (None )
239268
@@ -271,8 +300,8 @@ def __append_annotation(annotation_dict):
271300 annotation_type = annotation .get ("type" , "mask" )
272301 annotation_class_name = annotation .get ("className" )
273302 if (
274- annotation_class_name is None
275- or annotation_class_name not in class_name_to_color
303+ annotation_class_name is None
304+ or annotation_class_name not in class_name_to_color
276305 ):
277306 logger .warning (
278307 "Annotation class %s not found in classes json. Skipping." ,
@@ -308,7 +337,7 @@ def __append_annotation(annotation_dict):
308337 annotation_probability = annotation .get ("probability" )
309338 annotation_point_labels = annotation .get ("pointLabels" )
310339 attributes = annotation .get ("attributes" )
311- user_metadata = self .__get_user_metadata (annotation )
340+ user_metadata = self .__get_user_metadata (annotation )
312341 folder_name = None
313342 if annotation_path .parent != Path (self .project_root ):
314343 folder_name = annotation_path .parent .name
@@ -339,19 +368,19 @@ def __append_annotation(annotation_dict):
339368 attribute_group = attribute .get ("groupName" )
340369 attribute_name = attribute .get ("name" )
341370 if (
342- attribute_group
343- not in class_group_name_to_values [annotation_class_name ]
371+ attribute_group
372+ not in class_group_name_to_values [annotation_class_name ]
344373 ):
345374 logger .warning (
346375 "Annotation class group %s not in classes json. Skipping." ,
347376 attribute_group ,
348377 )
349378 continue
350379 if (
351- attribute_name
352- not in class_group_name_to_values [annotation_class_name ][
353- attribute_group
354- ]
380+ attribute_name
381+ not in class_group_name_to_values [annotation_class_name ][
382+ attribute_group
383+ ]
355384 ):
356385 logger .warning (
357386 "Annotation class group value %s not in classes json. Skipping." ,
0 commit comments