提示:代码仅供参考
前言
目标检测
用于VOC数据集复现标注信息,可能不太准确,可以更改参数
一、VOC数据集
博主一般使用MakeSense标注图片,这段代码可以用来复现标注好的信息,也可以用于其他已存的开源数据集
二、使用步骤
1.引入库
代码如下(示例):
import os
import xml.etree.ElementTree as ET
import cv2 as cv
2.读入数据
代码如下(示例):
def convert_annotation(img):
in_file = open('D:/pycharm/pic_anno_enh/1_000001.xml', encoding='utf-8')
tree=ET.parse(in_file)
root = tree.getroot()
loac=[]
for obj in root.iter('object'):
#确定坐标
xmlbox = obj.find('bndbox')
b = (int(float(xmlbox.find('xmin').text)), int(float(xmlbox.find('ymin').text)), int(float(xmlbox.find('xmax').text)), int(float(xmlbox.find('ymax').text)))
loac.append(b)
return loac
if __name__=="__main__":
img_=r'D:\pycharm\pic_jpeg_enh\1_000001.jpg'
loaction=convert_annotation(img_)
#print(loaction)
print(img_)
img = cv.imread(img_)
for b in loaction:
x1=b[0]
y1=b[1]
x2=b[2]
y2=b[3]
cv.rectangle(img,(x1,y1),(x2,y2),color=(0,0,255),thickness=9)
print(img.shape)
# print(int(img.shape[1]/10),int(img.shape[0]/10))
if img.shape[0]>3000:
img=cv.resize(img,(int(img.shape[1]/5),int(img.shape[0]/5)),interpolation=cv.INTER_AREA)
print(img.shape)
cv.imshow(os.path.basename(img_), img)
cv.waitKey(0) # 参数为0,代表任意键关闭图片;若为数字,则为毫秒延时
cv.destroyAllWindows()
总结
以上就是今天要讲的内容