GTSDB标签转COCO标签

caffe学习(10):交通标志目标检测训练整体流程_超超越越一亿粉丝的博客-CSDN博客

先看看COCO数据集格式

在COCO中 一个图片对应一个同名的txt,其中,第一个值是类别,COCO中是0 -79 80个类别

后四位是中心点 cx cy 图像宽高 的scale,整张图像视为单位1

再来看GTSDB标签

Public Archive: ff17dc924eba88d5d01a807357d6614c

 测试集和训练集标签都在一起,都在同一个txt中。依次是文件名,box: x1 y1 x2 y2 左上角右下角

要做的就是把他拆开,一个图片对应一个txt ,顺便把他的box格式改成COCO格式

input_train_path = "E:\\workspace\\YOLOv3\\data\\FullIJCNN2013_jpg\\gt.txt"
output_train_path = "E:\\workspace\\YOLOv3\\data\\FullIJCNN2013_jpg\\"
orign_w =1360
orign_h = 800
def get_label(label):
    prohibitory = [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 15, 16]  # (circular, white ground with red border)
    mandatory = [33, 34, 35, 36, 37, 38, 39, 40]  # (circular, blue ground)
    danger = [11, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]  # (triangular, white ground with red border)

    if label in prohibitory:
        new_label = "0"
    elif label in mandatory:
        new_label = "1"
    elif label in danger:
        new_label = "2"
    else:
        new_label = "3"

    return new_label
#read origin txt transform to coco
def gt2yolo():
    file_name=''
    for line in open(input_train_path,"r"):  # 设置文件对象
        # line = f.readline().strip()
        # while line :
            words = line.split(';')
            img_name= words[0].split(".")[0]
            label = get_label(int(words[-1]))
            x = float(words[1]) /orign_w
            w = float(words[3])/ orign_w -x
            y = float(words[2]) / orign_h
            h = float(words[4]) / orign_h - y
            #save
            if output_train_path+img_name+".txt" != file_name:
                file_name = output_train_path+img_name+".txt"
                fw = open(file_name, 'w')
            else:
                fw = open(file_name, 'a')
            fw.write(label+" "+str(x) + " " + str(y) + " "+ str(w) + " "+ str(h) + "\n")
            fw.close()

if __name__ == '__main__':
    gt2yolo()
    print("transform finsih")

            我这里有问题,我这个是左上角的点和宽高的scale,不是中心点,读者自行修改一下

             x = float(words[1]) /orign_w
            w = float(words[3])/ orign_w -x
            y = float(words[2]) / orign_h
            h = float(words[4]) / orign_h - y  

gt路径

input_train_path = "E:\\workspace\\YOLOv3\\data\\FullIJCNN2013_jpg\\gt.txt"

目标路径

output_train_path = "E:\\workspace\\YOLOv3\\data\\FullIJCNN2013_jpg\\"

弄完之后

 

 注意,弄完之后的txt和图片数量不一样,txts数量少,这是因为有垃圾数据,就是只有背景的照片

我把不要拿垃圾数据集去训练,接下来我们把垃圾数据拿走,然后把数据集分为测试机和训练集

第一个函数是按比例随机取一些文件走放在文件夹中,txt和jpg都可以,第二函数是把和图片名称同名的txt称移动到到一个文件夹中,第三个函数是把和txt名称同名的jpg名称移动到到一个文件夹中,

因此我们先执行第三个函数,把垃圾数据去掉,就是把和lable同名的图片移动走

然后执行第一个函数,取10%做测试集(填标签路径和图片路径都行,后面步骤对应改一下),然后执行第二个(第三个),把和测试集同名标签(图片)移动走

若第一个函数是填的标签路径,则执行第三个函数,移动图片,否则反之

import os
import cv2
import csv
import math
import random
import shutil


def move(filepath, destpath):
    # 从数据集中随机选取20%移动到另一文件夹下作为测试集,剩下的80%作为训练集
    pathdir = os.listdir(filepath)
    ranpath = random.sample(pathdir, int(0.1 * len(pathdir)))
    print(ranpath)
    for alldir in ranpath:
        child = os.path.join(filepath, alldir)
        dest = os.path.join(destpath, alldir)
        shutil.copy(child, dest)
        os.remove(child)


def move_label(imgpath, labelpath, testpath):
    # 根据不同文件夹下的图片移动相应图片的标签
    labels = os.listdir(labelpath)
    for label in labels:
        imgdir = os.listdir(imgpath)
        for img in imgdir:
            if label.strip('.txt') == img.strip('.jpg'):
                print('###')
                label_path = os.path.join(labelpath, label)
                test_path = os.path.join(testpath, label)
                print(label_path)
                if labelpath:
                    shutil.copy(label_path, test_path)
                    os.remove(label_path)


def move_img(imgpath, labelpath, testpath):
    labels = os.listdir(labelpath)
    for label in labels:
        imgdir = os.listdir(imgpath)
        for img in imgdir:
            if label.strip('.txt') == img.strip('.jpg'):
                print('###')
                img_path = os.path.join(imgpath, img)
                test_path = os.path.join(testpath, img)
                shutil.copy(img_path, test_path)
                os.remove(img_path)


if __name__ == "__main__":
    # move('E:\\workspace\\YOLOv3\\data\\custom\\images\\train', 'E:\\workspace\\YOLOv3\\data\\custom\\images\\test')
    move_label("E:\\workspace\\YOLOv3\\data\\custom\\images\\test",
               'E:\\workspace\\YOLOv3\\data\\custom\\labels\\train',
               'E:\workspace\YOLOv3\data\custom\labels\\test')

最后做一个txt 把所有图片路径放进去

import csv
import math
import random
import shutil
def make_txt(img_path,img_rel_path,txt_path):
    # 根据不同文件夹下的图片移动相应图片的标签
    fw = open(txt_path, 'w')
    fw.close()
    fw = open(txt_path, 'a')
    names = os.listdir(img_path)
    for name in names:
        line = img_rel_path+ name
        fw.write(line + "\n")
    fw.close()
if __name__ == '__main__':
    make_txt("E:\\workspace\\YOLOv3\\data\\custom\\images\\train","data/custom/images/train/",
            "E:\\workspace\\YOLOv3\\data\\custom\\train1.txt")
    make_txt("E:\\workspace\\YOLOv3\\data\\custom\\images\\test", "data/custom/images/test/",
             "E:\\workspace\\YOLOv3\\data\\custom\\valid1.txt")
    print("transform finsih")

就是把路径下的所有图片名和指定相对路拼接,写在txt中

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值