python实现将label中的矩形框坐标画到图片上

import os
import re
import cv2
from PIL import Image

path="/home/yangguide/Videos/label_2"
path1="/home/yangguide/Videos/image_2"
path2="/home/yangguide/Videos/haha"

for root,dirs,files in os.walk(path):
    for name in files:
         if name.endswith(".txt"):
            filename=root+"/"+name
            filename2=path2+"/"+name  
            
            file_name = name.split('.')[0]
            file_path=path1+"/"+file_name+".png"
            img = cv2.imread(file_path)

            f=open(filename,"r")
            count=0
            for each_line in f:
                each_line_list=each_line.split()#将每一行的数字分开放在列表中
                
                xmin=float(each_line_list[4])
                ymin=float(each_line_list[5])
                xmax=float(each_line_list[6])
                ymax=float(each_line_list[7])    
                
#                 h = ymax - ymin
#                 w = xmax - xmin
#                 if(w < 20 or h < 20):
#                     count += 1
#                     break
               
                cls=str(each_line_list[0])
                print(cls)
                c1, c2 = (int(xmin), int(ymin)), (int(xmax), int(ymax))
                cv2.rectangle(img, c1, c2, (0, 0, 255), 2)  
                cv2.putText(img, cls, c2, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)                

                count+=1
                print(count)
            cv2.imwrite(path2+"/"+file_name+".png", img)
             
            f.close()

知识点:

  1. name.split(’.’)[0]功能: 将字符串以’.'分开放在列表中
    示例:name = ‘www.doiido.com’
>>>name.split(".")
['www', 'doiido', 'com']
>>>name.split(".")[0]
www

你可能感兴趣的:(#,Python编程,自动驾驶,深度学习,机器学习,pytorch)