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

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

程序员文章站 2022-04-16 08:14:54
import osimport cv2import numpy as npfrom PIL import Imageimport matplotlib.pyplot as pltdef save(cropImg, framenum, tracker): pathnew = "../images1_cut/" #裁剪后文件夹 if (os.path.exists(pathnew)): cv2.imwrite(pathnew + framenum + '_' +...
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])  #保留小数

本文地址:https://blog.csdn.net/mndlgzzd/article/details/107366878