在使用YOLO算法进行目标检测的时候,我们常常需要获取检测到的目标图像进行下一步操作,Franpper在本文中为大家提供了预测时生成存放目标坐标的.txt文件与通过坐标在原图上进行裁剪目标图像的方法。
目录
一、生成预测结果的坐标txt文件
二、通过坐标在原图上进行裁剪目标图像
在预测程序detect.py中,有这样一行代码,是用来选择是否生成预测结果的坐标txt文件
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
加上default=True,如下。即可在生成预测结果的时候同时生成txt文件
parser.add_argument('--save-txt', default=True, action='store_true', help='save results to *.txt')
存放在 labels文件夹中
import os
import cv2
path = r'F:\work\2023-0103-0106\part\exp\labels' # jpg图片和对应的生成结果的txt标注文件,放在一起
path3 = r'F:\work\2023-0103-0106\part\exp\labels\cut' # 裁剪出来的小图保存的根目录
path2 = r'F:\work\2023-0103-0106\part\exp\labels\crop' # 覆盖目标区域后的原图
file = os.listdir(path)
# 生成图像与标签名称列表
img_total = []
txt_total = []
for filename in file:
first, last = os.path.splitext(filename)
if last == ".jpg": # 图片的后缀名
img_total.append(first)
else:
txt_total.append(first)
for img_name in img_total:
if img_name in txt_total:
filename_img = img_name+".jpg"
path1 = os.path.join(path, filename_img)
img = cv2.imread(path1)
h, w = img.shape[0], img.shape[1]
img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC) # resize 图像大小,否则roi区域可能会报错
filename_txt = img_name+".txt"
n = 1
with open(os.path.join(path, filename_txt), "r+", encoding="utf-8", errors="ignore") as f:
for line in f:
coordinate = line.split(" ")
x_center = w * float(coordinate[1]) # coordinate[1]左上点的x坐标
y_center = h * float(coordinate[2]) # coordinate[2]左上点的y坐标
width = int(w*float(coordinate[3])) # coordinate[3]图片width
height = int(h*float(coordinate[4])) # coordinate[4]图片height
lefttopx = int(x_center-width/2.0)
lefttopy = int(y_center-height/2.0)
filename_last = img_name + "_" + str(n) + ".jpg"
roi = img[lefttopy+1:lefttopy+height+3, lefttopx+1:lefttopx+width+1]
cv2.imwrite(os.path.join(path3, filename_last), roi)
filename_last = img_name+"_"+str(n)+".jpg" # 裁剪出来的小图文件名
img[lefttopy + 1:lefttopy + height + 3, lefttopx + 1:lefttopx + width + 1] = (255, 255, 255)
n = n+1
cv2.imwrite(os.path.join(path2, filename_last), img)
else:
continue