-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml2txt.py
62 lines (55 loc) · 2.45 KB
/
xml2txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import xml.etree.ElementTree as ET
import os
# Class isimleri ve indexleri
class_names = [
"person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog",
"horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle",
"wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich",
"orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote",
"keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book",
"clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"
]
class_dict = {class_name: idx for idx, class_name in enumerate(class_names)}
def convert_to_yolo(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(xml_path, output_path):
tree = ET.parse(xml_path)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
with open(output_path, 'w') as out_file:
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in class_dict:
continue
cls_id = class_dict[cls]
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text),
float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert_to_yolo((w, h), b)
out_file.write(f"{cls_id} " + " ".join([str(a) for a in bb]) + '\n')
# Kullanım örneği
xml_dir = "images"
output_dir = "labels"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for xml_file in os.listdir(xml_dir):
if xml_file.endswith('.xml'):
xml_path = os.path.join(xml_dir, xml_file)
output_path = os.path.join(output_dir, os.path.splitext(xml_file)[0] + '.txt')
convert_annotation(xml_path, output_path)