1.准备
 
1.检测结果的文本文件,文件的内容如下,图片名称,置信度,坐标  2.所检测图片的路径 3.生成xml文件的路径
 
2.代码
 
import os
from PIL 
import Image
 
image_path 
= r
'G:\shuju\test1'
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
)
C
=[]
for filename 
in os
.listdir
(image_path
):
    
    
    c 
= []
    for i 
in range(len(splitlines
)):
        
        if filename
[0:-4]==splitlines
[i
][0] and float(splitlines
[i
][1])>thresh
:
            c
.append
(splitlines
[i
])
            
            
            
            
    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]:
            
            im 
= Image
.open(path
)
            width
, height 
= im
.size
            
            
            
            
            
            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')
            
            for j 
in range(len(C
[i
])):
                
                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.结果
 
                
                
                
        
    
转载请注明原文地址:https://ipadbbs.8miu.com/read-1508.html