YOLOv3_TensorFlow版本標籤製作

此處標籤製作匹配的YOLOV3_Tensorflow版本是針對於:https://github.com/wizyoung/YOLOv3_TensorFlow.git

標籤格式爲:

0 xxx/xxx/a.jpg 1920 1080 0 453 369 473 391 1 588 245 608 268

結構就是:圖片索引+圖片路徑+圖片大小+圖片類別+圖像左上角和右下角座標

舉例說明:比如一張針對圖片000035.jpg,對應的標籤就是:

000035 /YOLOv3_TensorFlow/data/my_data/train/000035.jpg 500 375 14 1 96 191 361 14 218 98 465 318

標籤生成代碼:

import os
import xml.etree.cElementTree as ET

classes = ['aeroplane','bicycle','bird','boat','bottle','bus','car',
           'cat','chair','cow','diningtable','dog','horse','motorbike',
           'person','pottedplant','sheep','sofa','train','tvmonitor']

def convert_annotation(xml,list_file):
    in_file = open(xml)
    tree = ET.parse(in_file)
    root = tree.getroot()
    xmlsize = root.find('size')
    d = (int(xmlsize.find('width').text), int(xmlsize.find('height').text))
    if d[0]==500 and d[1]==375:
        list_file.write(" " + " ".join([str(c) for c in d]))
        for obj in root.iter('object'):
            difficult = obj.find('difficult').text
            cls = obj.find('name').text
            if cls not in classes or int(difficult) == 1:
                continue
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text),
                 int(xmlbox.find('ymax').text))
            list_file.write(" " + str(cls_id) + " " + " ".join([str(a) for a in b]))


wd = os.getcwd()

data_base_dir ="/YOLOv3_TensorFlow/data/my_data/train"
files=os.listdir(data_base_dir)
filepath ="/YOLOv3_TensorFlow/data/my_data/xml"
xmlfiles = os.listdir(filepath)
file_list=[]
write_file_name = "/YOLOv3_TensorFlow/data/my_data/train_file.txt"
write_file = open(write_file_name, "w")
for file in files:
    if file.endswith(".jpg"):
        index=file.rfind('.')
        file=file[:index]
        file_list.append(file)
number_of_lines=len(file_list)
for current_line in range(number_of_lines):
    write_file.write(file_list[current_line]+'\n')
write_file.close()
image_ids=open('/YOLOv3_TensorFlow/data/my_data/train_file.txt').read().strip().split()
list_file_name="/YOLOv3_TensorFlow/data/my_data/train.txt"
list_file=open(list_file_name,'w')
line_ind=0

for xml in xmlfiles:
    x=os.path.splitext(xml)[0]
    img_path=str(data_base_dir+"/"+x+'.jpg')
    list_file.write((x+" "+img_path))
    xml=filepath+"/"+xml
    convert_annotation(xml,list_file)
    list_file.write('\n')
    line_ind += 1
list_file.close()

最後結果展示:

000008/YOLOv3_TensorFlow/data/my_data/train/000008.jpg 500 375 8 192 16 364 249
000005 /YOLOv3_TensorFlow/data/my_data/train/000005.jpg 500 375 8 263 211 324 339 8 165 264 253 372 8 241 194 295 299
000069 /YOLOv3_TensorFlow/data/my_data/train/000069.jpg 500 375 3 19 64 424 190 14 305 155 390 375
000080 /YOLOv3_TensorFlow/data/my_data/train/000080.jpg 500 375 3 246 202 269 282 3 70 243 111 274
000088 /YOLOv3_TensorFlow/data/my_data/train/000088.jpg 500 375 18 123 7 500 365
000038 /YOLOv3_TensorFlow/data/my_data/train/000038.jpg 500 375 1 159 178 279 246 13 1 235 43 315 14 192 111 261 240
000035 /YOLOv3_TensorFlow/data/my_data/train/000035.jpg 500 375 14 1 96 191 361 14 218 98 465 318
000025 /YOLOv3_TensorFlow/data/my_data/train/000025.jpg 500 375 9 2 84 59 248 9 68 115 233 279 9 64 173 377 373 14 320 2 496 375 14 221 4 341 374 14 135 14 220 148 9 69 43 156 177 14 58 54 104 139 14 279 1 331 86 14 320 22 344 96
000058 /YOLOv3_TensorFlow/data/my_data/train/000058.jpg 500 375 14 334 1 436 373 14 196 80 309 326 14 279 53 334 267 13 29 109 359 375
000063 /YOLOv3_TensorFlow/data/my_data/train/000063.jpg 500 375 11 123 115 379 275 8 75 1 428 375
000015 /YOLOv3_TensorFlow/data/my_data/train/000015.jpg 500 375 1 77 136 360 358
000050 /YOLOv3_TensorFlow/data/my_data/train/000050.jpg 500 375 4 360 192 381 265 4 399 181 422 235 4 270 180 291 247 4 294 176 312 241 14 68 96 293 375 14 1 71 87 332 14 185 68 259 197 14 286 64 406 238
000066 /YOLOv3_TensorFlow/data/my_data/train/000066.jpg 500 375 14 209 187 228 230 14 242 182 274 259 14 269 188 295 259
000076 /YOLOv3_TensorFlow/data/my_data/train/000076.jpg 500 375 14 63 78 265 375 14 257 75 448 375 14 362 130 446 261 14 472 157 500 343 14 457 209 478 255 14 439 170 458 216 14 247 142 288 197 14 44 162 84 286 14 77 157 120 245 14 1 159 26 283 14 24 174 45 201
000013 /YOLOv3_TensorFlow/data/my_data/train/000013.jpg 500 375 9 299 160 446 252
000006 /YOLOv3_TensorFlow/data/my_data/train/000006.jpg 500 375 15 187 135 282 242 10 154 209 369 375 8 255 207 366 375 8 138 211 249 375
000068 /YOLOv3_TensorFlow/data/my_data/train/000068.jpg 500 375 2 27 45 266 375
000003 /YOLOv3_TensorFlow/data/my_data/train/000003.jpg 500 375 17 123 155 215 195 8 239 156 307 205
000037 /YOLOv3_TensorFlow/data/my_data/train/000037.jpg 500 375 11 61 96 464 339
000045 /YOLOv3_TensorFlow/data/my_data/train/000045.jpg 500 375 19 280 64 483 252
000019 /YOLOv3_TensorFlow/data/my_data/train/000019.jpg 500 375 7 231 88 483 256 7 11 113 266 259
000057 /YOLOv3_TensorFlow/data/my_data/train/000057.jpg 500 375 2 133 53 385 358
000054 /YOLOv3_TensorFlow/data/my_data/train/000054.jpg 500 375 5 24 54 472 292
000053 /YOLOv3_TensorFlow/data/my_data/train/000053.jpg 500 375 7 36 12 425 262
000074 /YOLOv3_TensorFlow/data/my_data/train/000074.jpg 500 375 6 58 61 475 283
000039 /YOLOv3_TensorFlow/data/my_data/train/000039.jpg 500 375 19 156 89 344 279

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章