CityScapes数据集转voc数据格式训练yolov5(含下载链接)

CityScapes数据集转voc数据格式训练yolov5(含下载链接)

CityScapes数据集需要官网注册才能使用,让人头疼无比,更可气的是国内还有些混蛋竟然将从官方下载好的数据集收费,真可无耻之极,找了好多博客,都是是收费的,看到这些博客,我反手就是一个举报,不为别的,就为它不要脸!!!
这里分享一个CityScapes数据集数据集百度云链接,感谢这个好人的无私分享:分享资料的好人csdn
CityScapes数据集百度云链接
提取码:1uxk

1、CityScapes数据集数据格式:

目录如下所示:
在这里插入图片描述

数据集中的类别标签包含如下(种类是真的多,也蛮丰满的):
在这里插入图片描述

{'caravan', 'wall', 'person', 'terrain', 'dynamic', 'sky', 'traffic light', 'license plate', 'ground', 'ego vehicle', 'guard rail', 'ridergroup', 'truck', 'sidewalk', 'trailer', 'polegroup', 'rectification border','truckgroup', 'parking', 'building', 'tunnel', 'traffic sign', 'pole', 'bicycle', 'vegetation', 'cargroup', 'motorcyclegroup', 'bus', 'train', 'car', 'static', 'out of roi', 'persongroup', 'bridge', 'rail track', 'fence', 'rider', 'motorcycle', 'bicyclegroup', 'road'}

我主要是打算用该数据集进行行人、车辆相关检测的,所需要的图像在leftImg8bit文件目录里面,对应的标注文件都在压缩文件gtFine,赶文件解压后,如下截图所示,
在这里插入图片描述
对,没看错,所需的标注文件都被包含在json格式的文件内:
打开json文件后,标签对应如下所示:
在这里插入图片描述

2、数据处理

(1)将leftImg8bit目录内的所有图像与gtFine目录内的所有json分别汇聚在两个目录下面,命名格式如下所示:
在这里插入图片描述
json数据可视化查看:

import os, json, cv2, shutil
from tqdm import tqdm
import numpy as np


# 转换xml文件
def bboxes2xml(folder, img_name, width, height, gts, xml_save_to):
    xml_file = open((xml_save_to + '/' + img_name + '.xml'), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>' + folder + '</folder>\n')
    xml_file.write('    <filename>' + str(img_name) + '.jpg' + '</filename>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(width) + '</width>\n')
    xml_file.write('        <height>' + str(height) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')

    for gt in gts:
        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str(gt[0]) + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(gt[1]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(gt[2]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(gt[3]) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(gt[4]) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

    xml_file.write('</annotation>')
    xml_file.close()


def seeImage(windowName, imageData):
    """
        显示图像
    :param windowName:  窗体名称
    :param imageData:   图像数据
    """
    cv2.imshow(windowName, imageData)
    cv2.waitKey(0)
    # cv2.destroyAllWindows()


def drawRectangle(image):
    """
        查看轮廓的宽、高比率
    :param image: 图像
    """
    contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    imageOne = np.ones((image.shape[0], image.shape[1], 3), dtype=np.uint8) * 255
    font = cv2.FONT_HERSHEY_SIMPLEX  # 字体样式
    for index in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[index])
        ratioW_H = round(float(w) / h, 2)
        Area = w * h
        txtInfo = str(Area) + "_" + str(ratioW_H)
        cv2.putText(imageOne, txtInfo, (x, y), font, 0.4, (255, 255, 0), 1)
        imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 1)
        print(txtInfo)
    seeImage("imageOne", imageOne)


def showImage(tempFileDir,tempImageDir):
    jsonNames = os.listdir(tempFileDir)
    # labelList = ['person', 'truck', 'trailer', 'bicycle', 'bus', 'train', 'car', 'rider', 'motorcycle']     # 我只需要这几类
    for jsonName in jsonNames:
        jsonName_ = os.path.splitext(jsonName)[0]
        imageName = jsonName_ + ".png"
        imagePath = os.path.join(tempImageDir, imageName)  # 拼接图像路径
        jsonPath = os.path.join(tempFileDir, jsonName)  # 拼接json路径

        imageData = cv2.imread(imagePath)
        imageOne = np.ones((imageData.shape[0], imageData.shape[1], 3), dtype=np.uint8) * 255

        filePath = os.path.join(tempFileDir, jsonPath)

        with open(filePath, "r", encoding="utf-8") as f:
            info = f.read()
            infoLoads = json.loads(info)
            h_image = infoLoads["imgHeight"]  # 图像的高度
            w_image = infoLoads["imgWidth"]  # 图像的宽度
            objects = infoLoads["objects"]
            gts = []
            for object in objects:
                temp_gt = []
                objectName = object["label"]
                # if objectName not in labelList:
                #     continue
                if objectName == "motorcycle":
                    temp_gt.append("motorbike")
                else:
                    temp_gt.append(objectName)
                polygon_ = object["polygon"]
                # print(polygon_)
                pots = np.asarray(polygon_, np.int32)

                cv2.polylines(imageOne,[pots],True,(0,255,0),2)
            seeImage("imageOne",imageOne)


if __name__ == '__main__':
    tempFileDir = r"E:\666\new_json"  # json文件所在目录
    tempImageDir = r"E:\666\new_image"  # 图像文件所在目录
    showImage(tempFileDir,tempImageDir)

利用以上代码将json中的内容读出后,物体轮廓是这样滴:是不是很性感,像极了爱情的颜色!!!
在这里插入图片描述

3、CityScapes数据集转VOC格式转换代码

接下来进入正题,开始将json格式的数据转为训练所需的xml格式

import os, json, cv2, shutil
from tqdm import tqdm
import numpy as np


# 转换xml文件
def bboxes2xml(folder, img_name, width, height, gts, xml_save_to):
    xml_file = open((xml_save_to + '/' + img_name + '.xml'), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>' + folder + '</folder>\n')
    xml_file.write('    <filename>' + str(img_name) + '.jpg' + '</filename>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(width) + '</width>\n')
    xml_file.write('        <height>' + str(height) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')

    for gt in gts:
        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str(gt[0]) + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(gt[1]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(gt[2]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(gt[3]) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(gt[4]) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

    xml_file.write('</annotation>')
    xml_file.close()


def seeImage(windowName, imageData):
    """
        显示图像
    :param windowName:  窗体名称
    :param imageData:   图像数据
    """
    cv2.imshow(windowName, imageData)
    cv2.waitKey(0)
    # cv2.destroyAllWindows()


def drawRectangle(image):
    """
        查看轮廓的宽、高比率
    :param image: 图像
    """
    contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    imageOne = np.ones((image.shape[0], image.shape[1], 3), dtype=np.uint8) * 255
    font = cv2.FONT_HERSHEY_SIMPLEX  # 字体样式
    for index in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[index])
        ratioW_H = round(float(w) / h, 2)
        Area = w * h
        txtInfo = str(Area) + "_" + str(ratioW_H)
        cv2.putText(imageOne, txtInfo, (x, y), font, 0.4, (255, 255, 0), 1)
        imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 1)
        print(txtInfo)
    seeImage("imageOne", imageOne)



def dooption(tempFileDir,tempImageDir,new_dir):
    jsonNames = os.listdir(tempFileDir)
    for jsonName in jsonNames:
        jsonName_ = os.path.splitext(jsonName)[0]
        imageName = jsonName_ + ".png"
        imagePath = os.path.join(tempImageDir, imageName)  # 拼接图像路径
        jsonPath = os.path.join(tempFileDir, jsonName)  # 拼接json路径

        imageData = cv2.imread(imagePath)
        # seeImage("imageData",imageData)
        print(imagePath)
        imageOne = np.ones((imageData.shape[0], imageData.shape[1], 3), dtype=np.uint8) * 255
        # seeImage("imageOne",imageOne)

        filePath = os.path.join(tempFileDir, jsonPath)

        with open(filePath, "r", encoding="utf-8") as f:
            info = f.read()
            infoLoads = json.loads(info)
            h_image = infoLoads["imgHeight"]  # 图像的高度
            w_image = infoLoads["imgWidth"]  # 图像的宽度
            objects = infoLoads["objects"]
            gts = []
            for object in objects:
                temp_gt = []
                objectName = object["label"]
                if objectName == "motorcycle":
                    temp_gt.append("motorbike")
                else:
                    temp_gt.append(objectName)
                polygon_ = object["polygon"]
                # print(polygon_)
                pots = np.asarray(polygon_, np.int32)

                # cv2.polylines(imageOne,[pots],True,(0,255,0),1)
                x, y, w, h = cv2.boundingRect(pots)
                # imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 2)
                xmax = int(x) + int(w)
                ymax = int(y) + int(h)
                temp_gt = temp_gt + [x, y, xmax, ymax]
                gts.append(temp_gt)
            print(gts)
            folder = "images"
            img_name = imageName.split(".")[0]
            width = w_image
            height = h_image
            xml_save_to = new_dir

            bboxes2xml(folder, img_name, width, height, gts, xml_save_to)

if __name__ == '__main__':
    tempFileDir = r"E:\666\new_json"  # json文件存放目录
    tempImageDir = r"E:\666\new_image"  # 图像文件存放目录
    new_dir = r"E:\666\temp"  # 转化得到的xml文件存放目录

    dooption(tempFileDir, tempImageDir, new_dir)

使用labelimg软件可以看到如下所示:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值