yolov5輸出檢測框的中心位置,框的長寬,框的位置,以及輸出對應(yīng)標(biāo)簽格式的輸出。 模型讀取每個圖片,并將上述的信息依此輸出到同名的txt文件中保存,具體需要哪些坐標(biāo)可以自己選。
更改信息在detect.py文件中,需要更改的第一個地方在如下地方,輸出位置在runs/detect/exp中
# 自己改的部分
location_center_dir = str(save_dir) + '/detect_location'
if not os.path.exists(location_center_dir):
os.makedirs(location_center_dir)
location_center_path = location_center_dir + '\\' + str(p.stem) + (
'' if dataset.mode == 'image' else f'_{frame}') #
flocation = open(location_center_path + '.txt', 'a')
第二個需要更改的地方:
# 自己改的部分
x0 = (int(xyxy[0].item()) + int(xyxy[2].item())) / 2
y0 = (int(xyxy[1].item()) + int(xyxy[3].item())) / 2 # 中心點(diǎn)坐標(biāo)(x0, y0)
chang = int(xyxy[2].item()) - int(xyxy[0].item())
kuan = int(xyxy[3].item()) - int(xyxy[1].item())
# class_index = cls # 獲取屬性
# object_name = names[int(cls)] # 獲取標(biāo)簽名如:person
label = int(cls) # 對應(yīng)每個物體的標(biāo)簽對應(yīng)的數(shù)字label,如person:0
x0 = format(x0/2448, '.6f') #此處我要保存和訓(xùn)練標(biāo)注的txt一樣的格式,故保留六位小數(shù)
y0 = format(y0/2048, '.6f')
chang = format(chang/2448, '.6f') # 2448和2048為我的圖片長和寬
kuan = format(kuan/2048, '.6f')
if label==0: # 在這里我只需要保存person的信息,可以刪去,也可以自己更改
flocation.write(str(label) + ' ' + str(x0) + ' ' + str(y0) + ' '+str(chang)+' ' +str(kuan)+'\n')
注釋應(yīng)該很清楚了,int(xyxy[0].item()) 為左邊的X坐標(biāo),int(xyxy[0].item(2))為右邊的x坐標(biāo),int(xyxy[1].item()) 為上面的y坐標(biāo),int(xyxy[3].item()) 為下面的y坐標(biāo)。文章來源:http://www.zghlxwxcb.cn/news/detail-523162.html
我的坐標(biāo)信息除以2048和2448,是為了獲得labelimg標(biāo)注工具同類型的坐標(biāo)信息(用于訓(xùn)練或者var的label標(biāo)簽),即比例。需要用可通過img.shape()獲得。文章來源地址http://www.zghlxwxcb.cn/news/detail-523162.html
到了這里,關(guān)于yolov5輸出檢測框的中心位置,框的長寬,框的位置,以及輸出對應(yīng)標(biāo)簽格式的輸出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!