分割具有yolo格式的图片的ground truth

# -*- coding: utf-8 -*-
"""
@Time : 2022/5/6 16:06

@File :divided_sub_txt.py
@IDE :PyCharm
@Motto:no bug!
分割图像
1.将txt文件中的标签和坐标改成对于图像的xy坐标
2.裁切图像,并另存为对应的文件名


存在问题:
1. 空txt
2. 不知道为什么会报错的一些图片
"""

import os
from PIL import Image
import cv2


# 获取图片宽高
def get_image_width_high(imgname):
    image = cv2.imread(imgname)
    image_width, image_high = image.shape[0], image.shape[1]
    return image_width, image_high


# 坐标转化
def convert_line(line, image_width, image_high):
    array = line.split()
    # opencv中的坐标是高 宽
    x_label_min = (float(array[1]) - float(array[3]) / 2) * image_high
    x_label_max = (float(array[1]) + float(array[3]) / 2) * image_high
    y_label_min = (float(array[2]) - float(array[4]) / 2) * image_width
    y_label_max = (float(array[2]) + float(array[4]) / 2) * image_width
    bbox = (int(x_label_min), int(y_label_min), int(x_label_max), int(y_label_max))
    return bbox




imgdir = r'xxx'
txtdir = r'xxx'
savedir = r'xxx'
imgs = os.listdir(imgdir)
txts = os.listdir(txtdir)

for img in imgs:
# img = '16915183.jpg'
    img1 = Image.open(os.path.join(imgdir, img))
    txt = img[:-4] + '.txt'
    f = open(os.path.join(txtdir, txt), 'r')
    lines = f.readlines()
    f.close()
    new_lines = []
    image_width, image_high = get_image_width_high(os.path.join(imgdir, img))
    if len(lines) > 1:
        for line in lines:
            new_lines.append(line[:-1])
        for i in range(len(lines)):
            crop_box = convert_line(new_lines[i], image_width, image_high)
            img2 = img1.crop(crop_box)
            imgname = img[:-4] + '_' + str(i) + '.jpg'
            try:
                img2.save(os.path.join(savedir, imgname))
            except:
                print(img,'error')
    elif len(lines) == 1:
        try:
            line = lines[0]
            if "\n" in line:
                line = line[:-1]
            else:
                pass
            crop_box = convert_line(line, image_width, image_high)
            # print(crop_box)
            img2 = img1.crop(crop_box)
            imgname = img
            img2.save(os.path.join(savedir, imgname))
        except:
            print(img)
            print(crop_box)
    else:
        print(img,'txt文件为空')

如果图片报错的话,或者只想有一张图片的话

import os
from PIL import Image
import cv2

# 获取图片宽高
def get_image_width_high(imgname):
    image = cv2.imread(imgname)
    image_width, image_high = image.shape[0], image.shape[1]
    return image_width, image_high


# 坐标转化
def convert_line(line, image_width, image_high):
    array = line.split()
    # opencv中的坐标是高 宽
    x_label_min = (float(array[1]) - float(array[3]) / 2) * image_high
    x_label_max = (float(array[1]) + float(array[3]) / 2) * image_high
    y_label_min = (float(array[2]) - float(array[4]) / 2) * image_width
    y_label_max = (float(array[2]) + float(array[4]) / 2) * image_width
    bbox = (int(x_label_min), int(y_label_min), int(x_label_max), int(y_label_max))
    return bbox
img = '16915183.jpg'
img1 = Image.open(os.path.join(imgdir, img))
txt = img[:-4] + '.txt'
f = open(os.path.join(txtdir, txt), 'r')
lines = f.readlines()
f.close()
new_lines = []
image_width, image_high = get_image_width_high(os.path.join(imgdir, img))
if len(lines) > 1:
    for line in lines:
        new_lines.append(line[:-2])
    for i in range(len(lines)):
        crop_box = convert_line(new_lines[i], image_width, image_high)
        img2 = img1.crop(crop_box)
        imgname = img[:-4] + '_' + str(i) + '.jpg'
        img2.save(os.path.join(savedir, imgname))
elif len(lines) == 1:
    try:
        line = lines[0]
        if "\n" in line:
            line = line[:-1]
        else:
            pass
        crop_box = convert_line(line, image_width, image_high)
        # print(crop_box)
        img2 = img1.crop(crop_box)
        imgname = img
        img2.save(os.path.join(savedir, imgname))
    except:
        print(img)
        print(crop_box)
else:
    print(img, 'txt文件为空')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值