@@ -9,7 +9,7 @@ def __init__(self, vertex, project_type):
99 self .project_type = project_type
1010
1111 @classmethod
12- def fromBbox (cls , width , height , points ):
12+ def from_bbox (cls , width , height , points ):
1313 vertices = [
1414 (points [0 ] / width , points [1 ] / height ), # 左上
1515 (points [2 ] / width , points [1 ] / height ), # 右上
@@ -18,21 +18,37 @@ def fromBbox(cls, width, height, points):
1818 ]
1919 return VisualInspectionAiFigure (vertices , "bbox" )
2020
21+ @classmethod
22+ def from_segmentation (cls , width , height , points ):
23+ # サンプルのjsonには矩形しかなかったので、とりあえずで変換
24+ figure_points = points [0 ][
25+ 0
26+ ] # FIXME fastlabelでは飛地がある場合に配列に複数の要素がある場合があるが、VisualInspectionAiで飛地をどのように扱うか不明なため、一旦最初の図形だけパース。
27+ vertices = [
28+ {"x" : figure_points [i ] / width , "y" : figure_points [i + 1 ] / height }
29+ for i in range (0 , len (figure_points ), 2 )
30+ ]
31+ return VisualInspectionAiFigure (vertices , "segmentation" )
32+
2133 # VisualInspectionAiのjsonlを作成する。
2234 def make_jsonl (self ) -> dict :
2335 return {
24- "image_gcs_uri" : "gs://ffod-98sm/20240613/NG/test/BQ1334B__11-7_18-19_0004.png" ,
36+ "image_gcs_uri" : "gs://ffod-98sm/20240613/NG/test/BQ1334B__11-7_18-19_0004.png" , # TODO ここに入れ込めそうな値はjsonにはないので、調査
2537 "vi_annotations" : self .__make_vi_annotations_jsonl (),
26- "dataItemResourceLabels" : {"goog_vi_ml_use" : "test" },
38+ "dataItemResourceLabels" : {
39+ "goog_vi_ml_use" : "test"
40+ }, # TODO この値が何を指しているのか調査
2741 }
2842
2943 def __make_vi_annotations_jsonl (self ) -> dict :
3044 return {
3145 "viBoundingPoly" : {"vertex" : self .vertex },
32- "annotationSpec" : "defect" ,
33- "annotationSet" : "Polygons Regions"
34- if self .project_type == "bbox"
35- else "TODO segmentationの場合の表記調査" ,
46+ "annotationSpec" : "defect" , # TODO ここにクラスが入る場合は入れる。
47+ "annotationSet" : (
48+ "Polygons Regions"
49+ if self .project_type == "bbox"
50+ else "TODO segmentationの場合の表記調査"
51+ ),
3652 }
3753
3854
@@ -44,23 +60,37 @@ def __init__(self, figures):
4460 def from_bbox (cls , bbox_json ):
4561 figures = []
4662 for bbox in bbox_json :
47- figures .extend (VisualInspectionAi .parseBboxJson (bbox ))
63+ figures .extend (VisualInspectionAi .parse_bbox_json (bbox ))
4864 return cls (figures )
4965
5066 @classmethod
51- def parseBboxJson (cls , bbox_json ) -> list [VisualInspectionAiFigure ]:
67+ def parse_bbox_json (cls , bbox_json ) -> list [VisualInspectionAiFigure ]:
5268 result = []
5369 width = bbox_json ["width" ]
5470 height = bbox_json ["height" ]
5571 for annotation in bbox_json ["annotations" ]:
5672 points = annotation ["points" ]
57- result .append (VisualInspectionAiFigure .fromBbox (width , height , points ))
73+ result .append (VisualInspectionAiFigure .from_bbox (width , height , points ))
5874 return result
5975
6076 @classmethod
61- def from_segmentation (cls , segmentation_json , project_type ):
62- # TODO segmentationからvertexへの変換処理
63- return cls ([])
77+ def from_segmentation (cls , segmentation_json ):
78+ figures = []
79+ for bbox in segmentation_json :
80+ figures .extend (VisualInspectionAi .parse_segmentation_json (bbox ))
81+ return cls (figures )
82+
83+ @classmethod
84+ def parse_segmentation_json (cls , json ) -> list [VisualInspectionAiFigure ]:
85+ result = []
86+ width = json ["width" ]
87+ height = json ["height" ]
88+ for annotation in json ["annotations" ]:
89+ points = annotation ["points" ]
90+ result .append (
91+ VisualInspectionAiFigure .from_segmentation (width , height , points )
92+ )
93+ return result
6494
6595 def __make_jsonl (self ) -> list :
6696 print (self .figures )
0 commit comments