yolov9 遇到ValueError: not enough values to unpack (expected 3, got 0)

之前跑过一次没有问题,换数据集之后报错了,除此之外还显示ignoring corrupt image/label: could not convert string to float。

查了好久之后发现是标签打错了,json文件转txt的时候没打对,标签名称应该设置成数字才对。忘记从什么地方爬过来的代码重新转txt就没事了。

import json
import os

def convert_labelme_to_yolo(json_file, output_dir, class_mapping):
    # 打开并读取 JSON 文件
    with open(json_file, 'r', encoding='utf-8') as f:
        data = json.load(f)

    # 获取图像的宽度和高度
    img_width = data['imageWidth']
    img_height = data['imageHeight']

    # 准备输出文件路径
    output_file = os.path.join(output_dir, os.path.splitext(os.path.basename(json_file))[0] + '.txt')

    with open(output_file, 'w') as out_f:
        for shape in data['shapes']:
            label = shape['label']
            points = shape['points']

            # 计算边界框的 x_min, y_min, x_max, y_max
            x_min = min([p[0] for p in points])
            y_min = min([p[1] for p in points])
            x_max = max([p[0] for p in points])
            y_max = max([p[1] for p in points])

            # 转换为 YOLO 格式
            x_center = (x_min + x_max) / 2.0 / img_width
            y_center = (y_min + y_max) / 2.0 / img_height
            width = (x_max - x_min) / img_width
            height = (y_max - y_min) / img_height

            # 获取类别索引
            class_id = class_mapping.get(label, -1)

            if class_id == -1:
                print(f"Warning: label '{label}' not found in class mapping. Skipping.")
                continue

            # 写入 YOLO 格式
            out_f.write(f"{class_id} {x_center} {y_center} {width} {height}\n")

# 定义类别名称到类别索引的映射
class_mapping = {
    'stem0': '0',
    'stem1': '1',
    # 添加其他类别
}

# 使用示例
json_dir = 'E:/code/js9'  # 替换为你的 JSON 文件所在的目录
output_dir = 'E:/code/txt9'  # 替换为你希望保存 TXT 文件的目录

# 确保输出目录存在
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 遍历目录中的所有 JSON 文件进行转换
for json_file in os.listdir(json_dir):
    if json_file.endswith('.json'):
        convert_labelme_to_yolo(os.path.join(json_dir, json_file), output_dir, class_mapping)

stem0和stem1是我用labelme做标注的时候设置的标签,在txt文件里面会把它设置成0和1,这样应该不会报gnoring corrupt image/label: could not convert string to float的错,因为想的是报错因为不能把字符型转成浮点数,所以就设置成数字了。

具体设置的类别可以按照自己的需求修改class_mapping

然后.yaml文件也顺便改了一下

这个相关的其他博客应该有更详细的,我这样就解决了报错问题,仅供参考。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和\[2\]的信息,出现"ValueError: not enough values to unpack (expected 3, got 0)"错误是因为在test.py文件中的一行代码中,期望有3个值被解包,但实际上没有得到任何值。这个错误通常与数据集格式有关。 根据引用\[3\]的分析,可能的原因是数据集格式错误。YOLOv8和YOLOv7的数据集格式有所不同,YOLOv8的数据集中多了一个关键点状态的标注,包括0、1和2。其中,2表示标注且可见,1表示标注但被遮挡,0表示关键点不存在。 为了解决这个问题,你可以检查你的数据集是否符合YOLOv8的格式要求。确保每个样本都包含正确的关键点状态标注,并且没有缺失任何值。如果你使用的是YOLOv7的数据集,你可能需要对其进行相应的调整以适应YOLOv8的格式。 另外,你也可以查看其他可能导致这个错误的原因,比如检查代码中是否有其他地方导致了数据解包错误的情况。 总结起来,"ValueError: not enough values to unpack (expected 3, got 0)"错误通常与数据集格式有关,你需要检查数据集是否符合YOLOv8的格式要求,并确保没有缺失任何值。 #### 引用[.reference_title] - *1* *2* [pycharm 训练YOLO模型时提示 ValueError: not enough values to unpack (expected 3, got 0)错误](https://blog.csdn.net/luoluoaiyuanyuan/article/details/128103085)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [YOLOv8进行关键点检测,使用自己的数据集,报错](https://blog.csdn.net/weixin_43501408/article/details/130810368)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值