Reference
第一篇
第二篇文章来源:https://www.toymoban.com/news/detail-510287.html
code
本文实现了代码的批量提取文章来源地址https://www.toymoban.com/news/detail-510287.html
import os
import cv2
def main():
img_path = './yolov5-master/runs/detect/exp2/' # 图片路径
label_path = './yolov5-master/runs/detect/exp2/labels/' # txt文件路径
save_path = '../data/pic_extracted/' # 保存路径
img_total = []
label_total = []
imgfile = os.listdir(img_path)
labelfile = os.listdir(label_path)
for filename in imgfile:
name, type = os.path.splitext(filename)
if type == ('.jpg' or '.png'):
img_total.append(name)
for filename in labelfile:
name, type = os.path.splitext(filename)
if type == '.txt':
label_total.append(name)
for _img in img_total:
if _img in label_total:
filename_img = _img + '.jpg'
path = os.path.join(img_path, filename_img)
img = cv2.imread(path) # 读取图片,结果为三维数组
filename_label = _img + '.txt'
w = img.shape[1] # 图片宽度(像素)
h = img.shape[0] # 图片高度(像素)
n = 1
# 打开文件,编码格式'utf-8','r+'读写
with open(os.path.join(label_path, filename_label), "r+", encoding='utf-8', errors="ignor") as f:
for line in f:
msg = line.split(" ") # 根据空格切割字符串,最后得到的是一个list
x1 = int((float(msg[1]) - float(msg[3]) / 2) * w) # x_center - width/2
y1 = int((float(msg[2]) - float(msg[4]) / 2) * h) # y_center - height/2
x2 = int((float(msg[1]) + float(msg[3]) / 2) * w) # x_center + width/2
y2 = int((float(msg[2]) + float(msg[4]) / 2) * h) # y_center + height/2
filename_last = _img + "_" + str(n) + ".jpg"
print(filename_last)
img_roi = img[y1:y2, x1:x2] # 剪裁,roi:region of interest
cv2.imwrite(os.path.join(save_path, filename_last), img_roi)
n = n + 1
else:
continue
if __name__ == '__main__':
main()
到了这里,关于yolov5目标检测样本框批量提取(将检测到的目标裁剪出来)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!