yolov5根据检测得到的txt文件和image文件,截取框中的图片并保存

import os
import cv2
path="/home/terryn/deep_learning/yolov5-master/output"
path3="/home/terryn/deep_learning/yolov5-master"
w=1920
h=1080
img_total=[]
txt_total=[]
file=os.listdir(path)
for filename in file:
    first,last=os.path.splitext(filename)
    if last==".jpg":
        img_total.append(first)
    else:
        txt_total.append(first)
for img_ in img_total:
    if img_ in txt_total:
        filename_img=img_+".jpg"
        path1=os.path.join(path,filename_img)
        img=cv2.imread(path1)
        filename_txt=img_+".txt"
        n=1
        with open(os.path.join(path,filename_txt),"r+",encoding="utf-8",errors="ignore") as f:
            for line in f:
                aa=line.split(" ")
                x_center =w * float(aa[1])
                y_center = h * float(aa[2])
                width=int(w*float(aa[3]))
                height=int(h*float(aa[4]))
                lefttopx=int(x_center-width/2.0)
                lefttopy=int(y_center-height/2.0)
                roi=img[lefttopy+1:lefttopy+height-1,lefttopx+1:lefttopx+width-1]
                filename_last=img_+"_"+str(n)+".jpg"
                print(filename_last)
                path2=os.path.join(path3,"roi")
                cv2.imwrite(os.path.join(path2,filename_last),roi)
                n=n+1
    else:
        continue

 

版权声明:本文为CSDN博主「firstwording」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Terry_n/article/details/107378639

firstwording

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

YOLOv3实现鱼类目标检测

YOLOv3实现鱼类目标检测
我将以一个项目实例,记录如何用YOLOv3训练自己的数据集。
在开始之前,首先了解一下YOLO系列代表性的DarkNet网络。
如下图所示,是YOLOv3中使用的Da