Skip to content

Commit ea87251

Browse files
author
shuichi
committed
improve convert script
1 parent e84f2ff commit ea87251

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

examples/visual_inspection_ai.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
import json
2-
2+
import random
33

44
# visualInspectionAiの一要素として表せる図形のValue Object。
55
# jsonl型として出力するのが責務
6+
7+
68
class VisualInspectionAiFigure:
7-
def __init__(self, vertex, project_type):
9+
def __init__(
10+
self,
11+
vertex,
12+
project_type,
13+
image_gcs_uri,
14+
label,
15+
goog_vi_ml_use,
16+
annotationId,
17+
annotationSpec,
18+
annotationSpecId,
19+
):
820
self.vertex = vertex
921
self.project_type = project_type
22+
self.image_gcs_uri = image_gcs_uri
23+
self.label = label
24+
self.goog_vi_ml_use = goog_vi_ml_use
25+
self.annotationId = annotationId
26+
self.annotationSpec = annotationSpec
27+
self.annotationSpecId = annotationSpecId
1028

1129
@classmethod
12-
def from_bbox(cls, width, height, points):
30+
def from_bbox(cls, width, height, points, image_gcs_uri, goog_vi_ml_use):
1331
vertices = [
1432
(points[0] / width, points[1] / height), # 左上
1533
(points[2] / width, points[1] / height), # 右上
1634
(points[0] / width, points[3] / height), # 左下
1735
(points[2] / width, points[3] / height), # 右下
1836
]
19-
return VisualInspectionAiFigure(vertices, "bbox")
37+
return VisualInspectionAiFigure(
38+
vertex=vertices,
39+
project_type="bbox",
40+
image_gcs_uri=image_gcs_uri,
41+
goog_vi_ml_use=goog_vi_ml_use,
42+
)
2043

2144
@classmethod
2245
def from_segmentation(cls, width, height, points):
@@ -38,33 +61,30 @@ def make_jsonl(self) -> dict:
3861
else self.__make_segmentation_vi_annotations_jsonl()
3962
)
4063
return {
41-
"image_gcs_uri": "gs://ffod-98sm/20240613/NG/test/BQ1334B__11-7_18-19_0004.png", # TODO ここに入れ込めそうな値はjsonにはないので、調査
64+
"image_gcs_uri": self.image_gcs_uri,
4265
"vi_annotations": vi_annotations,
4366
"dataItemResourceLabels": {
44-
"goog_vi_ml_use": "test"
45-
}, # TODO この値が何を指しているのか調査
67+
"label": self.label,
68+
"goog_vi_ml_use": self.goog_vi_ml_use,
69+
},
4670
}
4771

4872
def __make_bbox_vi_annotations_jsonl(self) -> dict:
49-
# TODO とりあえず貰ったjsonlを再現するように値を埋めた。各プロパティの正確な仕様を確認
5073
return {
5174
"viBoundingPoly": {"vertex": self.vertex},
52-
"annotationSpec": "defect",
75+
"annotationSpec": self.annotationSpec,
5376
"annotationSet": "Polygons Regions",
5477
}
5578

5679
def __make_segmentation_vi_annotations_jsonl(self) -> dict:
57-
# TODO とりあえず貰ったjsonlを再現するように値を埋めた。各プロパティの正確な仕様を確認
5880
return {
5981
"viBoundingPoly": {"vertex": self.vertex},
60-
"annotationId": "1466198469755535360",
61-
"annotationSpecId": "5575973384027635712",
62-
"annotationSpec": "defect",
82+
"annotationId": self.annotationId,
83+
"annotationSpecId": self.annotationSpecId,
84+
"annotationSpec": self.annotationSpec,
6385
"annotationSet": "Polygons Regions",
64-
"annotationSetId": "8581633279210291200",
65-
"annotationResourceLabels": {
66-
"goog_vi_annotation_set_name": "8581633279210291200"
67-
},
86+
"annotationSetId": "",
87+
"annotationResourceLabels": {"goog_vi_annotation_set_name": ""},
6888
}
6989

7090

@@ -80,14 +100,28 @@ def from_bbox(cls, bbox_json):
80100
figures.extend(VisualInspectionAi.parse_bbox_json(bbox))
81101
return cls(figures)
82102

103+
# image_gcs_uri タスク名をマッピング
104+
# dataItemResourceLabels タグ情報をマッピング
105+
# goog_vi_ml_use 全量に対して7:3でtrainingとtestがわかれるようにマッピング(分割ロジックがよくわからんので割合変えれるようにきりだしとく)
106+
83107
@classmethod
84-
def parse_bbox_json(cls, bbox_json) -> list[VisualInspectionAiFigure]:
108+
def parse_bbox_json(
109+
cls, bbox_json, goog_mi_use_test_probability=0.3
110+
) -> list[VisualInspectionAiFigure]:
85111
result = []
86112
width = bbox_json["width"]
87113
height = bbox_json["height"]
114+
image_gcs_uri = bbox_json["name"]
115+
goog_vi_ml_use = (
116+
"test" if random.random() < goog_mi_use_test_probability else "training"
117+
)
88118
for annotation in bbox_json["annotations"]:
89119
points = annotation["points"]
90-
result.append(VisualInspectionAiFigure.from_bbox(width, height, points))
120+
result.append(
121+
VisualInspectionAiFigure.from_bbox(
122+
width, height, points, image_gcs_uri, goog_vi_ml_use
123+
)
124+
)
91125
return result
92126

93127
@classmethod
@@ -110,6 +144,7 @@ def parse_segmentation_json(cls, json) -> list[VisualInspectionAiFigure]:
110144
return result
111145

112146
def __make_jsonl(self) -> list:
147+
# TODO 削除
113148
print(self.figures)
114149
print(self.figures[0].make_jsonl())
115150
print(list(map(lambda it: it.make_jsonl(), self.figures)))

0 commit comments

Comments
 (0)