Look it in its website:
We can find the mothed of installing labelImg:

So we could just input the command in cmd:
pip install labelImg -i https://pypi.douban.com/simple
Wait it for a while.

Then we get the result.
We could find it download PyQt5 automatically.
Input labelImg in cmd, and we could see it:


Then it saves a xml file in this floder:

Let's see the xml :
- <annotation>
- <folder>labelimgfolder>
- <filename>bus.jpgfilename>
- <path>C:\Users\misty\Desktop\labelimg\bus.jpgpath>
- <source>
- <database>Unknowndatabase>
- source>
- <size>
- <width>810width>
- <height>1080height>
- <depth>3depth>
- size>
- <segmented>0segmented>
- <object>
- <name>personname>
- <pose>Unspecifiedpose>
- <truncated>0truncated>
- <difficult>0difficult>
- <bndbox>
- <xmin>41xmin>
- <ymin>395ymin>
- <xmax>259xmax>
- <ymax>910ymax>
- bndbox>
- object>
- <object>
- <name>shoename>
- <pose>Unspecifiedpose>
- <truncated>0truncated>
- <difficult>0difficult>
- <bndbox>
- <xmin>154xmin>
- <ymin>845ymin>
- <xmax>255xmax>
- <ymax>907ymax>
- bndbox>
- object>
- annotation>
Python's xml module parses xml files.
The information we need is the image width width, image height height, the coordinates xmin and ymin in the upper left corner of the detection box, and the coordinates xmax and ymax in the lower right corner of the detection box. We can run voc_label.py to generate YOLOv5 label file in labels folder. The data in each row of the label file are class, x,y, w,h, class is the category of the object, x and y are the center coordinates of the detection box, and w and h are the width and height of the detection box.
voc_label.py code:
- import xml.etree.ElementTree as ET
- import os
- from os import getcwd
- from tqdm import tqdm
-
- classes = ["helmet", "person"]
-
- def convert(size, box):
- dw = 1. / size[0]
- dh = 1. / size[1]
- x = (box[0] + box[1]) / 2.0
- y = (box[2] + box[3]) / 2.0
- 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(image_id):
- in_file = './xml/%s.xml' % (image_id)
- out_file = open('./labels/%s.txt' % (image_id), 'w')
- tree = ET.parse(in_file)
- root = tree.getroot()
- size = root.find('size')
- w = int(size.find('width').text)
- h = int(size.find('height').text)
-
- 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 = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
- float(xmlbox.find('ymax').text))
- bb = convert((w, h), b)
- out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
-
- if __name__ == "__main__":
- wd = getcwd()
- print(wd)
- if not os.path.exists('./labels/'):
- os.makedirs('./labels/')
- image_ids = os.listdir('./datasets')
- for image_id in tqdm(image_ids):
- convert_annotation(image_id.split('.')[0])