Skip to content

Commit 04ea578

Browse files
author
shuichi
committed
add segmentation logic
1 parent 7510321 commit 04ea578

File tree

3 files changed

+84
-37
lines changed

3 files changed

+84
-37
lines changed

examples/bbox_to_visual_inspection_ai.py

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
import os
3+
4+
from visual_inspection_ai import VisualInspectionAi
5+
6+
7+
class VisualInspectionAiConverter:
8+
def convert_from_bbox(
9+
self, file_path: str, export_file_path: str, project_type: str
10+
) -> dict:
11+
json = self.open_bbox(file_path)
12+
if project_type == "bbox":
13+
visual_inspection_ai = VisualInspectionAi.from_bbox(json)
14+
else:
15+
visual_inspection_ai = VisualInspectionAi.from_segmentation(json)
16+
17+
os.makedirs(os.path.dirname(export_file_path), exist_ok=True)
18+
with open(export_file_path, "w") as file:
19+
file.write(visual_inspection_ai.as_jsonl())
20+
21+
def open_bbox(self, file_path: str) -> any:
22+
return json.load(open(file_path, "r"))
23+
24+
25+
converter = VisualInspectionAiConverter()
26+
27+
export_file_path = (
28+
"./export/visual_inspection_ai/test.jsonl" # 出力先のファイルパスを指定
29+
)
30+
31+
# bboxの場合
32+
# file_path = (
33+
# "./import/visual_inspection_ai/bbox/annotations.json" # 入力元のファイルパスを指定
34+
# )
35+
# project_type = "bbox"
36+
37+
# segmentationの場合
38+
file_path = "./import/visual_inspection_ai/segmentation/annotations.json" # 入力元のファイルパスを指定
39+
project_type = "segmentation"
40+
41+
result = converter.convert_from_bbox(file_path, export_file_path, project_type)

examples/visual_inspection_ai.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)