当前位置: 首页 > wzjs >正文

建设一个地方门户网站快速建站平台

建设一个地方门户网站,快速建站平台,专业团队口号,二级网站排名做不上去之前的博文【目标检测】YOLOv5跑通VisDrone数据集对Visdrone数据集简介过,这里不作复述,本文主要对Visdrone数据集和CARPK数据集进行目标提取和过滤。 需求描述 本文需要将Visdrone数据集中有关车和人的数据集进行提取和合并,车标记为类别0&…

之前的博文【目标检测】YOLOv5跑通VisDrone数据集对Visdrone数据集简介过,这里不作复述,本文主要对Visdrone数据集和CARPK数据集进行目标提取和过滤。

需求描述

本文需要将Visdrone数据集中有关车和人的数据集进行提取和合并,车标记为类别0,人标记为类别1,并转换成YOLO支持的txt格式。

Visdrone数据集

Visdrone数据集转换成YOLO的txt格式

首先对原始数据集做一个格式转换,下面这段代码延用官方提供的转换脚本。

from utils.general import download, os, Pathdef visdrone2yolo(dir):from PIL import Imagefrom tqdm import tqdmdef convert_box(size, box):# Convert VisDrone box to YOLO xywh boxdw = 1. / size[0]dh = 1. / size[1]return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh(dir / 'labels').mkdir(parents=True, exist_ok=True)  # make labels directorypbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}')for f in pbar:img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).sizelines = []with open(f, 'r') as file:  # read annotation.txtfor row in [x.split(',') for x in file.read().strip().splitlines()]:if row[4] == '0':  # VisDrone 'ignored regions' class 0continuecls = int(row[5]) - 1  # 类别号-1box = convert_box(img_size, tuple(map(int, row[:4])))lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n")with open(str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'labels' + os.sep), 'w') as fl:fl.writelines(lines)  # write label.txtdir = Path(r'E:\Dataset\VisDrone')  # datasets文件夹下Visdrone2019文件夹目录
# Convert
for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':visdrone2yolo(dir / d)  # convert VisDrone annotations to YOLO labels

标签可视化

对txt标签进行可视化,查看过滤之前的效果。

import os
import numpy as np
import cv2# 修改输入图片文件夹
img_folder = "image"
img_list = os.listdir(img_folder)
img_list.sort()
# 修改输入标签文件夹
label_folder = "labels2"
label_list = os.listdir(label_folder)
label_list.sort()
# 输出图片文件夹位置
path = os.getcwd()
output_folder = path + '/' + str("output")
os.mkdir(output_folder)# 坐标转换
def xywh2xyxy(x, w1, h1, img):label, x, y, w, h = x# print("原图宽高:\nw1={}\nh1={}".format(w1, h1))# 边界框反归一化x_t = x * w1y_t = y * h1w_t = w * w1h_t = h * h1# print("反归一化后输出:\n第一个:{}\t第二个:{}\t第三个:{}\t第四个:{}\t\n\n".format(x_t, y_t, w_t, h_t))# 计算坐标top_left_x = x_t - w_t / 2top_left_y = y_t - h_t / 2bottom_right_x = x_t + w_t / 2bottom_right_y = y_t + h_t / 2# print('标签:{}'.format(labels[int(label)]))# print("左上x坐标:{}".format(top_left_x))# print("左上y坐标:{}".format(top_left_y))# print("右下x坐标:{}".format(bottom_right_x))# print("右下y坐标:{}".format(bottom_right_y))# 绘制矩形框# cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), colormap[1], 2)# (可选)给不同目标绘制不同的颜色框if int(label) == 0:cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), (0, 255, 0), 2)elif int(label) == 1:cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), (255, 0, 0), 2)else:cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), (0, 0, 0), 2)return imgif __name__ == '__main__':for i in range(len(img_list)):image_path = img_folder + "/" + img_list[i]label_path = label_folder + "/" + label_list[i]# 读取图像文件img = cv2.imread(str(image_path))h, w = img.shape[:2]# 读取 labelswith open(label_path, 'r') as f:lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32)# 绘制每一个目标for x in lb:# 反归一化并得到左上和右下坐标,画出矩形框img = xywh2xyxy(x, w, h, img)"""# 直接查看生成结果图cv2.imshow('show', img)cv2.waitKey(0)"""cv2.imwrite(output_folder + '/' + '{}.png'.format(image_path.split('/')[-1][:-4]), img)

可视化效果如图所示:
注:该数据集对人的姿态还进行区分,行走状态的人划分为pedestrian,其它姿态(比如躺下或坐下)标记为people。

在这里插入图片描述

过滤标签

具体过滤规则:

  • 合并car、van、truck、bus为car(0)
  • 合并pedestrian,people为person(1)
  • 舍弃其它类别
import os
import numpy as np
from tqdm import tqdm# Visdrone类别
# names: ['pedestrian', 'people', 'bicycle', 'car', 'van', 'truck', 'tricycle', 'awning-tricycle', 'bus', 'motor' ]# 修改输入标签文件夹
label_folder = "labels"
label_list = os.listdir(label_folder)# 标签输出文件夹
label_output = "labels2"# class_set
car_set = [3, 4, 5, 8]
person_set = [0, 1]if __name__ == '__main__':for label_file in tqdm(os.listdir(label_folder)):# 读取 labelswith open(os.path.join(label_folder, label_file), 'r') as f:lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32)# 写入 labelswith open(os.path.join(label_output, label_file), 'a') as f:for obj in lb:# 若是行人,修改类别为1if int(obj[0]) in person_set:obj[0] = 1f.write(('%g ' * 5).rstrip() % tuple(obj) + '\n')# 若是车辆,修改类别为0elif int(obj[0]) in car_set:obj[0] = 0f.write(('%g ' * 5).rstrip() % tuple(obj) + '\n')

过滤之后的效果如图所示:

在这里插入图片描述

CARPK数据集

CARPK数据集是无人机在40米高空拍摄的汽车数据集,里面仅包含汽车单一目标。

下载地址:https://github.com/zstar1003/Dataset

原始label格式:

1019 521 1129 571 1
1013 583 1120 634 1

对应含义为: xmin, ymin, xmax, ymax,cls

处理脚本:

import os
import numpy as np
from tqdm import tqdm# 修改输入标签文件夹
# label_folder = r"E:\Dataset\CARPK_devkit\data\Annotations"
label_folder = r"annotations"
label_list = os.listdir(label_folder)# 标签输出文件夹
label_output = r"labels"# 图像宽高
img_width = 1280
img_height = 720if __name__ == '__main__':for label_file in tqdm(os.listdir(label_folder)):# 读取 labelswith open(os.path.join(label_folder, label_file), 'r') as f:lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=int)for obj in lb:class_index = obj[4]xmin, ymin, xmax, ymax = obj[0], obj[1], obj[2], obj[3]# 将box信息转换到yolo格式xcenter = xmin + (xmax - xmin) / 2ycenter = ymin + (ymax - ymin) / 2w = xmax - xminh = ymax - ymin# 绝对坐标转相对坐标,保存6位小数xcenter = round(xcenter / img_width, 6)ycenter = round(ycenter / img_height, 6)w = round(w / img_width, 6)h = round(h / img_height, 6)info = [str(i) for i in [class_index, xcenter, ycenter, w, h]]# 写入 labelswith open(os.path.join(label_output, label_file), 'a') as f:# 若文件不为空,添加换行if os.path.getsize(os.path.join(label_output, label_file)):f.write("\n" + " ".join(info))else:f.write(" ".join(info))

可视化验证转换效果:

在这里插入图片描述

http://www.dtcms.com/wzjs/218343.html

相关文章:

  • 买个网站服务器多少钱百度公司好进吗
  • 建筑工程招投标网站在线咨询
  • 有哪个网站做正品港货东莞seo建站公司
  • 胶南网站建设价格百度站长官网
  • 跨网浏览器南宁关键词优化服务
  • 网站改版需要注意哪些seo问题seo查询官方网站
  • 58同城推广怎么收费seo顾问推推蛙
  • 政府网站设计打开网址资料网站
  • 南庄网站建设优化设计电子课本下载
  • 免费的创建个人网站搜一搜排名点击软件
  • 高端网站制作 上海网络营销和传统营销有什么区别
  • 做设计的都用那些网站网络营销的手段有哪些
  • 无锡做网站seo百度一下点击搜索
  • 南京移动网站建设天津网站排名提升多少钱
  • 哪个网站可以接cad图纸做优化大师有必要安装吗
  • 做网站开发要具备什么知识如何自己制作网站
  • 91色做爰免费网站关键词优化是怎样收费的
  • 专门帮做ppt的网站吗品牌营销推广方案怎么做
  • 如何做网站新手个人教程软文的目的是什么
  • 建设监理工程师网站百度搜索排名怎么靠前
  • 网站版面布局设计的原则百度灰色词优化排名
  • 怎么做qq刷赞等网站网站友情链接的好处
  • 各种类型网站建设口碑好保定seo网络推广
  • 色一把看片网 做最好的在线看片网站如何做推广推广技巧
  • 长沙优质营销网站建设设计seo高级优化技巧
  • 泉州网站建设公司在百度上怎么卖自己的产品
  • 学习网站建设深圳网络营销信息推荐
  • 用.net做视频网站的案例真正免费建站
  • 流行的网站建设技术有哪些专业推广公司
  • wordpress获取当前页面的别名宁波seo外包代运营