python
1import xml.etree.ElementTree as ET 2from os import getcwd 3import sys,os 4sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')] 5 6classes = ["hoge"] 7 8if len(sys.argv) > 1: 9 classes = sys.argv[1:] 10 11with open('model_data/voc_classes.txt','w') as f: 12 f.write('\n'.join(classes)) 13 14 15def convert_annotation(year, image_id, list_file): 16 in_file = open('VOCDevkit/VOC%s/Annotations/%s.xml'%(year, image_id.replace(".jpg",""))) 17 tree=ET.parse(in_file) 18 root = tree.getroot() 19 20 for obj in root.iter('object'): 21 difficult = obj.find('difficult').text 22 cls = obj.find('name').text 23 if cls not in classes or int(difficult)==1: 24 continue 25 cls_id = classes.index(cls) 26 xmlbox = obj.find('bndbox') 27 b = (int(float(xmlbox.find('xmin').text)), 28 int(float(xmlbox.find('ymin').text)), 29 int(float(xmlbox.find('xmax').text)), 30 int(float(xmlbox.find('ymax').text))) 31 list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id)) 32 33wd = getcwd() 34 35for year, image_set in sets: 36 image_ids = open('VOCDevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split() 37 list_file = open('model_data/%s_%s.txt'%(year, image_set), 'w') 38 for image_id in image_ids: 39 if image_id == '1': continue 40 if image_id == '-1': continue 41 image_file_path = '%s/VOCDevkit/VOC%s/JPEGImages/%s'%(wd, year, image_id) 42 list_file.write(image_file_path) 43 convert_annotation(year, image_id, list_file) 44 list_file.write('\n') 45 list_file.close() 46 47
https://miyashinblog.com/yolo-fine-turning/
現在、こちらのサイトを参考に学習を行っています。
anaconda promt内で「python voc_annotation.py」を実行
その際、VOTTでアノテーションした.xmlファイルを『voc_annotation.py』YOLOの学習用に変換している際に以下のようなエラーが発生しました。
エラー
Traceback (most recent call last):
File "voc_annotation.py", line 43, in <module>
convert_annotation(year, image_id, list_file)
File "voc_annotation.py", line 16, in convert_annotation
in_file = open('VOCDevkit/VOC%s/Annotations/%s.xml'%(year, image_id.replace(".jpg","")))
FileNotFoundError: [Errno 2] No such file or directory: 'VOCDevkit/VOC2007/Annotations/hoge.JPG.xml'
補足
『voc_annotation.py』は6行目の
classes = ["item"]
内のアイテムをアノテーションタグ名称に変更して実行しています。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。