多边形4点坐标转yolo格式矩形标签

import os 
import cv2
import numpy as np
import matplotlib.pyplot as plt 
from shapely.geometry import LineString
def plot_img_with_label(imgdir = None, labeldir= None, text_name=None):

    #图像可视化代码,在批量转换的时候需要将下面几行注释,否则会报错#-----------------------------------
    
    # image = cv2.imread(imgdir)
    image = cv2.imdecode(np.fromfile(imgdir, dtype=np.uint8), cv2.IMREAD_COLOR)  
    image = cv2.resize(image,(2160,2160))
    tl = 3 or round(0.002 * (image.shape[0] + image.shape[1]) / 2) + 1 
    tf = max(tl - 1, 1)
    #--------------------------------------------------------------------------------------------


    # h,w,_ = image.shape   
    boxs = []
    with open(labeldir ,'r') as f:
        for line in f:
            boxs.append(line.strip().split(" ")) 
    
    with open(text_name,"w") as ff:
        for i in boxs:
            x1 = float(i[1])
            y1 = float(i[2])
            x2 = float(i[3])
            y2 = float(i[4])
            x3 = float(i[5])
            y3 = float(i[6])
            x4 = float(i[7])
            y4 = float(i[8])

            point1 = [x1,y1]
            point2 = [x2,y2]
            point3 = [x3,y3]
            point4 = [x4,y4]  

            label = i[0]
            if len(i)<10:
                conf = "1.0"
            else:
                conf = i[9]
            label = label+":"+conf
            a = np.array([[np.array(point1), np.array(point2), np.array(point3), np.array(point4)]], dtype = np.int32) 
    

            x,y,w,h = cv2.boundingRect(a)
            roi=str(i[0])+" "+ str(x)+" "+str(y)+" "+ str(w)+" "+str(h)
            ff.write(roi + '\n')



            #图像可视化代码,在批量转换的时候需要将下面几行注释,否则会报错----------------------------------
            cv2.polylines(image, a, 1, [255,0,255],2) # 绘制多边形
            cv2.putText(image, label, (int(point1[0]), int(point1[1]) - 2), 0, 2, [225, 255, 0], thickness=tf, lineType=cv2.LINE_AA) # 显示对应类别
            cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
            #--------------------------------------------------------------------------------------------


    #图像可视化代码,在批量转换的时候需要将下面几行注释,否则会报错----------------------------------
    plt.imshow(image) 
    plt.show()
    print("*****finished: %s"%imgdir)
    #--------------------------------------------------------------------------------------------


def main():
    # json_floder_path = r'.\annotation'  #json文件的路径
    folder_path = r'.\yolo5'
    img_folder =  r'.\images'
    targetdir = r'.\yolo5_rect'
    names = os.listdir(folder_path)
    # imgw = 3840
    # imgh = 2160
    imgw = 1.0
    imgh = 1.0

    for name in names:
        imgname ="...."+name[:-4]+".jpg" #这里临时获取靠前部分文件名图片可视化,成批获取会报错
        # print (imgname)
        imgdir = os.path.join(img_folder,imgname)
        labeldir = os.path.join(folder_path,name)
        text_name = os.path.join(targetdir,name)
        plot_img_with_label(imgdir, labeldir,text_name)
    
    print("finished...")


if __name__ == "__main__":
    main()
posted @ 2022-12-28 20:06  TCcjx  阅读(120)  评论(0编辑  收藏  举报