Yolov5(一)VOC劃分數據集、VOC轉YOLO數據集

代碼使用方法注意修改一下路徑、驗證集比例、類別名稱,其他均不需要改動,自動劃分訓練集、驗證集、建好全部文件夾、一鍵自動生成Yolo格式數據集在當前目錄下,大家可以直接修改相應的配置文件進行訓練。

目錄

使用方法:

?全部代碼:


使用方法:

?全部代碼:

import os,random,shutilimport xml.etree.ElementTree as ET
import pickle
import os
from os import listdir,getcwd
from os.path import joindef convert(size,box):x_center=(box[0]+box[1])/2.0y_center=(box[2]+box[3])/2.0x=x_center/size[0]y=y_center/size[1]w=(box[1]-box[0])/size[0]h=(box[3]-box[2])/size[1]return (x,y,w,h)def convert_annotation(xml_file_path,save_txt_file_path,classes):xml_file=os.listdir(xml_file_path)print(xml_file)for xml_name in xml_file:print(xml_file)xml_file=os.path.join(xml_file_path,xml_name)out_txt_path=os.path.join(save_txt_file_path,xml_name.split('.')[0]+".txt")out_txt_f=open(out_txt_path,'w')tree=ET.parse(xml_file)root=tree.getroot()size=root.find('size')w=int(size.find("width").text)h=int(size.find("height").text)for obj in root.iter("object"):difficult= obj.find('difficult').textcls=obj.find('name').textif cls not in classes or int(difficult)==1:continuecls_id=classes.index(cls)xmlbox=obj.find('bndbox')b=(float(xmlbox.find('xmin').text),float(xmlbox.find('xmax').text),float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))print(w,h,b)bb=convert((w,h),b)out_txt_f.write(str(cls_id)+" "+" ".join([str(a) for a in bb])+"\n")def moving(fileDir,tarDir,rate=0.2):pathDir=os.listdir(fileDir)filenumber=len(pathDir)#自定義抽取圖片比例picknumber=int(filenumber*rate)#按照rate比例從文件夾中取一定數量的圖片sample=random.sample(pathDir,picknumber)#隨機選取picknumber數量的圖片print(sample)for name in sample:shutil.move(fileDir+"/"+name,tarDir+"/"+name)returndef movelabel(file_list,file_label_train,file_label_val):for i in file_list:if i.endswith(".png") or i.endswith(".jpg"):#filename=file_label_train+"/"+i[:-4] ? ? ? 可以將.xml文件將.txt改成.xml文件filename=file_label_train+"/"+i[:-4]+".xml" ? ? ? #可以改成xml文件將.txt改成.xmlif os.path.exists(filename):shutil.move(filename,file_label_val)print("處理成功")if __name__=="__main__":"""設置圖片路徑、label路徑、驗證集比例、類別"""fileDir=r"./JPEGImages" ? ?#圖片的路徑file_label_train = r"./Annotations" ? ?#標簽文件的路徑rate=0.2 ?#驗證集的比例classes1 = ['fire']"""以下均不需要改動"""if not os.path.exists("./JPEGImages_val"):# Create the folderos.makedirs("./JPEGImages_val")tarDir=r"./JPEGImages_val"moving(fileDir,tarDir,rate)file_list=os.listdir(tarDir)if not os.path.exists("./Annotations_val"):# Create the folderos.makedirs("./Annotations_val")file_label_val=r"./Annotations_val"movelabel(file_list,file_label_train,file_label_val)#VOC轉Yolo格式# 2 voc 格式的xml標簽文件if not os.path.exists("./val"):# Create the folderos.makedirs("./val")if not os.path.exists("./train"):# Create the folderos.makedirs("./train")xml_files1 = r"./Annotations_val"save_txt_files1 = r"./val"convert_annotation(xml_files1, save_txt_files1, classes1)xml_files1 = r"./Annotations"save_txt_files1 = r"./train"convert_annotation(xml_files1, save_txt_files1, classes1)#創建所有文件夾if not os.path.exists("./images"):# Create the folderos.makedirs("./images")if not os.path.exists("./labels"):# Create the folderos.makedirs("./labels")#將所有文件移動到最終的文件夾中import shutil# Define the source and destination folderssource_folder = "./train"destination_folder = "./labels"# Move the files from the source folder to the destination foldershutil.move(source_folder, destination_folder)source_folder = "./val"destination_folder = "./labels"# Move the files from the source folder to the destination foldershutil.move(source_folder, destination_folder)source_folder = "./JPEGImages"destination_folder = "./images"# Move the files from the source folder to the destination foldershutil.move(source_folder, destination_folder)os.rename("./images/JPEGImages", "./images/train")source_folder = "./JPEGImages_val"destination_folder = "./images"# Move the files from the source folder to the destination foldershutil.move(source_folder, destination_folder)os.rename("./images/JPEGImages_val", "./images/val")

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/34893.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/34893.shtml
英文地址,請注明出處:http://en.pswp.cn/news/34893.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決監督學習,深度學習報錯:AttributeError: ‘xxx‘ object has no attribute ‘module‘!!!!

哈嘍小伙伴們大家好呀,很長時間沒有更新啦,最近在研究一個問題,就是AttributeError: xxx object has no attribute module 今天終于是解決了,所以來記錄分享一下: 我這里出現的問題是: 因為我的數據比較大…

SQL優化

一、插入數據 優化 1.1 普通插入(小數據量) 普通插入(小數據量): 采用批量插入(一次插入的數據不建議超過1000條)手動提交事務主鍵順序插入 1.2 大批量數據插入 大批量插入:&…

Android 開發中需要了解的 Gradle 知識

作者:wkxjc Gradle 是一個基于 Groovy 的構建工具,用于構建 Android 應用程序。在 Android 開發中,了解 Gradle 是非常重要的,因為它是 Android Studio 默認的構建工具,可以幫助我們管理依賴項、構建應用程序、運行測試…

macOS 如何安裝git和nvm

首先:先來安裝git 打開macOS終端 將下面的命令復制粘貼進去: curl -O https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.41.0.tar.gz 版本號可以參考一下官網的 我這里安裝的是目前最新的2.41.0 然后在終端輸入下面的代碼或者雙擊git的…

數據結構:力扣OJ題

目錄 ?編輯題一:鏈表分割 思路一: 題二:相交鏈表 思路一: 題三:環形鏈表 思路一: 題四:鏈表的回文結構 思路一: 鏈表反轉: 查找中間節點: 本人實力…

YOLOv8+ByteTrack多目標跟蹤(行人車輛計數與越界識別)

課程鏈接:https://edu.csdn.net/course/detail/38901 ByteTrack是發表于2022年的ECCV國際會議的先進的多目標跟蹤算法。YOLOv8代碼中已集成了ByteTrack。本課程使用YOLOv8和ByteTrack對視頻中的行人、車輛做多目標跟蹤計數與越界識別,開展YOLOv8目標檢測…

Leetcode每日一題:23. 合并 K 個升序鏈表(2023.8.12 C++)

目錄 23. 合并 K 個升序鏈表 題目描述: 實現代碼與解析: 優先級隊列: 原理思路: 23. 合并 K 個升序鏈表 題目描述: 給你一個鏈表數組,每個鏈表都已經按升序排列。 請你將所有鏈表合并到一個升序鏈表…

Flutter: A RenderFlex overflowed by 42 pixels on the bottom.

Flutter:渲染活動底部上方溢出了42個像素 Flutter 控件超出異常:A RenderFlex overflowed by 42 pixels on the bottom. 解決方案 1.Scaffold內添加 resizeToAvoidBottomInset 屬性,缺點是軟鍵盤下面的控件被擋住 Scaffold( resizeToAvoidBot…

第一百二十七天學習記錄:我的創作紀念日

機緣 今天收到CSDN官方的來信,想想也可以對我前面的學習記錄進行一個總結。 關于來到CSDN的初心,也就是為了讓自己養成一個良好的學習總結的習慣。這里要感謝我C語言視頻教程的老師,是他建議學生們在技術博客中進行記錄。對于技術博客&…

web-Element

在vueapp里<div><!-- <h1>{{message}}</h1> --><element-view></element-view></div> <div><!-- <h1>{{message}}</h1> --><element-view></element-view></div>在view新建個文件 <t…

C++ VTK 8.2 如何繪制彈簧圖形

//創建圓柱 vtkSmartPointer<vtkCylinderSource> spCylinderSource vtkSmartPointer<vtkCylinderSource>::New(); spCylinderSource->SetHeight(m_dCylinderHeight); // 設置圓柱的高度 spCylinderSource->SetRadius(m_dCylinderRadius)…

Spring(12) BeanFactory 和 ApplicationContext 區別

目錄 一、BeanFactory 和 ApplicationContext 區別&#xff1f;二、既然 Spring Boot 中使用的是 ApplicationContext 進行應用程序的啟動和管理&#xff0c;那么 Spring Boot 會用到 BeanFactory 嗎&#xff1f; 一、BeanFactory 和 ApplicationContext 區別&#xff1f; Bea…

git clone使用https協議報錯OpenSSL SSL_read: Connection was reset, errno 10054

在使用git 下載github上的代碼時&#xff0c; 一般有ssh協議和https協議兩種。使用ssh協議可以成功clone代碼&#xff0c; 但使用https協議時出錯&#xff1a; $ git clone https://github.com/openai/improved-diffusion.git Cloning into improved-diffusion... fatal: unab…

vue或uniapp使用pdf.js預覽

一、先下載穩定版的pdf.js&#xff0c;可以去官網下載 官網下載地址 或 pdf.js包下載(已配置好&#xff0c;無需修改) 二、下載好的pdf.js文件放在public下靜態文件里&#xff0c; uniapp是放在 static下靜態文件里 三、使用方式 1. vue項目 注意路徑 :src"static/pd…

每日一題 206反轉鏈表

題目 給你單鏈表的頭節點 head &#xff0c;請你反轉鏈表&#xff0c;并返回反轉后的鏈表。 示例 1&#xff1a; 輸入&#xff1a;head [1,2,3,4,5] 輸出&#xff1a;[5,4,3,2,1]示例 2&#xff1a; 輸入&#xff1a;head [1,2] 輸出&#xff1a;[2,1]示例 3&#xff1a; …

塊、行內塊水平垂直居中

1.定位實現水平垂直居中 <div class"outer"><div class"test inner1">定位實現水平垂直居中</div></div><style>.outer {width: 300px;height: 300px;border: 1px solid gray;margin: 100px auto 0;position: relative;}.te…

途樂證券-新股行情持續火爆,哪些因素影響首日表現?

全面注冊制以來&#xff0c;參加打新的投資者數量全體呈現下降。打新收益下降&#xff0c;破發頻出的布景下&#xff0c;投資者打新策略從逢新必打逐步向優選個股改變。 經過很多歷史數據&#xff0c;從商場定價、參加者熱度以及機構重視度維度揭秘了上市后股價體現優秀的個股具…

在多頁面應用和單頁面應用中(例如vue)怎么提高seo搜索引擎優化

那么 我們要先知道 搜索引擎是怎么工作的&#xff1f; 搜索引擎是通過一系列步驟來工作的&#xff0c;以下是其基本原理&#xff1a; 1、網絡爬蟲&#xff1a;搜索引擎使用網絡爬蟲&#xff08;也稱為蜘蛛、機器人&#xff09;來從互聯網上抓取網頁。網絡爬蟲按照預定義的規則…

Redis 之 緩存預熱 緩存雪崩 緩存擊穿 緩存穿透

目錄 一、緩存預熱 1.1 緩存預熱是什么&#xff1f; 1.2 解決方案&#xff1a; 二、緩存雪崩 2.1 緩存雪崩是什么&#xff1f;怎么發生的&#xff1f; 2.2 怎么解決 三、緩存穿透 3.1 是什么&#xff1f;怎么產生的呢&#xff1f; 3.2 解決方案 3.2.1、采用回寫增強&a…

Ceph入門到精通-分布式存儲產品的測試實踐

分布式存儲產品的測試實踐 在分布式存儲產品的測試過程中&#xff0c;測試到底做了些什么事情呢&#xff1f; 一&#xff1a;測試工作內容 需求&#xff0c;設計評審 測試需要參與到每一個過程中 在設計評審的時候就需要知道驗收的標準&#xff0c;這是最重要的開始。因為這…