Python实现keep_ratio批量缩放图片和label尺寸

import os
import cv2
from PIL import Image

#image和label原路径和缩放后的保存路径
label_path="/home/yangguide/Downloads/training/label_2"
new_label_path="/home/yangguide/Downloads/training/label"
image_path="/home/yangguide/Downloads/training/image_2"
new_image_path="/home/yangguide/Downloads/training/image"

for root,dirs,files in os.walk(label_path):
    for name in files:
         if name.endswith(".txt"):
            filename=root+"/"+name     #原label里.txt文件全路径名
            filename2=new_label_path+"/"+name  #缩放后的label里.txt文件全路径名
            
            file_name = name.split('.')[0]  
            file_path=image_path+"/"+file_name+".jpg"
            img = Image.open(file_path)
            weight = img.size[0]
            height = img.size[1]           
            
            target_size=[320,480]   #打算缩放的尺寸
            img = cv2.imread(file_path)
            old_size= img.shape[0:2]
            ratio = min(float(target_size[i])/(old_size[i]) for i in range(len(old_size)))

            new_size = tuple([int(i*ratio) for i in old_size])
            img = cv2.resize(img,(new_size[1], new_size[0]))
            pad_w = target_size[1] - new_size[1]
            pad_h = target_size[0] - new_size[0]
            top,bottom = pad_h//2, pad_h - (pad_h//2)
            left,right = pad_w//2, pad_w - (pad_w//2)
            img_new = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,None,(0,0,0))            
            cv2.imwrite(new_image_path+"/"+file_name+".jpg", img_new)
       
            f=open(filename,"r")
            f2=open(filename2,"w")
            count=0
            for each_line in f:
                each_line_list=each_line.split() #将每一行的数字分开放在列表中

                Xmin=float(each_line_list[4])
                Ymin=float(each_line_list[5])
                Xmax=float(each_line_list[6])
                Ymax=float(each_line_list[7])
                
                each_line_list[4]=(Xmin+1)*ratio+left
                each_line_list[5]=(Ymin+1)*ratio+top
                each_line_list[6]=(Xmax+1)*ratio+left
                each_line_list[7]=(Ymax+1)*ratio+top
                
                if each_line_list[4] <= 0:
                    each_line_list[4] = 1
                if each_line_list[5] <= 0:
                    each_line_list[5] = 1
                if each_line_list[6] >= 480:
                    each_line_list[6] = 480 - 1
                if each_line_list[7] >= 320:
                    each_line_list[7] = 320 - 1
                if each_line_list[6] <= each_line_list[4] or each_line_list[7] <= each_line_list[5]:
                    continue

                each_line_list[4]=float(each_line_list[4])  
                each_line_list[5]=float(each_line_list[5])  
                each_line_list[6]=float(each_line_list[6])  
                each_line_list[7]=float(each_line_list[7])  
                
                each_line_list[4]=str(format(each_line_list[4],'.2f'))  
                each_line_list[5]=str(format(each_line_list[5],'.2f'))  
                each_line_list[6]=str(format(each_line_list[6],'.2f'))  
                each_line_list[7]=str(format(each_line_list[7],'.2f'))  

                each_line_list.insert(1," ")
                each_line_list.insert(3," ")
                each_line_list.insert(5," ")
                each_line_list.insert(7," ")
                each_line_list.insert(9," ")
                each_line_list.insert(11," ")
                each_line_list.insert(13," ")
                each_line_list.insert(15," ")
                each_line_list.insert(17," ")
                each_line_list.insert(19," ")
                each_line_list.insert(21," ")
                each_line_list.insert(23," ")
                each_line_list.insert(25," ")
                each_line_list.insert(27," ")
                
                f2.writelines("".join(each_line_list))
                f2.write("\r\n")

                count+=1
               
            f.close()
            f2.close()

知识点:

  1. for root,dirs,files in os.walk(label_path):
    该语句的功能是获取label_path文件夹下的所有文件
    label_path: 代表需要遍历的根文件夹
    root: 表示正在遍历的文件夹的名字(根/子)
    dirs: 记录正在遍历的文件夹下的子文件夹集合
    files: 记录正在遍历的文件夹中的文件集合

  2. name.endswith(".txt"):
    要检查字符串的开头或结尾,只要使用str.startswith()和str.endswith()就可以

  3. img_new = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,None,(0,0,0))
    添加边框:
    cv2.copyMakeBorder()用来给图片添加边框,它有下面几个参数:
    src:要处理的原图
    top, bottom, left, right:上下左右要扩展的像素数
    borderType:边框类型,这个就是需要关注的填充方式, 其中默认方式和固定值方式最常用,参考:
    https://docs.opencv.org/3.3.1/d2/de8/group__core__array.html#ga209f2f4869e304c82d07739337eae7c5

  4. each_line_list=each_line.split()
    该语句的作用:将每一行的数字分开放在列表中

  5. 类型转换:
    float(each_line_list[4])
    str(format(each_line_list[4],’.2f’))

  6. each_line_list.insert(1," ")
    在字符串的某个位置中插入指定的字符

  7. f2.writelines("".join(each_line_list))
    将所有的行数合并写入.txt文件中

你可能感兴趣的:(#,Python编程,自动驾驶,tensorflow,深度学习,机器学习,pytorch)