Skip to content

Commit

Permalink
Fix problem saving to output directory
Browse files Browse the repository at this point in the history
When images were open using a full filepath (either specified from the command line or from the dialogue that pops up in response to the "Open" or "Open Dir" QT buttons), they were not saved in the location that the user specified. Instead, they were being saved in the same directory as the image. This bug was caused by how Python's os.path.join() function handles full file paths given as the second argument. For example, os.path.join('first/path', '/second/full/path') will just return '/second/full/path'.

To recreate this problem, run "labelme --output path/to/output/directory --autosave /full/path/to/image.jpg" without the fix. Create a polygon on the image and click save. Notice that the save dialog opens up to "/full/path/to/". Even if you go to File->Change Output Dir and select a directory, the save dialog still opens up to "/full/path/to/". If you apply this fix and then repeat these steps, now the save dialog opens up to "--output path/to/output/directory". The same problem occurred when the "--autosave" flag was used. But this fix corrects this problem as well.
  • Loading branch information
akindofyoga authored and wkentaro committed Mar 21, 2019
1 parent 90ccde1 commit 3b0bbcf
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,8 @@ def setDirty(self):
if self._config['auto_save'] or self.actions.saveAuto.isChecked():
label_file = osp.splitext(self.imagePath)[0] + '.json'
if self.output_dir:
label_file = osp.join(self.output_dir, label_file)
label_file_without_path = osp.basename(label_file)
label_file = osp.join(self.output_dir, label_file_without_path)
self.saveLabels(label_file)
return
self.dirty = True
Expand Down Expand Up @@ -1120,7 +1121,8 @@ def loadFile(self, filename=None):
self.status("Loading %s..." % osp.basename(str(filename)))
label_file = osp.splitext(filename)[0] + '.json'
if self.output_dir:
label_file = osp.join(self.output_dir, label_file)
label_file_without_path = osp.basename(label_file)
label_file = osp.join(self.output_dir, label_file_without_path)
if QtCore.QFile.exists(label_file) and \
LabelFile.is_label_file(label_file):
try:
Expand Down Expand Up @@ -1369,7 +1371,7 @@ def saveFileDialog(self):
dlg.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
dlg.setOption(QtWidgets.QFileDialog.DontConfirmOverwrite, False)
dlg.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, False)
basename = osp.splitext(self.filename)[0]
basename = osp.basename(osp.splitext(self.filename)[0])
if self.output_dir:
default_labelfile_name = osp.join(
self.output_dir, basename + LabelFile.suffix
Expand Down Expand Up @@ -1566,7 +1568,8 @@ def importDirImages(self, dirpath, pattern=None, load=True):
continue
label_file = osp.splitext(filename)[0] + '.json'
if self.output_dir:
label_file = osp.join(self.output_dir, label_file)
label_file_without_path = osp.basename(label_file)
label_file = osp.join(self.output_dir, label_file_without_path)
item = QtWidgets.QListWidgetItem(filename)
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
if QtCore.QFile.exists(label_file) and \
Expand Down

0 comments on commit 3b0bbcf

Please sign in to comment.