花了几天时间调试了一下SSD网络,对于FLIR数据集训练测试进行了准备工作,最终顺利调通
json_yolo格式准换
from __future__ import print_function
import argparse
import glob
import os
import json
if __name__ == '__main__' :
parser = argparse. ArgumentParser( )
parser. add_argument(
"path" , default= 'E:/CNN/ssd/data/json/' , help = 'Directory of json files containing annotations' )
parser. add_argument(
"output_path" , default= 'E:/CNN/ssd/data/xml/' , help = 'Output directory for image.txt files' )
args = parser. parse_args( )
json_files = sorted ( glob. glob( os. path. join( args. path, '*.json' ) ) )
for json_file in json_files:
with open ( json_file) as f:
data = json. load( f)
images = data[ 'images' ]
annotations = data[ 'annotations' ]
width = 640.0
height = 512.0
for i in range ( 0 , len ( images) ) :
converted_results = [ ]
for ann in annotations:
if ann[ 'image_id' ] == i and ann[ 'category_id' ] <= 3 :
cat_id = int ( ann[ 'category_id' ] )
left, top, bbox_width, bbox_height = map ( float , ann[ 'bbox' ] )
cat_id -= 1
x_center, y_center = (
left + bbox_width / 2 , top + bbox_height / 2 )
x_rel, y_rel = ( x_center / width, y_center / height)
w_rel, h_rel = ( bbox_width / width, bbox_height / height)
converted_results. append(
( cat_id, x_rel, y_rel, w_rel, h_rel) )
image_name = images[ i] [ 'file_name' ]
image_name = image_name[ 14 : - 5 ]
print ( image_name)
file = open ( args. output_path + str ( image_name) + '.txt' , 'w+' )
file . write( '\n' . join( '%d %.6f %.6f %.6f %.6f' % res for res in converted_results) )
file . close( )
yolo_xml转换
"""
Created on Fri Aug 17 22:10:01 2018
@author: Caroline Pacheco do E. Silva
"""
import os
import cv2
from xml. dom. minidom import parseString
from lxml. etree import Element, SubElement, tostring
import numpy as np
from os. path import join
YOLO_CLASSES = ( 'person' , 'bicycle' , 'car' , 'motorcycle' )
def unconvert ( class_id, width, height, x, y, w, h) :
xmax = int ( ( x * width) + ( w * width) / 2.0 )
xmin = int ( ( x * width) - ( w * width) / 2.0 )
ymax = int ( ( y * height) + ( h * height) / 2.0 )
ymin = int ( ( y * height) - ( h * height) / 2.0 )
class_id = int ( class_id)
return ( class_id, xmin, xmax, ymin, ymax)
ROOT = 'E:/CNN/ssd/data/yolo'
def xml_transform ( root, classes) :
class_path = join( root, 'labels' )
ids = list ( )
l = os. listdir( class_path)
check = '.DS_Store' in l
if check == True :
l. remove( '.DS_Store' )
ids = [ x. split( '.' ) [ 0 ] for x in l]
annopath = join( root, 'labels' , '%s.txt' )
imgpath = join( root, 'images' , '%s.jpeg' )
os. makedirs( join( root, 'outputs' ) , exist_ok= True )
outpath = join( root, 'outputs' , '%s.xml' )
for i in range ( len ( ids) ) :
img_id = ids[ i]
img = cv2. imread( imgpath % img_id)
height, width, channels = img. shape
node_root = Element( 'annotation' )
node_folder = SubElement( node_root, 'folder' )
node_folder. text = 'VOC2007'
img_name = img_id + '.jpeg'
node_filename = SubElement( node_root, 'filename' )
node_filename. text = img_name
node_source = SubElement( node_root, 'source' )
node_database = SubElement( node_source, 'database' )
node_database. text = 'Coco database'
node_size = SubElement( node_root, 'size' )
node_width = SubElement( node_size, 'width' )
node_width. text = str ( width)
node_height = SubElement( node_size, 'height' )
node_height. text = str ( height)
node_depth = SubElement( node_size, 'depth' )
node_depth. text = str ( channels)
node_segmented = SubElement( node_root, 'segmented' )
node_segmented. text = '0'
target = ( annopath % img_id)
if os. path. exists( target) :
label_norm = np. loadtxt( target) . reshape( - 1 , 5 )
for i in range ( len ( label_norm) ) :
labels_conv = label_norm[ i]
new_label = unconvert( labels_conv[ 0 ] , width, height, labels_conv[ 1 ] , labels_conv[ 2 ] , labels_conv[ 3 ] ,
labels_conv[ 4 ] )
node_object = SubElement( node_root, 'object' )
node_name = SubElement( node_object, 'name' )
node_name. text = classes[ new_label[ 0 ] ]
node_pose = SubElement( node_object, 'pose' )
node_pose. text = 'Unspecified'
node_truncated = SubElement( node_object, 'truncated' )
node_truncated. text = '0'
node_difficult = SubElement( node_object, 'difficult' )
node_difficult. text = '0'
node_bndbox = SubElement( node_object, 'bndbox' )
node_xmin = SubElement( node_bndbox, 'xmin' )
node_xmin. text = str ( new_label[ 1 ] )
node_ymin = SubElement( node_bndbox, 'ymin' )
node_ymin. text = str ( new_label[ 3 ] )
node_xmax = SubElement( node_bndbox, 'xmax' )
node_xmax. text = str ( new_label[ 2 ] )
node_ymax = SubElement( node_bndbox, 'ymax' )
node_ymax. text = str ( new_label[ 4 ] )
xml = tostring( node_root, pretty_print= True )
dom = parseString( xml)
print ( xml)
f = open ( outpath % img_id, "wb" )
f. write( xml)
f. close( )
xml_transform( ROOT, YOLO_CLASSES)
提取文件名并保存成txt格式文件
import os
file_path = "E:/CNN/ssd/data/xml/"
path_list = os. listdir( file_path)
path_name = [ ]
for i in path_list:
path_name. append( i. split( "." ) [ 0 ] )
for file_name in path_name:
with open ( "E:/CNN/ssd/data/train.txt" , "a" ) as file :
file . write( file_name + "\n" )
print ( file_name)
file . close( )