Halcon DeepLearning Tool工具是一个非常好用的目标检测标注工具,但是其生成的hdict文件无法直接用于其他深度学习工具,so本文提供了一种方法可以将hdict先转为TXT数据格式。
方法如下:
1.新建一个C#项目Hdict2Txt,创建一个窗体添加两个按钮控件,添加Form1_Load事件、button1_Click和button2_Click点击事件
2.复制下面代码,然后点运行就可以了
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using HalconDotNet;
namespace Hdict2Txt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private HTuple HDict = new HTuple();
private DictDatas dictDatas = new DictDatas();
private HTuple hv_DictFile = @"data\train.hdict";
private string saveInfoPath = @"data\labels.txt";
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "选择文件";
button2.Text = "转为TXT";
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofg = new OpenFileDialog() { Filter = "Halcon字典文件(*.hdict)|*.hdict" })
{
if (ofg.ShowDialog() == DialogResult.OK)
{
hv_DictFile = ofg.FileName;
saveInfoPath = hv_DictFile + ".txt";
HOperatorSet.ReadDict(hv_DictFile, new HTuple(), new HTuple(), out HDict);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
HOperatorSet.ReadDict(hv_DictFile, new HTuple(), new HTuple(), out HDict);
GetDictInfo();
GetLabels();
MessageBox.Show("处理完成,文件已保存到:"+Application.StartupPath+ "\\"+saveInfoPath);
}
private void GetDictInfo()
{
HTuple AllKeys = new HTuple();
HOperatorSet.GetDictParam(HDict, "keys", new HTuple(), out AllKeys);
if (AllKeys.Length != 0)
{
try
{
HOperatorSet.GetDictTuple(HDict, "class_ids", out this.dictDatas.class_ids);
HOperatorSet.GetDictTuple(HDict, "class_names", out this.dictDatas.class_names);
HOperatorSet.GetDictTuple(HDict, "samples", out this.dictDatas.samples);
HOperatorSet.GetDictTuple(HDict, "image_dir", out this.dictDatas.image_dir);
if (File.Exists(saveInfoPath))
{
File.Delete(saveInfoPath);
}
StringBuilder class_ids = new StringBuilder();
StringBuilder class_names = new StringBuilder();
class_ids.Append("class_ids:");
class_names.Append("class_names:");
for (int i = 0; i < this.dictDatas.class_ids.Length; i++)
{
class_ids.Append(this.dictDatas.class_ids.TupleSelect(i).ToString() + ";");
class_names.Append(this.dictDatas.class_names.TupleSelect(i).ToString() + ";");
}
string[] lines = new string[4];
lines[0] = class_ids.ToString();
lines[1] = class_names.ToString();
lines[2] = "image_dir:" + this.dictDatas.image_dir.ToString();
lines[3] = "samples:";
File.AppendAllLines(saveInfoPath, lines);
}
catch
{
MessageBox.Show("此文件格式不支持,无法正常读取!");
return;
}
}
else
{
MessageBox.Show("hdict文件为空!");
}
}
private void GetLabels()
{
HTuple selectsample = dictDatas.samples.TupleSelect(0);
HTuple AllKeys = new HTuple();
HOperatorSet.GetDictParam(selectsample, "keys", new HTuple(), out AllKeys);
if (AllKeys.Length == 7)
{
//image_id; image_file_name; bbox_label_id; bbox_row1; bbox_col1; bbox_row2; bbox_col2;
File.AppendAllText(saveInfoPath, "image_id;image_file_name;bbox_label_id;bbox_row1;bbox_col;bbox_row2;bbox_col2"+"\r\n");
for (int i = 0; i < dictDatas.samples.Length; i++)
{
selectsample = dictDatas.samples.TupleSelect(i);
HOperatorSet.GetDictParam(selectsample, "keys", new HTuple(), out AllKeys);
DictObjectItems doi = new DictObjectItems();
HOperatorSet.GetDictTuple(selectsample, "image_id", out doi.image_id);
HOperatorSet.GetDictTuple(selectsample, "image_file_name", out doi.image_file_name);
HOperatorSet.GetDictTuple(selectsample, "bbox_label_id", out doi.bbox_label_id);
HOperatorSet.GetDictTuple(selectsample, "bbox_row1", out doi.bbox_row1);
HOperatorSet.GetDictTuple(selectsample, "bbox_col1", out doi.bbox_col1);
HOperatorSet.GetDictTuple(selectsample, "bbox_row2", out doi.bbox_row2);
HOperatorSet.GetDictTuple(selectsample, "bbox_col2", out doi.bbox_col2);
string[] lines = new string[doi.bbox_label_id.Length];
for (int k = 0; k < doi.bbox_label_id.Length; k++)
{
lines[k] = doi.image_id + ";" + doi.image_file_name + ";" + doi.bbox_label_id.TupleSelect(k) + ";" + doi.bbox_row1.TupleSelect(k) + ";"
+ doi.bbox_col1.TupleSelect(k) + ";" + doi.bbox_row2.TupleSelect(k) + ";" + doi.bbox_col2.TupleSelect(k);
}
File.AppendAllLines(saveInfoPath, lines);
}
}
else if (AllKeys.Length == 8)
{
//['image_file_name', 'image_id', 'bbox_row', 'bbox_col', 'bbox_length1', 'bbox_length2', 'bbox_phi', 'bbox_label_id']
File.AppendAllText(saveInfoPath, "image_id;image_file_name;bbox_label_id;bbox_row;bbox_col;bbox_length1;bbox_length2;bbox_phi" + "\r\n");
for (int i = 0; i < dictDatas.samples.Length; i++)
{
selectsample = dictDatas.samples.TupleSelect(i);
HOperatorSet.GetDictParam(selectsample, "keys", new HTuple(), out AllKeys);
DictObjectItems doi = new DictObjectItems();
HOperatorSet.GetDictTuple(selectsample, "image_id", out doi.image_id);
HOperatorSet.GetDictTuple(selectsample, "image_file_name", out doi.image_file_name);
HOperatorSet.GetDictTuple(selectsample, "bbox_label_id", out doi.bbox_label_id);
HOperatorSet.GetDictTuple(selectsample, "bbox_row", out doi.bbox_row1);
HOperatorSet.GetDictTuple(selectsample, "bbox_col", out doi.bbox_col1);
HOperatorSet.GetDictTuple(selectsample, "bbox_length1", out doi.bbox_row2);
HOperatorSet.GetDictTuple(selectsample, "bbox_length2", out doi.bbox_col2);
HOperatorSet.GetDictTuple(selectsample, "bbox_phi", out doi.bbox_phi);
string[] lines = new string[doi.bbox_label_id.Length];
for (int k = 0; k < doi.bbox_label_id.Length; k++)
{
lines[k] = doi.image_id + ";" + doi.image_file_name + ";" + doi.bbox_label_id.TupleSelect(k) + ";" + doi.bbox_row1.TupleSelect(k) + ";"
+ doi.bbox_col1.TupleSelect(k) + ";" + doi.bbox_row2.TupleSelect(k) + ";" + doi.bbox_col2.TupleSelect(k) + ";" + doi.bbox_phi.TupleSelect(k);
}
File.AppendAllLines(saveInfoPath, lines);
}
}
else
{
MessageBox.Show("此文件格式不支持,无法正常读取!");
return;
}
}
}
public class DictDatas
{
//分类名
public HTuple class_names;
//分类ID
public HTuple class_ids;
//图片文件夹路径
public HTuple image_dir;
//图片标注信息,包括名称、标签、目标矩形框坐标等
public HTuple samples;
}
public class DictObjectItems
{
public HTuple image_id;
public HTuple image_file_name;
public HTuple bbox_label_id;
public HTuple bbox_row1;
public HTuple bbox_col1;
public HTuple bbox_row2;
public HTuple bbox_col2;
public HTuple bbox_phi;
}
}
注意Halcon DeepLearning Tool工具有两种标注方法,常用的就是轴平行矩形标注,它还有个高级的自由矩形标注方法,两种方法产生数据格式不一样
例如:Halcon例程中的screws.hdict,采用的自由矩形标注方法,转换为TXT后部分内容截图如下:
下面这个是一个手势识别的例子,采用轴平行矩形标注方法,转换为TXT后部分内容截图如下:
最后,对于生成这个TXT有啥用或怎么用的问题,各位自行研究,这是我自己写的,最后我转换成了YOLOv3的数据格式,用YOLOv3训练没有问题.
直接下载通道(源码):
https://download.csdn.net/download/xiangdehu/12068057