Explorar el Código

Fix problem saving to output directory

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.
Roger Iyengar hace 6 años
padre
commit
3b0bbcfec9
Se han modificado 1 ficheros con 7 adiciones y 4 borrados
  1. 7 4
      labelme/app.py

+ 7 - 4
labelme/app.py

@@ -660,7 +660,8 @@ class MainWindow(QtWidgets.QMainWindow):
         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
@@ -1120,7 +1121,8 @@ class MainWindow(QtWidgets.QMainWindow):
         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:
@@ -1369,7 +1371,7 @@ class MainWindow(QtWidgets.QMainWindow):
         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
@@ -1566,7 +1568,8 @@ class MainWindow(QtWidgets.QMainWindow):
                 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 \