2025-05-27 15:46:31 +08:00

112 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
class AbdomenData:
def __init__(self):
"""
初始化处理器,设置默认值
"""
self.vertices = [] # 腹部四个顶点
self.navel_point = None # 肚脐眼坐标
self.length = 0 # 腹部长度
self.width = 0 # 腹部宽度
def load_data(self, json_data):
"""
加载并处理JSON数据
Args:
json_data: 包含标注信息的JSON数据字典
"""
self._process_data(json_data)
return self
def _calculate_distance(self, point1, point2):
"""计算两点之间的欧几里得距离"""
return np.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)
def _process_data(self, json_data):
"""处理JSON数据提取所需信息"""
for shape in json_data.get('shapes', []):
label = shape['label']
points = shape['points']
if shape['shape_type'] == 'polygon' and label == 'fubu':
# 存储四个顶点坐标
self.vertices = [tuple(map(float, point)) for point in points]
# 计算长和宽
self.length = self._calculate_distance(points[0], points[1]) # 第1点和第2点之间的距离
self.width = self._calculate_distance(points[1], points[2]) # 第2点和第3点之间的距离
elif shape['shape_type'] == 'point' and label == 'duqiyan':
# 存储肚脐眼坐标
self.navel_point = tuple(map(float, points[0]))
def get_all_data(self):
"""获取所有处理后的数据"""
return {
'vertices': self.vertices,
'navel_point': self.navel_point,
'dimensions': {
'length': self.length,
'width': self.width
}
}
def get_vertices(self):
"""获取四个顶点坐标"""
return self.vertices
def get_navel_point(self):
"""获取肚脐眼坐标"""
return self.navel_point
def get_dimensions(self):
"""获取长度和宽度"""
return {
'length': self.length,
'width': self.width
}
# 使用示例
if __name__ == "__main__":
# 示例JSON数据
sample_json = {
"version": "5.6.0",
"flags": {},
"shapes": [
{
"label": "fubu",
"points": [
[263.12977099236645, 119.96183206106873],
[451.2977099236641, 140.19083969465652],
[439.0839694656489, 243.24427480916032],
[250.53435114503822, 218.4351145038168]
],
"shape_type": "polygon"
},
{
"label": "duqiyan",
"points": [[358.5496183206107, 184.84732824427482]],
"shape_type": "point"
}
]
}
# 创建处理器实例并加载数据
processor = AbdomenData().load_data(sample_json)
# 获取所有数据
all_data = processor.get_all_data()
# 打印结果
# print("四个顶点坐标:")
for i, vertex in enumerate(processor.get_vertices(), 1):
print(f"vertex {i}: {vertex}")
print(f"\position : {processor.get_navel_point()}")
dimensions = processor.get_dimensions()
print(f"\nlength: {dimensions['length']:.2f}")
print(f"width: {dimensions['width']:.2f}")