目标检测yolo根据标签box坐标剪裁原图至新的文件夹

import os
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

def save(cropImg, framenum, tracker):  
    pathnew = "../images1_cut/"  #裁剪后文件夹
    if (os.path.exists(pathnew)):
        cv2.imwrite(pathnew + framenum + '_' + tracker + '.jpg', cropImg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
    else:
        os.makedirs(pathnew)
        cv2.imwrite(pathnew + framenum + tracker + '.jpg', cropImg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])

f = open("../person.txt", "r")   #标签txt文件
lines = f.readlines()
for line in lines:
    li = line.split(' ')
    print(li[0], li[1], li[2], li[3], li[4], li[5])  #根据自己的txt内容格式进行修改
    filename = li[0] + '.jpg'
    img = cv2.imread("../images1/" + filename)  #原始图片文件夹

    a = int(float(li[3]))  # xmin 
    b = int(float(li[5]))  # xmax
    c = int(float(li[2]))  # ymin 
    d = int(float(li[4]))  # ymax 
    cropImg = img[a:b, c:d]  # 裁剪

    save(cropImg, li[0], str(li[1]).split('.')[1][:3])  #保留小数

你可能感兴趣的:(数据集处理,python,opencv)