目标检测——GDXray数据集转为YOLO格式

embedded/2024/9/22 18:35:04/

关于该数据集的介绍可以看我写的另一篇博客:链接

论文题目:《GDXray: The Database of X-ray Images for Nondestructive Testing》
论文链接:https://link.springer.com/article/10.1007/s10921-015-0315-7

Github链接  https://github.com/computervision-xray-testing/GDXray/blob/main/README.md

以GDXray的Baggages为例,其原数据集下载下来如下所示:

 

 其中每个文件夹里都有ground_truth.txt,即坐标框相关的信息,ground_truth.txt内容如下:

 知道了每个数字对应的含义,接下来的任务就简单了,因此只需要针对相同的ID创建同样的TXT的Label,并且对应图片的ID名即可,参考代码如下:
注:该代码仅是针对一组图片的,所有的图片都需转换的话再套一个循环即可!

import osdef map_number_to_filename(dir_name, number):"""将数字映射为特定的文件名格式。参数:- number: 一个浮点数,表示映射的键。返回:- str: 按照给定格式映射后的文件名。"""# 将浮点数转换为字符串,并去掉不必要的小数点和之后的所有0number_str = format(number, '.0f')# 将字符串格式化为至少4位的数字,不足部分前面补0number_str_padded = number_str.zfill(4)# 构造文件名img_name = f"{dir_name}_{number_str_padded}.png"label_name = f"{dir_name}_{number_str_padded}.txt"return label_name, img_nameimport cv2def get_image_dimensions(image_path, pos_list):"""使用OpenCV获取指定图片文件的宽度和高度。参数:- image_path: 图片文件的路径。返回:- (width, height): 图片的宽度和高度的元组。"""templist = []# 使用cv2.imread读取图片,0表示以原始格式读取image = cv2.imread(image_path, 0)# 检查图片是否正确读取if image is None:raise FileNotFoundError(f"The image at path {image_path} could not be found or is not a valid image file.")# 图片的尺寸可以通过image.shape属性获取,它返回一个三元组(height, width, channels)height, width = image.shape[:2]b_w = float(pos_list[1]) - float(pos_list[0])b_h = float(pos_list[3]) - float(pos_list[2])b_x = (float(pos_list[0]) + float(pos_list[1]))/2.0b_y = (float(pos_list[2]) + float(pos_list[3]))/2.0x = float(b_x)/widthy = float(b_y)/heightw = float(b_w)/widthh = float(b_h)/heighttemplist.append(x)templist.append(y)templist.append(w)templist.append(h)return templistif __name__ == "__main__":dir_path = r"F:\BaiduNetdiskDownload\DataSet\GDXray\Baggages\Baggages\B0001"dir_basename = os.path.basename(dir_path)ground_truth_txt = os.path.join(dir_path,'ground_truth.txt')save_txt_dir = os.path.join(os.path.dirname(dir_path), f"YOLO_{dir_basename}")if not os.path.exists(save_txt_dir):os.makedirs(save_txt_dir)f = open(ground_truth_txt,'r',encoding='utf-8')contents = f.readlines()for content in contents:content = content.strip().split("   ")filename, img_name = map_number_to_filename(dir_name=dir_basename, number=float(content[0]))img_path = os.path.join(dir_path, img_name)yolo_pos_list = get_image_dimensions(img_path, pos_list=content[1:])save_filename = os.path.join(save_txt_dir,filename)f_s = open(save_filename,'a',encoding='utf-8')f_s.write("0 ")f_s.write(str(yolo_pos_list[0]))f_s.write(" ")f_s.write(str(yolo_pos_list[1]))f_s.write(" ")f_s.write(str(yolo_pos_list[2]))f_s.write(" ")f_s.write(str(yolo_pos_list[3]))

拿一组数据集来进行实验,B0001文件夹

运行代码得到: 

进行可视化验证,相关代码如下:链接

 

 

针对于它的类别暂时不知道写什么,但是框的位置没问题了,类别后期确定了根据做个映射就行,完美!!! 


http://www.ppmy.cn/embedded/92862.html

相关文章

14. 最长公共前缀【 力扣(LeetCode) 】

一、题目描述 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 二、测试用例 示例 1: 输入:strs ["flower","flow","flight"] 输出:"fl"示…

Topaz Video AI——视频修复

一、Topaz Video AI 介绍及使用 Topaz Video AI 是一款基于人工智能的视频增强和修复软件,主要用于提升视频质量、去噪、插帧和分辨率提升。它利用深度学习技术对视频进行智能化处理,使得视频看起来更加清晰和流畅。Topaz Video AI 特别适合那些需要修复…

Redis相关

一、BitMap 1. SETBIT对key存储的字符串值,设置或清除指定偏移量的位,位的设置和清除取决于value参数,可以是0或者1,如果key不存在,自动创建一个新的字符串值,offset参数必须大于或等于0,小于2…

使用Spring与JDK动态代理实现事务管理

使用Spring与JDK动态代理实现事务管理 在现代企业级应用开发中,事务管理是一项关键的技术,它可以保证一系列操作要么全部成功,要么全部失败,从而确保数据的一致性和完整性。Spring框架提供了强大的事务管理能力,但有时…

24 - clearerr()函数

文章目录 1 函数原型2 参数3 返回值 1 函数原型 clearerr()函数:清除指定流stream的错误指示符,函数原型如下: void clearerr ( FILE * stream );cstdio库描述如下: Clear error indicators 1. Resets both the error and the …

YOLOv8进 | 检测头 | 小目标遮挡物性能提升的检测头Detect_MultiSEAM【完整代码】

秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 💡💡💡本专栏所有程序均经过测试,可成功执行💡💡💡 专栏目录 :《YOLOv8改进有效…

OSPF 命令 Default-router-advertise 之 always 选项解析

1、关于 default-route-advertise 命令 Ospf 是可以通过 import-route 命令引入外部路由的,但很少有人会注意到,在默认情况下,ospf 是不会引入来自外部路由的缺省路由的。 但 ospf 有一个变通的方法,就是通过 default-route-adv…

AI+ 如何重塑技术生产力

在21世纪的科技浪潮中,人工智能(AI)作为一股不可忽视的力量,正以前所未有的速度渗透并重塑着各行各业的生产力格局。从智能制造到智慧农业,从金融服务到医疗健康,AI技术的应用不仅极大地提升了生产效率&…