将fasterrcnn的检测结果生成xml的voc数据集格式文件

    技术2022-07-10  105

    1.准备

    1.检测结果的文本文件,文件的内容如下,图片名称,置信度,坐标 2.所检测图片的路径 3.生成xml文件的路径

    2.代码

    import os #import glob from PIL import Image #import cv2 #图像存储位置 image_path = r'G:\shuju\test1' # 图像的 ground truth 的 txt 文件存放位置 result_txt='./xin_test_qx.txt' image_xml_dir='./Annotations' thresh=0.9 def read_txt(wei_zhi):#阅读文本文件函数 with open(wei_zhi, 'r') as f: lines = f.readlines() splitlines = [x.strip().split(' ') for x in lines] return splitlines#返回带类列表 splitlines=read_txt(result_txt) #print(splitlines) C=[]#c中存储的使连续的挨着的缺陷坐标位置 for filename in os.listdir(image_path): #path=image_path+'/'+filename #print(path) c = [] for i in range(len(splitlines)): #print(splitlines[i][0]) if filename[0:-4]==splitlines[i][0] and float(splitlines[i][1])>thresh:#如果文本文件中的图片名称与所有图片 c.append(splitlines[i]) #print(filename[0:-4]) #image = cv2.imread(path) #print(image) #cv2.imwrite(image_path1+'/'+filename,image) if c==[]: continue else: C.append(c) print(C) for filename in os.listdir(image_path):#阅读图片文件夹 path=image_path+'/'+filename for i in range(len(C)):#遍历焊缝坐标 if filename[:-4]==C[i][0][0]:#当图片名称和焊缝图片对应上,进行处理 #img=cv2.imread(path) im = Image.open(path) width, height = im.size # open the crospronding txt file #gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines() # gt = open(src_txt_dir + '/gt_' + img + '.txt').read().splitlines() # write in xml file # os.mknod(src_xml_dir + '/' + img + '.xml') xml_file = open((image_xml_dir+ '/' +filename[:-4]+ '.xml'), 'w') xml_file.write('<annotation>\n') xml_file.write(' <folder>VOC2007</folder>\n') xml_file.write(' <filename>' +filename+'</filename>\n') xml_file.write(' <path>'+'E:/pythonpycharm/yuan_Faster-RCNN-TensorFlow-Python3-master/data/VOCdevkit2007/VOC2007/'+filename+ '.bmp'+'</path>\n') xml_file.write(' <source>\n') xml_file.write(' <database>'+'Unknown'+'</database>\n') xml_file.write(' </source>\n') xml_file.write(' <size>\n') xml_file.write(' <width>' + str(width) + '</width>\n') xml_file.write(' <height>' + str(height) + '</height>\n') xml_file.write(' <depth>'+'1'+'</depth>\n') xml_file.write(' </size>\n') xml_file.write(' <segmented>'+'0'+'</segmented>\n') # write the region of image on xml file for j in range(len(C[i])): #spt = img_each_label.split(' ') # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。 xml_file.write(' <object>\n') xml_file.write(' <name>' + 'qx' + '</name>\n') xml_file.write(' <pose>Unspecified</pose>\n') xml_file.write(' <truncated>0</truncated>\n') xml_file.write(' <difficult>0</difficult>\n') xml_file.write(' <bndbox>\n') xml_file.write(' <xmin>' + C[i][j][2] + '</xmin>\n') xml_file.write(' <ymin>' + C[i][j][3] + '</ymin>\n') xml_file.write(' <xmax>' + C[i][j][4] + '</xmax>\n') xml_file.write(' <ymax>' + C[i][j][5] + '</ymax>\n') xml_file.write(' </bndbox>\n') xml_file.write(' </object>\n') xml_file.write('</annotation>')

    3.结果

    Processed: 0.021, SQL: 9