PennFudanPed数据集转yolo格式

pennFudanPed数据集只有不到200张图片,标注的行人为非遮挡行人,遮挡和骑车的行人都没有标注
标注框做的比较标注和准确,下面是该数据集直接转成yolo格式的代码,新建train文件夹,然后使用下面代码存为py,然后运行

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
import re
from pathlib import Path
from PIL import Image
import csv
import shutil
import numpy as np

pngpath='./PNGImages/'
oritxt='./Annotation/'
newtxt='./train/'
savepath='./train/'
 
def convert(size, box0,box1,box2,box3):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box0 + box2) / 2 * dw
    y = (box1 + box3)/ 2 * dh
    w = (box2-box0) * dw
    h = (box3-box1) * dh
    return (x, y, w, h)

txtfiles = os.listdir(oritxt)
matrixs  = []
for txtpath in txtfiles:
    img_path = pngpath + txtpath[:-4] + ".png";
    outpath = newtxt + txtpath[:-4] + '.txt'
    with Image.open(img_path) as Img:
            img_size = Img.size
    ans = ''
    f1 = open(oritxt + txtpath, 'r')
    for line in f1.readlines():
        if re.findall('Xmin',line):
            pt = [int(x) for x in re.findall(r"\d+",line)]
            matrixs.append(pt)
            bb = convert(img_size, pt[1], pt[2], pt[3], pt[4])
            ans = ans + '1' + ' ' + ' '.join(str(a) for a in bb) + '\n'
    f1.close()
    with open(outpath, 'w') as outfile:
            outfile.write(ans)
    shutil.copy(img_path, savepath +  txtpath[:-4] + '.png') 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
将COCO数据集换为YOLO格式需要进行以下步骤: 1. 下载COCO数据集:首先,你需要下载COCO数据集,包括图像和标注文件。你可以从COCO官方网站上下载。 2. 解析标注文件:COCO数据集的标注文件是JSON格式的。你可以使用Python中的json库来解析它们。标注文件中包含了每个图像的标注信息,包括类别、边界框的坐标等。 3. 换为YOLO格式:将COCO数据集的标注信息换为YOLO格式需要进行一些计算和换。首先,YOLO格式要求边界框坐标是相对于图像宽度和高度的比例值,而不是像素坐标。因此,你需要将COCO数据集中的像素坐标换为相对值。另外,YOLO格式要求每个标注行包含类别索引和边界框坐标,以空格分隔。 4. 保存为txt文件:最后,将换后的YOLO格式标注信息保存为txt文件。每个图像对应一个txt文件,文件名与图像文件名相同,只是扩展名不同。 下面是一个示例代码,演示了如何将COCO数据集换为YOLO格式: ```python import json # 读取COCO标注文件 with open('annotations.json', 'r') as f: annotations = json.load(f) # 换为YOLO格式 yolo_annotations = [] for annotation in annotations: image_id = annotation['image_id'] category_id = annotation['category_id'] bbox = annotation['bbox'] # 计算边界框相对于图像的比例值 x, y, width, height = bbox img_width, img_height = get_image_size(image_id) # 获取图像宽度和高度 x_rel = x / img_width y_rel = y / img_height width_rel = width / img_width height_rel = height / img_height # 将标注信息换为YOLO格式 yolo_annotation = f"{category_id} {x_rel} {y_rel} {width_rel} {height_rel}" yolo_annotations.append(yolo_annotation) # 保存为txt文件 with open('annotations.txt', 'w') as f: for annotation in yolo_annotations: f.write(annotation + '\n') ``` 请注意,上述代码仅是一个示例,你需要根据你的实际情况进行适当修改和调整。另外,你可能还需要编写一些辅助函数来获取图像的宽度和高度等信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wukongxuetang

如果文章对你的工作有帮助就鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值