C#本地将labelme数据集转换为机器视觉yolo数据集格式

ops/2025/3/19 19:16:20/

C#本地,将labelme数据集转换为机器视觉yolo数据集格式

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.RegularExpressions;
using System.Text.Unicode;
using System.Threading.Tasks;
using System.Windows.Ink;
using System.Windows.Media.Media3D;namespace WpfAppYolo11_test1
{/// <summary>/// 将labelme数据集转换为yolo数据集格式/// </summary>public class LabelmeToYoloDataSetUtils{Dictionary<string, short> labels = new Dictionary<string, short>();/// <summary>/// 转换后保存目录/// </summary>const string outDir = "D:\\yoloTrainDataSet";const int saveLength = 10;/// <summary>/// labelme转yolo格式数据,要转换的目录/// </summary>/// <param name="inputDir"></param>/// 2025-3-4 00:45:38,public void FileConvert(string inputDir){//labels.Clear();var files = System.IO.Directory.GetFiles(inputDir, "*.json");if (files == null || files.Length == 0){return;}//获取目录中文件夹,以数字升序命名最新的训练文件夹var folders = Directory.GetDirectories(outDir);int lastNum = 1;if (folders != null && folders.Length > 0){List<DirectoryInfo> dirList = new List<DirectoryInfo>();foreach (var item in folders){DirectoryInfo directoryInfo = new DirectoryInfo(item);dirList.Add(directoryInfo);}var lastDir = dirList.OrderByDescending(g => g.CreationTime).FirstOrDefault();//获取文件夹后缀var lastNumStr = Regex.Match(lastDir.Name, "\\d*$").Value;lastNum = Convert.ToInt32(lastNumStr) + 1;}//此次训练保存的文件夹string newTrainDir = "train" + lastNum;//string dirNew = "train" + Guid.NewGuid().ToString("n");string dirNew = outDir + "\\" + newTrainDir + "\\train";//处理json文件foreach (var item in files){//处理单个json文件SingleFileConvert(item, dirNew);}//挑选训练图片前两个图片作为验证集//创建val文件夹string val = Path.Combine(outDir, newTrainDir, "val");                      var files21 = System.IO.Directory.GetFiles(inputDir, "*.jpg");var files22 = System.IO.Directory.GetFiles(inputDir, "*.png");var fileContainer = files21.Union(files22).ToList();var first1 = fileContainer[0];var first2 = fileContainer[1];//将训练图片全部复制到train中var trainImgNew = Path.Combine(dirNew, "images");if (!Directory.Exists(trainImgNew)){Directory.CreateDirectory(trainImgNew);}foreach (var itemImg in fileContainer){string fileNew2  = Path.Combine(trainImgNew, Path.GetFileName(itemImg));File.Copy(itemImg, fileNew2);}//只有2个样本时,将训练的文件夹作为验证if (files.Length <= 2){val = dirNew;goto Val_done;}if (!Directory.Exists(val)){Directory.CreateDirectory(val);}//图片文件夹string valImage = Path.Combine(val, "images");if (!Directory.Exists(valImage)){Directory.CreateDirectory(valImage);}string file1Name = Path.GetFileName(first1);string file2Name = Path.GetFileName(first2);string img1 = Path.Combine(valImage, file1Name);File.Copy(first1, img1);string img2 = Path.Combine(valImage, file2Name);File.Copy(first2, img2);//坐标数据string vallabels = Path.Combine(val, "labels");if (!Directory.Exists(vallabels)){Directory.CreateDirectory(vallabels);}//找到train目录中的坐标数据string vallabels2 = Path.Combine(dirNew, "labels");string txt1 = Path.GetFileNameWithoutExtension(file1Name) + ".txt";string txt2 = Path.GetFileNameWithoutExtension(file2Name) + ".txt";string fileLabel1 = Path.Combine(vallabels2, txt1);string fileLabel2 = Path.Combine(vallabels2, txt2);if (File.Exists(fileLabel1)){string fileLabelTxt1 = Path.Combine(vallabels, txt1);File.Copy(fileLabel1, fileLabelTxt1);}if (File.Exists(fileLabel2)){string fileLabelTxt2 = Path.Combine(vallabels, txt2);File.Copy(fileLabel2, fileLabelTxt2);}Val_done://生成data.yamlstring dir = dirNew;//Path.Combine(outDir, newTrainDir);dir = dir.Replace("\\", "/");val = val.Replace("\\", "/");StringBuilder sb = new StringBuilder();sb.AppendLine("train: " + dir);sb.AppendLine("val: " + val);sb.AppendLine("test: " + val);sb.AppendLine("");sb.AppendLine("nc: " + labels.Count);//设置编码支持中文string labelName = System.Text.Json.JsonSerializer.Serialize(labels.Keys,new System.Text.Json.JsonSerializerOptions(){Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)});labelName = labelName.Replace("\"", "'");//替换双引号为单引号sb.AppendLine("names: " + labelName);//保存//var arr = inputDir.Split("\\");//var lastDir = arr.Last();//string newDir = inputDir.TrimEnd(lastDir.ToArray());//string filePath = Path.Combine(newDir, "yoloTrainDataSet", "data.yaml");string yamlDir = outDir + "\\" + newTrainDir;string filePath = Path.Combine(yamlDir, "data.yaml");System.IO.File.WriteAllText(filePath, sb.ToString());}/// <summary>/// 单个json文件转换/// </summary>public void SingleFileConvert(string file, string inputDir){string json = System.IO.File.ReadAllText(file);var data2 = System.Text.Json.JsonSerializer.Deserialize<LabelMeView>(json);if (data2 == null || data2.shapes == null || data2.shapes.Count == 0){return;}StringBuilder stringBuilder = new StringBuilder();//读取每个形状foreach (var item in data2.shapes){//忽略没名称的形状if (string.IsNullOrEmpty(item.label)){continue;}labels.TryAdd(item.label, 1);double x1 = 0, x2 = 0, y1 = 0, y2 = 0;if (item.shape_type == "rectangle"){x1 = item.points[0][0];y1 = item.points[0][1];x2 = item.points[1][0];y2 = item.points[1][1];}else if (item.shape_type == "polygon"){x1 = item.points.Select(g => g[0]).Min();y1 = item.points.Select(g => g[1]).Min();x2 = item.points.Select(g => g[0]).Max();y2 = item.points.Select(g => g[1]).Max();}//中心点double x_center = (x1 + x2) / (double)2 / data2.imageWidth;double y_center = (y1 + y2) / (double)2 / data2.imageHeight;double width = Math.Abs((x2 - x1) / data2.imageWidth);double height = Math.Abs((y2 - y1) / data2.imageHeight);//获取对象idint classId = labels.Keys.ToList().IndexOf(item.label);stringBuilder.AppendLine($"{classId} {x_center} {y_center} {width} {height}");}//保存txt文件//var arr = inputDir.Split("\\");//var lastDir = arr.Last();//string newDir = inputDir.TrimEnd(lastDir.ToArray());// string dir = System.IO.Path.Combine( outDir, "labels");string dir = System.IO.Path.Combine(inputDir, "labels");if (!System.IO.Directory.Exists(dir)){System.IO.Directory.CreateDirectory(dir);}//string fileName = Path.GetFileName(file) + "_" + Guid.NewGuid().ToString("n") + ".txt";string fileName = Path.GetFileNameWithoutExtension(file) + ".txt";string filePath = System.IO.Path.Combine(dir, fileName);File.WriteAllText(filePath, stringBuilder.ToString());}}public class LabelMeView{public string version { get; set; }public List<ShapeView> shapes { get; set; }public string imagePath { get; set; }/// <summary>/// 图片高度,px/// </summary>public int imageHeight { get; set; }/// <summary>/// 图片宽度,px/// </summary>public int imageWidth { get; set; }}public class ShapeView{/// <summary>/// 对象说明,标签/// </summary>public string label { get; set; }/// <summary>/// 形状描述;rectangle;polygon/// </summary>public string shape_type { get; set; }public object flags { get; set; }/// <summary>/// 坐标点/// </summary>public List<double[]> points { get; set; }public string version { get; set; }}}

http://www.ppmy.cn/ops/167094.html

相关文章

【大语言模型_5】xinference部署embedding模型和rerank模型

一、安装xinference pip install xinference 二、启动xinference ./xinference-local --host0.0.0.0 --port5544 三、注册本地模型 1、注册embedding模型 curl -X POST "http://localhost:5544/v1/models" \ -H "Content-Type: application/json" \…

单片机自学总结

自从工作以来&#xff0c;一直努力耕耘单片机&#xff0c;至今&#xff0c;颇有收获。从51单片机&#xff0c;PIC单片机&#xff0c;直到STM32&#xff0c;以及RTOS和Linux&#xff0c;几乎天天在搞:51单片机&#xff0c;STM8S207单片机&#xff0c;PY32F003单片机&#xff0c;…

微信小程序:修改提示信息placeholder颜色

方法一&#xff1a;使用 placeholder-style 直接在 input 或 textarea 组件中使用 placeholder-style 属性来设置 placeholder 的样式。 <input placeholder"请输入内容" placeholder-style"color: #999; font-size: 14px;" /> 或者&#xff1a; …

计算机网络--访问一个网页的全过程

文章目录 访问一个网页的全过程应用层在浏览器输入URL网址http://www.aspxfans.com:8080/news/index.aspboardID5&ID24618&page1#r_70732423通过DNS获取IP地址生成HTTP请求报文应用层最后 传输层传输层处理应用层报文建立TCP连接传输层最后 网络层网络层对TCP报文进行处…

Ollama 0.4 发布!支持 Llama 3.2 Vision,实现多模态 RAG

“ 阅读本文大概需要5分钟。 前言 最近&#xff0c;Ollama 推出了 0.4 版本&#xff0c;其中最大的亮点就是支持了 Llama 3.2 Vision 模型&#xff0c;该模型具备多模态特性&#xff0c;也就是说能够理解图像并将图像纳入提示词中进行处理&#xff0c;让模型更智能地处理RAG中…

hbuiderx的sass编译器报dart-sass等错误的解决方法

HBuilderX 4.5起&#xff0c;vue2的sass编译器由node-sass改为dart-sass。node-sass是已经被淘汰的不再维护的库&#xff0c;且不支持arm cpu。 node-sass有些过期语法在dart-sass上报错导致无法编译。 虽然默认为dart-sass&#xff0c;但HBuilderX 4.56版也提供了选项&#xf…

K8S学习之基础三十四:K8S之监控Prometheus部署pod版

使用 Kubernetes Pod 的方式部署 Prometheus 是一种常见的方法&#xff0c;尤其是在容器化和微服务架构中。以下是详细的步骤&#xff1a; 1. 创建命名空间&#xff08;可选&#xff09; 为了方便管理&#xff0c;可以为 Prometheus 创建一个单独的命名空间。 yaml 复制 a…

Spark 中agg的用法

在 Spark 中&#xff0c;agg 是用于对 DataFrame 进行聚合操作的函数。它可以同时对多个列应用多个聚合函数&#xff0c;并返回一个新的 DataFrame。agg 通常与 groupBy 结合使用&#xff0c;用于对分组后的数据进行聚合操作。 以下是 agg 的详细用法和示例。 1. agg 的基本用…