YOLOv5 txt标签转图像标签(多个标签)

news/2024/7/11 0:07:09 标签: YOLO, 计算机视觉, opencv

Python YOLOv5 txt标签转图像标签(多个标签

  • txt的数据如图所示
    • 1.读原始图像以及对应的txt文件
    • 2.获得原始图像的大小
    • 3.生成一张大小相同,黑色背景的图片
    • 4.读取txt文件,循环的增加标签
    • 5.获得不规则图形(标签)
    • 6.完整代码
    • 7.示例

txt的数据如图所示

在这里插入图片描述
这里每一行是一个标签,原始的图片如图所示
在这里插入图片描述
这里有五个龋齿

1.读原始图像以及对应的txt文件

我这里图像和txt的名称是相同的

def init_func():
    # txt文件夹操作
    folder_type = 'train'
    # folder_type = 'val'
    img_dir = '../teeth_data/' + folder_type + '/image'
    txt_dir = '../teeth_data/' + folder_type + '/txt'
    save_dir = '../teeth_data/' + folder_type + '/mask'
    files = os.listdir(img_dir)

    for file in files:
        name = file[0:-4]
        img_path = img_dir + '/' + name + '.png'
        txt_path = txt_dir + '/' + name + '.txt'

这样我就能得到对应的图像和txt文件了,然后我需要获得原始图像的大小

2.获得原始图像的大小

img = cv2.imread(img_path)  # 读取图片信息
img_x = img.shape[0]
img_y = img.shape[1]

3.生成一张大小相同,黑色背景的图片

img_save = np.zeros((img_x, img_y, 1))  # 黑色背景

4.读取txt文件,循环的增加标签

# 打开文件
file = open(txt_path, "r")
# 逐行读取文件内容
for line in file:
    data = txt2mask_new(img_x, img_y, line) # 获得不规则图形

    color = 225
    cv2.fillPoly(img_save,  # 原图画板
                 [data],  # 多边形的点
                 color=color)
save_path = save_dir + '/' + name + '.png'
cv2.imwrite(save_path, img_save)

5.获得不规则图形(标签)

def txt2mask_new(img_x, img_y, line):
    # 处理每一行的内容
    data = line.split('\n')[0]
    d = data.split(' ', -1)
    # d[-1] = d[-1][0:-1]
    data = []
    for i in range(1, int(len(d) / 2) + 1):
        data.append([img_y * float(d[2 * i - 1]), img_x * float(d[2 * i])])
    data.append(data[0])
    data = np.array(data, dtype=np.int32)

    return data

这样就能实现所有功能啦!完整的代码如下:

6.完整代码

def txt2mask_new(img_x, img_y, line):
    # 处理每一行的内容
    data = line.split('\n')[0]
    d = data.split(' ', -1)
    # d[-1] = d[-1][0:-1]
    data = []
    for i in range(1, int(len(d) / 2) + 1):
        data.append([img_y * float(d[2 * i - 1]), img_x * float(d[2 * i])])
    data.append(data[0])
    data = np.array(data, dtype=np.int32)

return data


def init_func():
    # txt文件夹操作
    folder_type = 'train'
    # folder_type = 'val'
    img_dir = '../teeth_data/' + folder_type + '/image'
    txt_dir = '../teeth_data/' + folder_type + '/txt'
    save_dir = '../teeth_data/' + folder_type + '/mask'
    files = os.listdir(img_dir)

 for file in files:
        name = file[0:-4]
        img_path = img_dir + '/' + name + '.png'
        txt_path = txt_dir + '/' + name + '.txt'

 

   img = cv2.imread(img_path)  # 读取图片信息
    img_x = img.shape[0]
    img_y = img.shape[1]

img_save = np.zeros((img_x, img_y, 1))  # 黑色背景

# 打开文件
        file = open(txt_path, "r")
        # 逐行读取文件内容
        for line in file:
            data = txt2mask_new(img_x, img_y, line)

            color = 225
            cv2.fillPoly(img_save,  # 原图画板
                         [data],  # 多边形的点
                         color=color)
        save_path = save_dir + '/' + name + '.png'
        cv2.imwrite(save_path, img_save)

        # 关闭文件
        file.close()


if __name__ == '__main__':
    init_func()

最终输出的图像如下:

7.示例

在这里插入图片描述


http://www.niftyadmin.cn/n/281864.html

相关文章

存储资源调优技术——智能缓存分区

SmartPratition智能缓存分区 基本概念 本质上就是一种Cache分区技术 通过对系统核心资源的分区(隔离不同业务所需要的缓存资源),保证关键应用的性能 工作原理 用户可以以LUN或文件系统为单位设置SmartPartition分区 每个SmartPartition分区的…

HTML5 新增元素

目录 一、页面布局二、新增表单元素三、output元素四、address五、progress六、meter七、figure 和 figcaption八、fieldset 和 legend九、script十、bdi十一、details和summary十二、dialog十三、mark十四、time十五、datalist十六、新的输入属性 一、页面布局 标签位置heade…

Vue3 +TypeScript 引入 BabylonJs(Vue3实现3D)【一篇文章精通系列】

本文主要介绍如何使用Vue3和TypeScript引入BabylonJs技术实现3D效果。结合实际案例,详细讲解了如何在Vue3项目中引入BabylonJs,并了解其相关知识。通过本文的学习,相信读者可以轻松掌握Vue3实现3D效果以及BabylonJs的相关知识。 Vue3 TypeS…

Redux工作流程,reducer优化,react-redux

Redux 一、redux的基本配置 下载redux的依赖包基本redux入口文件创建和配置 由于redux不像vuex,我们必须包括文件的创建,基本入口文件都是需要自己创建 1)在src创建一个名称为redux或者store文件 2)在store文件夹下创建入口文件…

什么是Java中的泛型?

Java中的泛型(Generics)是一种在编译时期进行类型检查和类型推断的机制,它可以让程序员在定义类、接口和方法时使用一个或多个类型参数来代表不确定的类型。通过使用泛型,我们可以写出更加通用、类型安全、可重用的代码。 使用泛…

Fastapi交互式文档实现方法讲解

Fastapi交互式文档实现方法讲解 # applications.py class FastAPI(Starlette):def __init__(self,*,debug: bool False,routes: List[BaseRoute] None,title: str "FastAPI",description: str "",version: str "0.1.0",openapi_url: Optio…

debian11快速 ceph集群17.2.6(Quincy版)

由于网友跟我讲Pacific版快到期了,所以出一个Quincy版的部署文档 配置一下源 echo "deb http://mirrors.163.com/ceph/debian-quincy/ bullseye main" > /etc/apt/sources.list.d/ceph.list还是像以前一样使用docker或者podman 安装工具cephadm ce…

【五一创作】【笔记】Git|如何将仓库中所有的 commit 合成一个?又名,如何清除所有 git 提交记录?(附 git rebase 机制的简要分析)

在对代码进行开源时,我们往往并不希望代码开发过程中的提交记录被其他人看到,因为提交的过程中往往会涵盖一些敏感信息。因此会存在 将仓库中所有 commit 合成一个 的需求。 直觉上,往往会用 rebase 和 squash 或 reset,不过我尝…