import json import os #有多少个类别就在字典中写多少给对应的 name2id = {‘mask’:0,‘no-mask’:1} def convert(img_size,box): dw = 1./(img_size[0]) dh = 1./(img_size[1]) x = (box[0] + box[2])/2.0 - 1 y = (box[1] + box[3]) / 2.0 - 1 w = box[2] - box[0] h = box[3] - box[1] x = x * dw w = w * dw y = y * dh h = h * dh return (x,y,w,h) def decode_json (json_floder_path,json_name): #改为转换好的标签的路径 txt_name = “…\labels” + json_name [0:-5] + “.txt” txt_file = open(txt_name,‘w’) json_path = os.path.join(json_floder_path,json_name) data = json.load(open((json_path), ‘r’ ,encoding=‘gb2312’)) img_w = data[‘imageWidth’] img_h = data[‘imageHeight’] for i in data[‘shape’]: label_name = i[‘label’] if (i[‘shape_type’]==‘rectangel’): x1 = int(i[‘points’][0][0]) y1 = int(i[‘points’][0][1]) x2 = int(i[‘points’][1][0]) y2 = int(i[‘points’][1][1]) bb = (x1,y1,x2,y2) bbox = convert((img_w,img_h),bb) txt_file.write(str(name2id[label_name]) + “” + “”.join([str(a) for a in bbox]) + ‘\n’) if name == “main”: #标签的文件夹 json_floder_path = " " json_names = os.listdir(json_floder_path) for json_name in json_names: decode_json(json_floder_path, json_name)
时间: 2024-04-04 07:31:45 浏览: 92
这段代码看起来像是一个将Json格式的标注文件转换为YOLOv5格式的脚本。简单来说,它做了以下几件事情:
1. 定义了一个字典name2id,用于将标注文件中的类别名称映射为数字id;
2. 定义了一个函数convert,用于将标注框的坐标值从图像坐标系转换为归一化坐标系;
3. 定义了一个函数decode_json,用于读取Json格式的标注文件,提取其中的类别和标注框信息,将其转换为YOLOv5格式的标注信息,并将结果写入对应的txt文件中;
4. 在主函数中,遍历标注文件所在的文件夹,调用decode_json函数将每个标注文件转换为YOLOv5格式的标注信息。
需要注意的是,这段代码中可能存在一些小错误,比如矩形框的类型应该是"rectangle"而不是"rectangel",同时在写入txt文件时可能需要在类别id和坐标之间添加一个空格,否则可能会影响后续处理。
相关问题
语义分割jpg转json格式
### 将语义分割的JPG文件转换为JSON格式
为了将语义分割的结果从 JPG 文件转换为 JSON 格式,通常涉及以下几个方面的工作:
#### 数据准备与读取图像
首先需要准备好要处理的 JPG 图像以及对应的类别信息。对于每一张图片中的像素值代表不同的类别的情况,可以通过 Python 的 `PIL` 或者 `opencv` 库来读取这些图像。
```python
from PIL import Image
import numpy as np
# 打开并加载图像
img = Image.open('segmentation_result.jpg')
array_img = np.array(img)
print(array_img.shape, array_img.dtype)
```
#### 创建映射表
创建一个字典形式的颜色到标签ID之间的映射关系,这取决于具体的数据集定义。例如,在 COCO 数据集中,不同颜色对应着特定的对象类别[^1]。
```python
color_to_label_id = {
(0, 0, 0): 0, # 背景
(70, 70, 70): 1, # 建筑物
...
}
```
#### 构建JSON结构体
构建最终输出的 JSON 结构体,其中包含了各个实例的信息,比如多边形坐标、所属分类等。这里可以根据实际需求调整具体的字段名称和内容。
```python
import json
def create_json_structure(image_name, labels_info):
data = {}
data['version'] = "4.5.6"
data['flags'] = {}
shapes = []
for label in labels_info:
shape_data = {"label": str(label["category"]),
"points": [[float(x), float(y)] for x,y in zip(*np.where(label["mask"]==True))],
"group_id": None,
"shape_type": "polygon",
"flags": {}}
shapes.append(shape_data)
data['shapes'] = shapes
data['imagePath'] = image_name
return data
```
#### 处理图像并将结果保存至JSON文件
遍历整个图像数组,利用之前建立好的映射表找到每个位置上应该属于哪个类别;接着根据连通域分析或者其他方法提取出单个对象区域作为 mask 掩膜,并将其加入到上述函数所构造出来的列表里去。最后把所有的形状信息写入新的 JSON 文件中。
```python
unique_colors = set(tuple(v) for m2d in array_img for v in m2d)
labels_info = []
for color in unique_colors:
if color not in color_to_label_id.keys():
continue
category = list(color_to_label_id.keys()).index(color)+1
binary_mask = np.all(array_img == color, axis=-1).astype(int)
labels_info.append({"category": category, "mask":binary_mask})
output_dict = create_json_structure("example_image", labels_info)
with open('output.json', 'w') as outfile:
json.dump(output_dict, outfile, indent=4)
```
通过以上步骤就可以完成由 JPG 到 JSON 的转化过程了。需要注意的是这里的实现方式较为基础,可能还需要针对实际情况做出适当修改优化。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)