参考DLT标注导出来的数据集格式,用halcon代码的字典格式来保存即可。
首先需要对labelme标注出来的json文件进行转换,转换生成label标签图,然后在halcon里将标签图转换成与Halcon标签格式的8位图,每一类的标签的像素值从1开始,第一类像素就为1,第二类像素就为2。
完成标签图转换后,参考DLT的数据集格式,生成字典,将数据集路径等等参数插入进去。
最后保存字典即可。 最后使用DLT工具读取数据集,验证即可。
- * 存放图片的文件夹路径
- imageDir:='train'
- * 存放上边python生成的label文件夹路径 注:这里本人先用python对json数据集转换成16位的标签图,然后在halcon里将16位图转8位
- LabelDir:='模板_labels'
- * 存放类别的classes.txt路径 里面存放你的类别
- classFile:='classes.txt'
- * 生成的halcon训练所需的hdict
- DataList:='dataset.hdict'
- list_image_files (LabelDir, 'default', ['recursive'], ImageFiles)
- for Index := 0 to |ImageFiles|-1 by 1
- read_image (Image, ImageFiles[Index])
- 注:这部分代码 将16位的标签图转成8位标签图,符合Halcon的数据格式
- convert_image_type(Image,Image,'byte') // 0-127
- parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
- write_image (Image, 'png', 0, Directory+BaseName + '.' + 'png')
- endfor
- dlt_read_classnames ('classes.txt', ClassNames)
- tuple_length (ClassNames, Length)
- create_dict (tempImgDist)
- set_dict_tuple (tempImgDist, 'class_ids', [0:Length-1])
- set_dict_tuple (tempImgDist, 'class_names', ClassNames)
- set_dict_tuple (tempImgDist, 'image_dir', imageDir)
- set_dict_tuple (tempImgDist, 'segmentation_dir', LabelDir)
- list_image_files (imageDir, 'default', ['recursive'], ImageFiles)
- nei:=[]
- wai:=[]
- for Index := 0 to |ImageFiles|-1 by 1
- create_dict (image)
- parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
- indexx:=Index+1
- image_file_name:=BaseName + '.' + 'bmp'
- nei:=['image_id:'+' '+indexx, 'image_file_name:'+image_file_name,'segmentation_file_name:'+BaseName + '.' + 'png']
- set_dict_tuple (image, 'image_id', Index+1)
- set_dict_tuple (image, 'image_file_name', BaseName + '.' + 'bmp')
- set_dict_tuple (image, 'segmentation_file_name', './'+BaseName + '.' + 'png')
- wai:=[wai,image]
- endfor
- set_dict_tuple (tempImgDist, 'samples', wai)
- write_dict (tempImgDist, DataList, [], [])
- read_dict (DataList, [], [], DLDataset)
- stop ()
复制代码 |