Skip to content

Commit f563c16

Browse files
committed
Create a const for encoding and fix an issue in yolo format according to pr#387
1 parent 40ea8e7 commit f563c16

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LabelImg is a graphical image annotation tool.
1212
It is written in Python and uses Qt for its graphical interface.
1313

1414
Annotations are saved as XML files in PASCAL VOC format, the format used
15-
by `ImageNet <http://www.image-net.org/>`__.
15+
by `ImageNet <http://www.image-net.org/>`__. Besdies, it also supports YOLO format
1616

1717
.. image:: https://raw.githubusercontent.com/tzutalin/labelImg/master/demo/demo3.jpg
1818
:alt: Demo Image

libs/constants.py

100755100644
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
FORMAT_PASCALVOC='PascalVOC'
1616
FORMAT_YOLO='YOLO'
1717
SETTING_DRAW_SQUARE = 'draw/square'
18+
DEFAULT_ENCODING = 'utf-8'

libs/pascal_voc_io.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from xml.etree.ElementTree import Element, SubElement
66
from lxml import etree
77
import codecs
8+
from libs.constants import DEFAULT_ENCODING
89

910
XML_EXT = '.xml'
10-
ENCODE_METHOD = 'utf-8'
11+
ENCODE_METHOD = DEFAULT_ENCODING
1112

1213
class PascalVocWriter:
1314

@@ -84,11 +85,8 @@ def appendObjects(self, top):
8485
for each_object in self.boxlist:
8586
object_item = SubElement(top, 'object')
8687
name = SubElement(object_item, 'name')
87-
try:
88-
name.text = unicode(each_object['name'])
89-
except NameError:
90-
# Py3: NameError: name 'unicode' is not defined
91-
name.text = each_object['name']
88+
print (each_object['name'])
89+
name.text = each_object['name']
9290
pose = SubElement(object_item, 'pose')
9391
pose.text = "Unspecified"
9492
truncated = SubElement(object_item, 'truncated')

libs/ustr.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import sys
2+
from libs.constants import DEFAULT_ENCODING
23

34
def ustr(x):
45
'''py2/py3 unicode helper'''
56

67
if sys.version_info < (3, 0, 0):
78
from PyQt4.QtCore import QString
89
if type(x) == str:
9-
return x.decode('utf-8')
10+
return x.decode(DEFAULT_ENCODING)
1011
if type(x) == QString:
11-
return unicode(x)
12+
return unicode(x, DEFAULT_ENCODING)
1213
return x
1314
else:
14-
return x # py3
15+
return x

libs/yolo_io.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from xml.etree.ElementTree import Element, SubElement
77
from lxml import etree
88
import codecs
9+
from libs.constants import DEFAULT_ENCODING
910

1011
TXT_EXT = '.txt'
11-
ENCODE_METHOD = 'utf-8'
12+
ENCODE_METHOD = DEFAULT_ENCODING
1213

1314
class YOLOWriter:
1415

@@ -39,7 +40,12 @@ def BndBox2YoloLine(self, box, classList=[]):
3940
w = float((xmax - xmin)) / self.imgSize[1]
4041
h = float((ymax - ymin)) / self.imgSize[0]
4142

42-
classIndex = classList.index(box['name'])
43+
# PR387
44+
boxName = box['name']
45+
if boxName not in classList:
46+
classList.append(boxName)
47+
48+
classIndex = classList.index(boxName)
4349

4450
return classIndex, xcen, ycen, w, h
4551

@@ -62,11 +68,11 @@ def save(self, classList=[], targetFile=None):
6268

6369
for box in self.boxlist:
6470
classIndex, xcen, ycen, w, h = self.BndBox2YoloLine(box, classList)
65-
print (classIndex, xcen, ycen, w, h)
71+
# print (classIndex, xcen, ycen, w, h)
6672
out_file.write("%d %.6f %.6f %.6f %.6f\n" % (classIndex, xcen, ycen, w, h))
6773

68-
print (classList)
69-
print (out_class_file)
74+
# print (classList)
75+
# print (out_class_file)
7076
for c in classList:
7177
out_class_file.write(c+'\n')
7278

@@ -89,12 +95,12 @@ def __init__(self, filepath, image, classListPath=None):
8995
else:
9096
self.classListPath = classListPath
9197

92-
print (filepath, self.classListPath)
98+
# print (filepath, self.classListPath)
9399

94100
classesFile = open(self.classListPath, 'r')
95101
self.classes = classesFile.read().strip('\n').split('\n')
96102

97-
print (self.classes)
103+
# print (self.classes)
98104

99105
imgSize = [image.height(), image.width(),
100106
1 if image.isGrayscale() else 3]

0 commit comments

Comments
 (0)