Create a const for encoding and fix an issue in yolo format according to pr#387

This commit is contained in:
tzutalin 2018-11-04 20:13:34 -08:00
parent 40ea8e7801
commit f563c164d0
5 changed files with 23 additions and 17 deletions

View File

@ -12,7 +12,7 @@ LabelImg is a graphical image annotation tool.
It is written in Python and uses Qt for its graphical interface. It is written in Python and uses Qt for its graphical interface.
Annotations are saved as XML files in PASCAL VOC format, the format used Annotations are saved as XML files in PASCAL VOC format, the format used
by `ImageNet <http://www.image-net.org/>`__. by `ImageNet <http://www.image-net.org/>`__. Besdies, it also supports YOLO format
.. image:: https://raw.githubusercontent.com/tzutalin/labelImg/master/demo/demo3.jpg .. image:: https://raw.githubusercontent.com/tzutalin/labelImg/master/demo/demo3.jpg
:alt: Demo Image :alt: Demo Image

1
libs/constants.py Executable file → Normal file
View File

@ -15,3 +15,4 @@ SETTING_SINGLE_CLASS = 'singleclass'
FORMAT_PASCALVOC='PascalVOC' FORMAT_PASCALVOC='PascalVOC'
FORMAT_YOLO='YOLO' FORMAT_YOLO='YOLO'
SETTING_DRAW_SQUARE = 'draw/square' SETTING_DRAW_SQUARE = 'draw/square'
DEFAULT_ENCODING = 'utf-8'

View File

@ -5,9 +5,10 @@ from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement from xml.etree.ElementTree import Element, SubElement
from lxml import etree from lxml import etree
import codecs import codecs
from libs.constants import DEFAULT_ENCODING
XML_EXT = '.xml' XML_EXT = '.xml'
ENCODE_METHOD = 'utf-8' ENCODE_METHOD = DEFAULT_ENCODING
class PascalVocWriter: class PascalVocWriter:
@ -84,10 +85,7 @@ class PascalVocWriter:
for each_object in self.boxlist: for each_object in self.boxlist:
object_item = SubElement(top, 'object') object_item = SubElement(top, 'object')
name = SubElement(object_item, 'name') name = SubElement(object_item, 'name')
try: print (each_object['name'])
name.text = unicode(each_object['name'])
except NameError:
# Py3: NameError: name 'unicode' is not defined
name.text = each_object['name'] name.text = each_object['name']
pose = SubElement(object_item, 'pose') pose = SubElement(object_item, 'pose')
pose.text = "Unspecified" pose.text = "Unspecified"

View File

@ -1,4 +1,5 @@
import sys import sys
from libs.constants import DEFAULT_ENCODING
def ustr(x): def ustr(x):
'''py2/py3 unicode helper''' '''py2/py3 unicode helper'''
@ -6,9 +7,9 @@ def ustr(x):
if sys.version_info < (3, 0, 0): if sys.version_info < (3, 0, 0):
from PyQt4.QtCore import QString from PyQt4.QtCore import QString
if type(x) == str: if type(x) == str:
return x.decode('utf-8') return x.decode(DEFAULT_ENCODING)
if type(x) == QString: if type(x) == QString:
return unicode(x) return unicode(x, DEFAULT_ENCODING)
return x return x
else: else:
return x # py3 return x

View File

@ -6,9 +6,10 @@ from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement from xml.etree.ElementTree import Element, SubElement
from lxml import etree from lxml import etree
import codecs import codecs
from libs.constants import DEFAULT_ENCODING
TXT_EXT = '.txt' TXT_EXT = '.txt'
ENCODE_METHOD = 'utf-8' ENCODE_METHOD = DEFAULT_ENCODING
class YOLOWriter: class YOLOWriter:
@ -39,7 +40,12 @@ class YOLOWriter:
w = float((xmax - xmin)) / self.imgSize[1] w = float((xmax - xmin)) / self.imgSize[1]
h = float((ymax - ymin)) / self.imgSize[0] h = float((ymax - ymin)) / self.imgSize[0]
classIndex = classList.index(box['name']) # PR387
boxName = box['name']
if boxName not in classList:
classList.append(boxName)
classIndex = classList.index(boxName)
return classIndex, xcen, ycen, w, h return classIndex, xcen, ycen, w, h
@ -62,11 +68,11 @@ class YOLOWriter:
for box in self.boxlist: for box in self.boxlist:
classIndex, xcen, ycen, w, h = self.BndBox2YoloLine(box, classList) classIndex, xcen, ycen, w, h = self.BndBox2YoloLine(box, classList)
print (classIndex, xcen, ycen, w, h) # print (classIndex, xcen, ycen, w, h)
out_file.write("%d %.6f %.6f %.6f %.6f\n" % (classIndex, xcen, ycen, w, h)) out_file.write("%d %.6f %.6f %.6f %.6f\n" % (classIndex, xcen, ycen, w, h))
print (classList) # print (classList)
print (out_class_file) # print (out_class_file)
for c in classList: for c in classList:
out_class_file.write(c+'\n') out_class_file.write(c+'\n')
@ -89,12 +95,12 @@ class YoloReader:
else: else:
self.classListPath = classListPath self.classListPath = classListPath
print (filepath, self.classListPath) # print (filepath, self.classListPath)
classesFile = open(self.classListPath, 'r') classesFile = open(self.classListPath, 'r')
self.classes = classesFile.read().strip('\n').split('\n') self.classes = classesFile.read().strip('\n').split('\n')
print (self.classes) # print (self.classes)
imgSize = [image.height(), image.width(), imgSize = [image.height(), image.width(),
1 if image.isGrayscale() else 3] 1 if image.isGrayscale() else 3]