欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python对txt文件里内容进行批量risize功能

程序员文章站 2022-04-17 15:53:37
...

  因为项目的原因,需要对数据集的图片和相对应的txt文件进行resize。因为txt文档里的坐标进行了归一化处理,所以大致思路是对横纵坐标分别乘以size,即320和240。代码如下:

# coding: utf-8
import os
import linecache
import random
txt_path_base='/home/ue/桌面/标签及类别说明/labels'
file_names=os.listdir(txt_path_base)
file_names.sort(key=lambda x:int(x[:-4]))
file_ob_list=[]

def split(full_list,ratio=0.2):
    length=len(full_list)
    offset=int(ratio*length)
    random.shuffle(full_list)
    sub1=full_list[:offset]
    sub2=full_list[offset:]
    return sub1,sub2



for file_name in file_names:
    fileob = txt_path_base + '/' + file_name
    file_ob_list.append(fileob)

total_classes={}
size=len(file_ob_list)

f1=open('/home/ue/桌面/test.txt',"w+")
f2=open('/home/ue/桌面/valid.txt',"w+")

list_line=0
for file_ob in file_ob_list:
    data = []
    if list_line>=size:break
    f = open('/home/ue/桌面/txtoutdir/' + file_names[list_line], "w+")
    init_line = 1
    total_line = len(open(file_ob_list[list_line]).readlines())
    while init_line<=total_line:
        line = linecache.getline(file_ob, init_line)
        line=line.strip()
        print(line)

        data.append(line)
        data_split=data[init_line-1].split(' ')
        init_line += 1
        data_split[0]=int(data_split[0])+1

        if data_split[0]>12:
            data_split[0]=12

        number=data_split[0]
        if number not in total_classes:
            total_classes[number]=[]
        total_classes[number].append(file_names[list_line])


        for i in range(1,5):
            data_split[i]=float(data_split[i])
            print(data_split[i])
        data_split[1]=data_split[1]*320
        data_split[3]=data_split[3]*320
        data_split[2]=data_split[2]*240
        data_split[4]=data_split[4]*240
        for i in range(1,5):
            print(data_split[i])
        dataline=""
        for i in range(4):
            dataline+=str(data_split[i])
            dataline+=" "
        dataline+=str(data_split[4])
        print(dataline)
        f.write(dataline)
        f.write('\n')


    list_line+=1

for k in total_classes.keys():
    print(k)
    sub1,sub2=split(total_classes[k])
    random.shuffle(sub1)
    random.shuffle(sub2)
    len1=len(sub1)
    len2=len(sub2)
    print(len1,len2)
    for i in range(len2):
        f2.write(sub2[i])
        f2.write('\n')
    for i in range(len1):
        f1.write(sub1[i])
        f1.write('\n')