一.问题描述
WARNING ⚠️ Box and segment counts should be equal, but got len(segments) = 102, len(boxes) = 105. To resolve this only boxes will be used and all segments will be removed. To avoid this please supply either a detect or segment dataset, not a detect-segment mixed dataset.
大概意思就是标注文件中有segment和bbox的数量对不上,就是数据集中不全是segment标注。
二.解决办法
labelme标注的数据集可以有多种格式,包括但不限于
1.polygon(实例分割的标准格式)
2.rectangle(bbox框格式)
3.circle(圆圈格式)
如果数据集中包含rectangle等标注格式,不加修改直接转yolo格式进行训练的话,会出现数据集错误,产生如上报错信息
2.1 筛选json文件中的标注格式
import os
import json
import cv2
import numpy as np
import shutil
# json目录
fileDir = '/home/fc/sft/Paper_Detection/python/afan/yolov8/dataset/dataset_lingjian3/json'
output_cropped_img_dir = '/home/afan/Desktop/down/new'
# 使用os.listdir()来列出目录中的所有文件
file_names = os.listdir(fileDir)
# 获取该目录下的.json 文件名称
json_files = [file for file in file_names if file.endswith('.json')]
w = []
y = []
# 遍历指定目录下的 .json文件名称
for json_file in json_files:
# 文件名称 去掉 .json
fileName = json_file.replace('.json', '')
print(fileName)
# 读取json文件内容,返回字典格式
with open(f'{fileDir}/{fileName}.json', 'r', encoding='utf8') as fp:
json_data = json.load(fp)
# 创建需要返回的字典 my_dict{'分类名称:[point xy坐标]'}
shaixuan = json_data['shapes']
for element in shaixuan:
if 'shape_type' in element and element['shape_type'] != 'polygon':
print(json_file)
w += [json_file]
y += [element['shape_type']]
break
else:
print("All elements have shape_type 'polygon'")
print(w)
print(y)
上述代码会打印除polygon格式外含有的其他格式以及对应的json文件名。