ターゲット検出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])  #