基于yolov5的车辆目标检测(二)数据集的获取

数据集的获取

一、将视频转换成图片

import os
import cv2


def video2img(video_folder, img_folder, frequency=10):
    video_files = os.listdir(video_folder)
    for video_file in video_files:
        print("{}图片提取中...".format(video_file))
        times = 0
        img_idx = 0
        video_file_name = video_file.split(".")[0]
        video_file_path = os.path.join(video_folder, video_file)
        img_sub_folder = os.path.join(img_folder, video_file_name)
        if not os.path.exists(img_sub_folder):
            os.makedirs(img_sub_folder)
            camera = cv2.VideoCapture(video_file_path)
            while True:
                times += 1
                res, image = camera.read()
                if not res:
                    print('EOF')
                    break
                if times % frequency == 0:
                    img_name = video_file_name + "_" + str(img_idx) + ".jpg"
                    img_idx = img_idx + 1
                    img_save_path = os.path.join(img_sub_folder, img_name)
                    cv2.imwrite(img_save_path, image)
            print('{}文件图片提取结束:{}张'.format(video_file, img_idx))
            camera.release()


if __name__ == '__main__':
    video2img(video_folder=r'E:\Machine-Vision\Test_Mov\Mov\MVI_2052', img_folder="E:\Machine-Vision\yolov5-master\data\images", frequency=2)

视频存放的路径为绝对路径,由于’E:\Machine-Vision\Test_Mov\Mov\MVI_2052’含有转义字符‘\',所以这里在绝对路径前加r以区分转义字符
video_folder=视频的绝对路径
img_folder=照片保存的绝对路径
frequency=采样频率,相当于每隔frequency,对视频截取一次照片

在这里插入图片描述
这里选择的frequency=10
在这里插入图片描述
视频转换后的照片,保存在images路径下

二、对转换后的照片进行打标签

2.1使用lambeling软件进行打标签

在这里插入图片描述
这里将格式选择为yolo格式

2.2使用make sense进行打标签

make sense
在这里插入图片描述
点击Get Started
在这里插入图片描述
在这里插入图片描述
创建标签类别,选择为橙色,名称为car
在这里插入图片描述
将图片全部打完标签之后,点击上方菜单栏,Actions->Export Annotation->A.zip package containing files in YOLO format
在这里插入图片描述
之后生成zip压缩包,解压后为txt文件
将该文件重命名为labels,存放在与images相同的目录下
在这里插入图片描述
在这里插入图片描述

三、数据集的分类

数据集的分类使用到了如下代码:

import os
import shutil
import random

random.seed(0)


def split_data(file_path, xml_path, new_file_path, train_rate, val_rate, test_rate):
    each_class_image = []
    each_class_label = []
    for image in os.listdir(file_path):
        each_class_image.append(image)
    for label in os.listdir(xml_path):
        each_class_label.append(label)
    data = list(zip(each_class_image, each_class_label))
    total = len(each_class_image)
    random.shuffle(data)
    each_class_image, each_class_label = zip(*data)
    train_images = each_class_image[0:int(train_rate * total)]
    val_images = each_class_image[int(train_rate * total):int((train_rate + val_rate) * total)]
    test_images = each_class_image[int((train_rate + val_rate) * total):]
    train_labels = each_class_label[0:int(train_rate * total)]
    val_labels = each_class_label[int(train_rate * total):int((train_rate + val_rate) * total)]
    test_labels = each_class_label[int((train_rate + val_rate) * total):]

    for image in train_images:
        print(image)
        old_path = file_path + '/' + image
        new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + image
        shutil.copy(old_path, new_path)

    for label in train_labels:
        print(label)
        old_path = xml_path + '/' + label
        new_path1 = new_file_path + '/' + 'train' + '/' + 'labels'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + label
        shutil.copy(old_path, new_path)

    for image in val_images:
        old_path = file_path + '/' + image
        new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + image
        shutil.copy(old_path, new_path)

    for label in val_labels:
        old_path = xml_path + '/' + label
        new_path1 = new_file_path + '/' + 'val' + '/' + 'labels'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + label
        shutil.copy(old_path, new_path)

    for image in test_images:
        old_path = file_path + '/' + image
        new_path1 = new_file_path + '/' + 'test' + '/' + 'images'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + image
        shutil.copy(old_path, new_path)

    for label in test_labels:
        old_path = xml_path + '/' + label
        new_path1 = new_file_path + '/' + 'test' + '/' + 'labels'
        if not os.path.exists(new_path1):
            os.makedirs(new_path1)
        new_path = new_path1 + '/' + label
        shutil.copy(old_path, new_path)


if __name__ == '__main__':
    file_path = "E:\Machine-Vision\Data_get\data\car\images_car"
    xml_path = "E:\Machine-Vision\Data_get\data\car\labels_car"
    new_file_path = "E:\Machine-Vision\Data_get\data\car\imageSets_car"
    split_data(file_path, xml_path, new_file_path, train_rate=0.7, val_rate=0.2, test_rate=0.1)

其中:
images存放训练的图片
labels存放打好的标签.txt文件
imageSets存放分类好的数据集
file_path->images
xml_path->labels
new_file_path->imageSets

在这里插入图片描述

test 测试数据集
train 训练数据集
val 验证数据集

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很抱歉,我不是一个能够提供代码的实体,但是我可以给你指一些方向和资源,帮助你实现基于 YOLOv5车辆识别、测速和测距功能。 1. YOLOv5 的安装和使用:YOLOv5 是一个目标检测算法,可以用于车辆检测。你可以使用 PyTorch 框架来安装和使用 YOLOv5。以下是一些教程和资源,可以帮助你开始学习和使用 YOLOv5: - YOLOv5 官方 GitHub:https://github.com/ultralytics/yolov5 - YOLOv5 安装指南:https://github.com/ultralytics/yolov5/wiki/Installation - YOLOv5 教程:https://github.com/ultralytics/yolov5/wiki/Tutorial 2. 车辆识别:YOLOv5 可以帮助你识别车辆,但是如果你想要更准确的识别,你可以使用已经训练好的模型,如 COCO 数据集上训练好的模型。以下是一些资源,可以帮助你获得已经训练好的模型: - YOLOv5 官方模型:https://github.com/ultralytics/yolov5/releases - COCO 数据集上训练好的 YOLOv5 模型:https://github.com/WongKinYiu/yolov5/tree/master/models 3. 测速和测距:测速和测距需要使用摄像头来获取车辆的位置和速度信息。你可以使用 OpenCV 库来获取摄像头的输入,并使用 YOLOv5 模型识别车辆,然后使用计算机视觉技术来测速和测距。以下是一些教程和资源,可以帮助你学习和使用 OpenCV: - OpenCV 官方网站:https://opencv.org/ - OpenCV 教程:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html 希望以上资源可以帮助你实现基于 YOLOv5车辆识别、测速和测距功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值