Provide an extra argument to change predefined classes path

This commit is contained in:
tzutalin 2017-05-10 21:58:39 +08:00
parent a7e3a32385
commit 132492d61f
2 changed files with 17 additions and 13 deletions

View File

@ -46,6 +46,7 @@ Ubuntu Linux
sudo pip install lxml sudo pip install lxml
make all make all
./labelImg.py ./labelImg.py
./labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
OS X OS X
^^^^ ^^^^
@ -56,6 +57,7 @@ OS X
brew install libxml2 brew install libxml2
make all make all
./labelImg.py ./labelImg.py
./labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
Windows Windows
^^^^^^^ ^^^^^^^
@ -71,12 +73,15 @@ Open cmd and go to `labelImg <#labelimg>`__ directory
pyrcc4 -o resources.py resources.qrc pyrcc4 -o resources.py resources.qrc
python labelImg.py python labelImg.py
python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
Get from PyPI Get from PyPI
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
.. code:: .. code::
pip install labelimg pip install labelImg
labelImg
labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
I tested pip on Ubuntu14.04 and 16.04. However, I didn't test pip on MacOS and windows I tested pip on Ubuntu14.04 and 16.04. However, I didn't test pip on MacOS and windows

View File

@ -87,14 +87,12 @@ class HashableQListWidgetItem(QListWidgetItem):
class MainWindow(QMainWindow, WindowMixin): class MainWindow(QMainWindow, WindowMixin):
FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = list(range(3)) FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = list(range(3))
def __init__(self, defaultFilename=None): def __init__(self, defaultFilename=None, defaultPrefdefClassFile=None):
super(MainWindow, self).__init__() super(MainWindow, self).__init__()
self.setWindowTitle(__appname__) self.setWindowTitle(__appname__)
# Save as Pascal voc xml # Save as Pascal voc xml
self.defaultSaveDir = None self.defaultSaveDir = None
self.usingPascalVocFormat = True self.usingPascalVocFormat = True
if self.usingPascalVocFormat:
LabelFile.suffix = '.xml'
# For loading all image under a directory # For loading all image under a directory
self.mImgList = [] self.mImgList = []
self.dirname = None self.dirname = None
@ -111,7 +109,6 @@ class MainWindow(QMainWindow, WindowMixin):
self.screencastViewer = "firefox" self.screencastViewer = "firefox"
self.screencast = "https://youtu.be/p0nR2YsCY_U" self.screencast = "https://youtu.be/p0nR2YsCY_U"
self.loadPredefinedClasses()
# Main widgets and related state. # Main widgets and related state.
self.labelDialog = LabelDialog(parent=self, listItem=self.labelHist) self.labelDialog = LabelDialog(parent=self, listItem=self.labelHist)
self.labelList = QListWidget() self.labelList = QListWidget()
@ -379,6 +376,8 @@ class MainWindow(QMainWindow, WindowMixin):
# Add Chris # Add Chris
self.difficult = False self.difficult = False
# Load predefined classes to the list
self.loadPredefinedClasses(defaultPrefdefClassFile)
# XXX: Could be completely declarative. # XXX: Could be completely declarative.
# Restore application settings. # Restore application settings.
if have_qstring(): if have_qstring():
@ -713,8 +712,7 @@ class MainWindow(QMainWindow, WindowMixin):
# Can add differrent annotation formats here # Can add differrent annotation formats here
try: try:
if self.usingPascalVocFormat is True: if self.usingPascalVocFormat is True:
print ('Img: ' + self.filePath + print ('Img: ' + self.filePath + ' -> Its xml: ' + annotationFilePath)
' -> Its xml: ' + annotationFilePath)
self.labelFile.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData, self.labelFile.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData,
self.lineColor.getRgb(), self.fillColor.getRgb()) self.lineColor.getRgb(), self.fillColor.getRgb())
else: else:
@ -1203,11 +1201,9 @@ class MainWindow(QMainWindow, WindowMixin):
self.canvas.endMove(copy=False) self.canvas.endMove(copy=False)
self.setDirty() self.setDirty()
def loadPredefinedClasses(self): def loadPredefinedClasses(self, predefClassesFile):
predefined_classes_path = os.path.join( if os.path.exists(predefClassesFile) is True:
'data', 'predefined_classes.txt') with codecs.open(predefClassesFile, 'r', 'utf8') as f:
if os.path.exists(predefined_classes_path) is True:
with codecs.open(predefined_classes_path, 'r', 'utf8') as f:
for line in f: for line in f:
line = line.strip() line = line.strip()
if self.labelHist is None: if self.labelHist is None:
@ -1282,7 +1278,10 @@ def get_main_app(argv=[]):
app = QApplication(argv) app = QApplication(argv)
app.setApplicationName(__appname__) app.setApplicationName(__appname__)
app.setWindowIcon(newIcon("app")) app.setWindowIcon(newIcon("app"))
win = MainWindow(argv[1] if len(argv) == 2 else None) # Tzutalin 201705+: Accept extra agruments to change predefined class file
# Usage : labelImg.py image predefClassFile
win = MainWindow(argv[1] if len(argv) >= 2 else None,
argv[2] if len(argv) >= 3 else os.path.join('data', 'predefined_classes.txt'))
win.show() win.show()
return app, win return app, win