99from kili .domain .annotation import (
1010 ClassicAnnotation ,
1111 ClassificationAnnotation ,
12+ ObjectDetectionAnnotation ,
1213 RankingAnnotation ,
1314 TranscriptionAnnotation ,
1415 Vertice ,
@@ -55,7 +56,7 @@ def patch_label_json_response(
5556
5657 Modifies the input label.
5758 """
58- if self ._project_input_type in {"VIDEO" , "LLM_RLHF" }:
59+ if self ._project_input_type in {"VIDEO" , "LLM_RLHF" , "GEOSPATIAL" }:
5960 if not annotations and self ._label_has_json_response_data (label ):
6061 return
6162
@@ -65,8 +66,11 @@ def patch_label_json_response(
6566 annotations = annotations , json_interface = self ._project_json_interface
6667 )
6768 else :
69+ print ("converting annotations" )
6870 annotations = cast (List [ClassicAnnotation ], annotations )
69- converted_json_resp = _classic_annotations_to_json_response (annotations = annotations )
71+ converted_json_resp = _classic_annotations_to_json_response (
72+ annotations = annotations , json_interface = self ._project_json_interface
73+ )
7074
7175 label ["jsonResponse" ] = converted_json_resp
7276
@@ -152,7 +156,7 @@ def _video_annotations_to_json_response(
152156
153157
154158def _classic_annotations_to_json_response (
155- annotations : List [ClassicAnnotation ],
159+ annotations : List [ClassicAnnotation ], json_interface : Dict
156160) -> Dict [str , Dict [JobName , Dict ]]:
157161 """Convert label annotations to a json response."""
158162 json_resp = defaultdict (dict )
@@ -185,6 +189,18 @@ def _classic_annotations_to_json_response(
185189 for job_name , job_resp in ann_json_resp .items ():
186190 json_resp .setdefault (job_name , {}).setdefault ("text" , job_resp ["text" ])
187191
192+ elif ann ["__typename" ] == "ObjectDetectionAnnotation" :
193+ ann = cast (ObjectDetectionAnnotation , ann )
194+ ann_json_resp = _object_detection_annotation_to_json_response (
195+ ann ,
196+ other_annotations ,
197+ json_interface ,
198+ )
199+ for job_name , job_resp in ann_json_resp .items ():
200+ json_resp .setdefault (job_name , {}).setdefault ("annotations" , []).extend (
201+ job_resp ["annotations" ]
202+ )
203+
188204 else :
189205 raise NotImplementedError (f"Cannot convert classic annotation to json response: { ann } " )
190206
@@ -352,6 +368,46 @@ def _transcription_annotation_to_json_response(
352368 return json_resp
353369
354370
371+ def _object_detection_annotation_to_json_response (
372+ annotation : ObjectDetectionAnnotation ,
373+ other_annotations : List [ClassicAnnotation ],
374+ json_interface : Dict ,
375+ ) -> Dict [JobName , Dict ]:
376+ """Convert object detection annotation to a json response."""
377+ child_annotations = _get_child_annotations (annotation , other_annotations )
378+ json_resp_child_jobs = (
379+ _compute_children_json_resp (child_annotations , other_annotations )
380+ if child_annotations
381+ else {}
382+ )
383+
384+ annotation_dict = {
385+ "children" : json_resp_child_jobs ,
386+ "categories" : [{"name" : annotation ["category" ]}],
387+ "mid" : annotation ["mid" ],
388+ "type" : json_interface ["jobs" ][annotation ["job" ]]["tools" ][0 ],
389+ }
390+
391+ norm_vertices = annotation ["annotationValue" ]["vertices" ]
392+
393+ if json_interface ["jobs" ][annotation ["job" ]]["tools" ][0 ] == "marker" :
394+ annotation_dict ["point" ] = norm_vertices [0 ][0 ][0 ]
395+
396+ elif json_interface ["jobs" ][annotation ["job" ]]["tools" ][0 ] in {"polygon" , "rectangle" }:
397+ annotation_dict ["boundingPoly" ] = [{"normalizedVertices" : norm_vertices [0 ][0 ]}]
398+
399+ elif json_interface ["jobs" ][annotation ["job" ]]["tools" ][0 ] == "semantic" :
400+ annotation_dict ["boundingPoly" ] = [
401+ {"normalizedVertices" : norm_vert } for norm_vert in norm_vertices [0 ]
402+ ]
403+
404+ return {
405+ annotation ["job" ]: {
406+ "annotations" : [annotation_dict ],
407+ },
408+ }
409+
410+
355411def _video_transcription_annotation_to_json_response (
356412 annotation : VideoTranscriptionAnnotation ,
357413) -> Dict [str , Dict [JobName , Dict ]]:
0 commit comments