【发布时间】:2021-03-15 08:27:54
【问题描述】:
这是我的代码:
import numpy as np
import os
import json
import xmltodict
from xml.dom import minidom
BASE_Path = 'E:/Personal Projects/ML/computer_vision/open_image_vehicle/'
for file in os.listdir(os.path.join(BASE_Path,'cng_resized')):
if file.endswith(".xml"):
mydoc = minidom.parse(file)
li = []
ob = mydoc.getElementsByTagName('object')
image_width = 416.0
image_height = 416.0
# total amount of items
for i in ob:
bbox = i.getElementsByTagName("bndbox")
#print(len(bbox))
for child in bbox:
xmin = float(child.getElementsByTagName("xmin")[0].childNodes[0].nodeValue)
ymin = float(child.getElementsByTagName("ymin")[0].childNodes[0].nodeValue)
xmax = float(child.getElementsByTagName("xmax")[0].childNodes[0].nodeValue)
ymax = float(child.getElementsByTagName("ymax")[0].childNodes[0].nodeValue)
bbox_w =(xmax - xmin)/image_width
bbox_h = (ymax - ymin)/image_height
x_center = (xmin + bbox_w/2)/image_width
y_center = (ymin + bbox_h/2)/image_height
li.append([5,x_center,y_center,bbox_w,bbox_h])
li = np.array(li)
print(BASE_Path)
np.savetxt(os.path.join(BASE_Path, "/cng/labels/"+file.rsplit('.', 1)[0]+".txt"),
li,
fmt = ["%d", "%f", "%f", "%f", "%f"]
)
所需输出: E:/Personal Projects/ML/computer_vision/open_image_vehicle/cng/labels/cng.txt
但显示:没有这样的文件或目录:'E:/cng/labels/CNG.txt'
为什么 os.path.join 不加入 BASE_Path??
【问题讨论】: