设置首页收藏本站
开启左侧

将Halcon DeepLearning Tool工具生成的hdict文件转为TXT文件

[复制链接]
Tigger 发表于 2019-12-30 18:49:27 | 显示全部楼层 |阅读模式
本帖最后由 Tigger 于 2020-1-1 15:21 编辑

Halcon DeepLearning Tool工具是一个非常好用的目标检测标注工具,但是其生成的hdict文件无法直接用于其他深度学习工具,so本文将提供一种解决方案可以将hdict先转为TXT数据格式。然后各位可以很直观的根据需要再转为其他深度学习工具数据格式。

方法如下:
1.新建一个C#项目Hdict2Txt,创建一个窗体添加两个按钮控件,添加Form1_Load事件、button1_Click和button2_Click点击事件
2.复制下面代码,然后点运行就可以转为TXT了
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using HalconDotNet;

  6. namespace Hdict2Txt
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.         private HTuple HDict = new HTuple();
  15.         private DictDatas dictDatas = new DictDatas();
  16.         private HTuple hv_DictFile = @"data\train.hdict";
  17.         private string saveInfoPath = @"data\labels.txt";
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.             button1.Text = "选择文件";
  21.             button2.Text = "转为TXT";
  22.         }
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             using (OpenFileDialog ofg = new OpenFileDialog() { Filter = "Halcon字典文件(*.hdict)|*.hdict" })
  26.             {
  27.                 if (ofg.ShowDialog() == DialogResult.OK)
  28.                 {
  29.                     hv_DictFile = ofg.FileName;
  30.                     saveInfoPath = hv_DictFile + ".txt";
  31.                     HOperatorSet.ReadDict(hv_DictFile, new HTuple(), new HTuple(), out HDict);
  32.                 }
  33.             }
  34.         }

  35.         private void button2_Click(object sender, EventArgs e)
  36.         {
  37.             HOperatorSet.ReadDict(hv_DictFile, new HTuple(), new HTuple(), out HDict);
  38.             GetDictInfo();
  39.             GetLabels();
  40.             MessageBox.Show("处理完成,文件已保存到:" + Application.StartupPath + "\" + saveInfoPath);
  41.         }
  42.         private void GetDictInfo()
  43.         {
  44.             HTuple AllKeys = new HTuple();
  45.             HOperatorSet.GetDictParam(HDict, "keys", new HTuple(), out AllKeys);
  46.             if (AllKeys.Length != 0)
  47.             {
  48.                 try
  49.                 {
  50.                     HOperatorSet.GetDictTuple(HDict, "class_ids", out this.dictDatas.class_ids);
  51.                     HOperatorSet.GetDictTuple(HDict, "class_names", out this.dictDatas.class_names);
  52.                     HOperatorSet.GetDictTuple(HDict, "samples", out this.dictDatas.samples);
  53.                     HOperatorSet.GetDictTuple(HDict, "image_dir", out this.dictDatas.image_dir);

  54.                     if (File.Exists(saveInfoPath))
  55.                     {
  56.                         File.Delete(saveInfoPath);
  57.                     }

  58.                     StringBuilder class_ids = new StringBuilder();
  59.                     StringBuilder class_names = new StringBuilder();
  60.                     class_ids.Append("class_ids:");
  61.                     class_names.Append("class_names:");
  62.                     for (int i = 0; i < this.dictDatas.class_ids.Length; i++)
  63.                     {
  64.                         class_ids.Append(this.dictDatas.class_ids.TupleSelect(i).ToString() + ";");
  65.                         class_names.Append(this.dictDatas.class_names.TupleSelect(i).ToString() + ";");
  66.                     }

  67.                     string[] lines = new string[4];
  68.                     lines[0] = class_ids.ToString();
  69.                     lines[1] = class_names.ToString();
  70.                     lines[2] = "image_dir:" + this.dictDatas.image_dir.ToString();
  71.                     lines[3] = "samples:";

  72.                     File.AppendAllLines(saveInfoPath, lines);
  73.                 }
  74.                 catch
  75.                 {
  76.                     MessageBox.Show("此文件格式不支持,无法正常读取!");
  77.                     return;
  78.                 }
  79.             }
  80.         }
  81.         private void GetLabels()
  82.         {
  83.             HTuple selectsample = dictDatas.samples.TupleSelect(0);
  84.             HTuple AllKeys = new HTuple();
  85.             HOperatorSet.GetDictParam(selectsample, "keys", new HTuple(), out AllKeys);
  86.             if (AllKeys.Length == 7)
  87.             {
  88.                 //image_id; image_file_name; bbox_label_id; bbox_row1; bbox_col1; bbox_row2; bbox_col2;
  89.                 File.AppendAllText(saveInfoPath, "image_id;image_file_name;bbox_label_id;bbox_row1;bbox_col;bbox_row2;bbox_col2" + "\r\n");
  90.                 for (int i = 0; i < dictDatas.samples.Length; i++)
  91.                 {
  92.                     selectsample = dictDatas.samples.TupleSelect(i);
  93.                     HOperatorSet.GetDictParam(selectsample, "keys", new HTuple(), out AllKeys);

  94.                     DictObjectItems doi = new DictObjectItems();
  95.                     HOperatorSet.GetDictTuple(selectsample, "image_id", out doi.image_id);
  96.                     HOperatorSet.GetDictTuple(selectsample, "image_file_name", out doi.image_file_name);
  97.                     HOperatorSet.GetDictTuple(selectsample, "bbox_label_id", out doi.bbox_label_id);
  98.                     HOperatorSet.GetDictTuple(selectsample, "bbox_row1", out doi.bbox_row1);
  99.                     HOperatorSet.GetDictTuple(selectsample, "bbox_col1", out doi.bbox_col1);
  100.                     HOperatorSet.GetDictTuple(selectsample, "bbox_row2", out doi.bbox_row2);
  101.                     HOperatorSet.GetDictTuple(selectsample, "bbox_col2", out doi.bbox_col2);

  102.                     string[] lines = new string[doi.bbox_label_id.Length];
  103.                     for (int k = 0; k < doi.bbox_label_id.Length; k++)
  104.                     {
  105.                         lines[k] = doi.image_id + ";" + doi.image_file_name + ";" + doi.bbox_label_id.TupleSelect(k) + ";" + doi.bbox_row1.TupleSelect(k) + ";"
  106.                             + doi.bbox_col1.TupleSelect(k) + ";" + doi.bbox_row2.TupleSelect(k) + ";" + doi.bbox_col2.TupleSelect(k);
  107.                     }
  108.                     File.AppendAllLines(saveInfoPath, lines);
  109.                 }
  110.             }
  111.             else if (AllKeys.Length == 8)
  112.             {
  113.                 //['image_file_name', 'image_id', 'bbox_row', 'bbox_col', 'bbox_length1', 'bbox_length2', 'bbox_phi', 'bbox_label_id']
  114.                 File.AppendAllText(saveInfoPath, "image_id;image_file_name;bbox_label_id;bbox_row;bbox_col;bbox_length1;bbox_length2;bbox_phi" + "\r\n");
  115.                 for (int i = 0; i < dictDatas.samples.Length; i++)
  116.                 {
  117.                     selectsample = dictDatas.samples.TupleSelect(i);
  118.                     HOperatorSet.GetDictParam(selectsample, "keys", new HTuple(), out AllKeys);

  119.                     DictObjectItems doi = new DictObjectItems();
  120.                     HOperatorSet.GetDictTuple(selectsample, "image_id", out doi.image_id);
  121.                     HOperatorSet.GetDictTuple(selectsample, "image_file_name", out doi.image_file_name);
  122.                     HOperatorSet.GetDictTuple(selectsample, "bbox_label_id", out doi.bbox_label_id);
  123.                     HOperatorSet.GetDictTuple(selectsample, "bbox_row", out doi.bbox_row1);
  124.                     HOperatorSet.GetDictTuple(selectsample, "bbox_col", out doi.bbox_col1);
  125.                     HOperatorSet.GetDictTuple(selectsample, "bbox_length1", out doi.bbox_row2);
  126.                     HOperatorSet.GetDictTuple(selectsample, "bbox_length2", out doi.bbox_col2);
  127.                     HOperatorSet.GetDictTuple(selectsample, "bbox_phi", out doi.bbox_phi);

  128.                     string[] lines = new string[doi.bbox_label_id.Length];
  129.                     for (int k = 0; k < doi.bbox_label_id.Length; k++)
  130.                     {
  131.                         lines[k] = doi.image_id + ";" + doi.image_file_name + ";" + doi.bbox_label_id.TupleSelect(k) + ";" + doi.bbox_row1.TupleSelect(k) + ";"
  132.                             + doi.bbox_col1.TupleSelect(k) + ";" + doi.bbox_row2.TupleSelect(k) + ";" + doi.bbox_col2.TupleSelect(k) + ";" + doi.bbox_phi.TupleSelect(k);
  133.                     }
  134.                     File.AppendAllLines(saveInfoPath, lines);
  135.                 }
  136.             }
  137.             else
  138.             {
  139.                 MessageBox.Show("此文件格式不支持,无法正常读取!");
  140.                 return;
  141.             }
  142.         }
  143.     }
  144.     public class DictDatas
  145.     {
  146.         //分类名
  147.         public HTuple class_names;
  148.         //分类ID
  149.         public HTuple class_ids;
  150.         //图片文件夹路径
  151.         public HTuple image_dir;
  152.         //图片标注信息,包括名称、标签、目标矩形框坐标等
  153.         public HTuple samples;
  154.     }
  155.     public class DictObjectItems
  156.     {
  157.         public HTuple image_id;
  158.         public HTuple image_file_name;
  159.         public HTuple bbox_label_id;
  160.         public HTuple bbox_row1;
  161.         public HTuple bbox_col1;
  162.         public HTuple bbox_row2;
  163.         public HTuple bbox_col2;
  164.         public HTuple bbox_phi;
  165.     }
  166. }
复制代码

注意Halcon DeepLearning Tool工具有两种标注方法,常用的就是轴平行矩形标注,它还有个高级的自由矩形标注方法,两种方法产生数据格式不一样
例如:Halcon例程中的screws.hdict,采用的自由矩形标注方法,转换为TXT后部分内容截图如下:
QQ截图20191230183042.png

下面这个是一个手势识别的例子,采用轴平行矩形标注方法,转换为TXT后部分内容截图如下:
QQ截图20191230183307.png

最后,对于生成这个TXT有啥用或怎么用的问题,各位自行研究,这是我自己写的,最后我转换成了YOLOv3的数据格式,用YOLOv3训练没有问题.
直接下载通道:
https://download.csdn.net/download/xiangdehu/12068057




奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
jash 发表于 2020-1-16 15:45:02 | 显示全部楼层
分享另一種寫法是在HALCON內將Dict轉為Tuple
AT:=[]
get_dict_param (DictHandle, 'keys', [] , AllKeys)
for Index := 0 to |AllKeys|-1 by 1
    get_dict_param (DictHandle, 'key_data_type', AllKeys[Index], data_type)
    if (data_type = 'tuple')
        get_dict_tuple (DictHandle, AllKeys[Index], Tuple)
        AT:=[AT,Tuple]
    endif
endfor
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
云翎storm 发表于 2021-9-15 16:06:47 | 显示全部楼层
请问大佬,为什么复制到吗后运行到读取的这一句就报错?
HOperatorSet.ReadDict(hv_DictFile, new HTuple(), new HTuple(), out HDict);
异常:System.BadImageFormatException:“试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)”
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
进击的烤肉 发表于 2022-3-27 21:29:29 | 显示全部楼层
还可以再转回去吗
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
momodog 发表于 2022-6-27 11:21:50 | 显示全部楼层
云翎storm 发表于 2021-9-15 16:06
请问大佬,为什么复制到吗后运行到读取的这一句就报错?
HOperatorSet.ReadDict(hv_DictFile, new HTuple() ...

我也遇到同样的问题,请问你是怎么解决的呀?
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
Lanren 发表于 2023-12-16 18:31:16 | 显示全部楼层
momodog 发表于 2022-6-27 11:21
我也遇到同样的问题,请问你是怎么解决的呀?

俺也一样,请问有解决办法了吗
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表