Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex filename search, os.path.normpath filenames to display paths correctly on Windows, Edit label on double click on shape in canvas #912

Closed
wants to merge 13 commits into from
Closed
27 changes: 23 additions & 4 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def __init__(
double_click=self._config["canvas"]["double_click"],
num_backups=self._config["canvas"]["num_backups"],
crosshair=self._config["canvas"]["crosshair"],
edit_label_method=self.editLabel,
)
self.canvas.zoomRequest.connect(self.zoomRequest)

Expand Down Expand Up @@ -446,17 +447,27 @@ def __init__(
hideAll = action(
self.tr("&Hide\nPolygons"),
functools.partial(self.togglePolygons, False),
shortcuts["hide_all_polygons"],
icon="eye",
tip=self.tr("Hide all polygons"),
enabled=False,
)
showAll = action(
self.tr("&Show\nPolygons"),
functools.partial(self.togglePolygons, True),
shortcuts["show_all_polygons"],
icon="eye",
tip=self.tr("Show all polygons"),
enabled=False,
)
toggleAll = action(
self.tr("&Toggle\nPolygons"),
functools.partial(self.togglePolygons, None),
shortcuts["toggle_all_polygons"],
icon="eye",
tip=self.tr("Toggle all polygons"),
enabled=False,
)

help = action(
self.tr("&Tutorial"),
Expand Down Expand Up @@ -677,7 +688,7 @@ def __init__(
editMode,
brightnessContrast,
),
onShapesPresent=(saveAs, hideAll, showAll),
onShapesPresent=(saveAs, hideAll, showAll, toggleAll),
)

self.canvas.vertexSelected.connect(self.actions.removePoint.setEnabled)
Expand Down Expand Up @@ -723,6 +734,7 @@ def __init__(
None,
hideAll,
showAll,
toggleAll,
None,
zoomIn,
zoomOut,
Expand Down Expand Up @@ -1534,8 +1546,11 @@ def brightnessContrast(self, value):
self.brightnessContrast_values[self.filename] = (brightness, contrast)

def togglePolygons(self, value):
flag = value
for item in self.labelList:
item.setCheckState(Qt.Checked if value else Qt.Unchecked)
if value is None:
flag = (item.checkState() == Qt.Unchecked)
item.setCheckState(Qt.Checked if flag else Qt.Unchecked)

def loadFile(self, filename=None):
"""Load the specified file, or the last opened file if None."""
Expand Down Expand Up @@ -2117,7 +2132,11 @@ def importDirImages(self, dirpath, pattern=None, load=True):
self.fileListWidget.clear()
for filename in self.scanAllImages(dirpath):
if pattern and pattern not in filename:
continue
try:
if not re.search(pattern, filename):
continue
except re.error:
pass
label_file = osp.splitext(filename)[0] + ".json"
if self.output_dir:
label_file_without_path = osp.basename(label_file)
Expand All @@ -2143,7 +2162,7 @@ def scanAllImages(self, folderPath):
for root, dirs, files in os.walk(folderPath):
for file in files:
if file.lower().endswith(tuple(extensions)):
relativePath = osp.join(root, file)
relativePath = os.path.normpath(osp.join(root, file))
images.append(relativePath)
images = natsort.os_sorted(images)
return images
4 changes: 4 additions & 0 deletions labelme/config/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ shortcuts:
edit_label: Ctrl+E
toggle_keep_prev_mode: Ctrl+P
remove_selected_point: [Meta+H, Backspace]

show_all_polygons: G
hide_all_polygons: H
toggle_all_polygons: T
11 changes: 11 additions & 0 deletions labelme/translate/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ Polygons</source>
<source>Show all polygons</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../app.py" line="329"/>
<source>&amp;Toggle
Polygons</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../app.py" line="329"/>
<source>Toggle all polygons</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../app.py" line="331"/>
<source>&amp;Tutorial</source>
Expand Down
11 changes: 11 additions & 0 deletions labelme/translate/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ Polygons</source>
<source>Show all polygons</source>
<translation>显示所有多边形</translation>
</message>
<message>
<location filename="../app.py" line="329"/>
<source>&amp;Toggle
Polygons</source>
<translation>开关多边形(&amp;S)</translation>
</message>
<message>
<location filename="../app.py" line="329"/>
<source>Toggle all polygons</source>
<translation>开关所有多边形</translation>
</message>
<message>
<location filename="../app.py" line="331"/>
<source>&amp;Tutorial</source>
Expand Down
28 changes: 18 additions & 10 deletions labelme/widgets/canvas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import gdown
from qtpy import QtCore
from qtpy import QtGui
Expand Down Expand Up @@ -40,7 +42,7 @@ class Canvas(QtWidgets.QWidget):

_fill_drawing = False

def __init__(self, *args, **kwargs):
def __init__(self, edit_label_method, *args, **kwargs):
self.epsilon = kwargs.pop("epsilon", 10.0)
self.double_click = kwargs.pop("double_click", "close")
if self.double_click not in [None, "close"]:
Expand Down Expand Up @@ -95,6 +97,8 @@ def __init__(self, *args, **kwargs):
self.hShapeIsSelected = False
self._painter = QtGui.QPainter()
self._cursor = CURSOR_DEFAULT
self._lastMouseReleaseTime = 0
self._editLabelMethod = edit_label_method
# Menus:
# 0: right-click without selection and dragging of shapes
# 1: right-click with selection and dragging of shapes
Expand Down Expand Up @@ -235,10 +239,7 @@ def selectedEdge(self):
def mouseMoveEvent(self, ev):
"""Update line with last point and current coordinates."""
try:
if QT5:
pos = self.transformPos(ev.localPos())
else:
pos = self.transformPos(ev.posF())
pos = self.getMousePos(ev)
except AttributeError:
return

Expand Down Expand Up @@ -409,13 +410,9 @@ def removeSelectedPoint(self):
self.movingShape = True # Save changes

def mousePressEvent(self, ev):
if QT5:
pos = self.transformPos(ev.localPos())
else:
pos = self.transformPos(ev.posF())
pos = self.getMousePos(ev)

is_shift_pressed = ev.modifiers() & QtCore.Qt.ShiftModifier

if ev.button() == QtCore.Qt.LeftButton:
if self.drawing():
if self.current:
Expand Down Expand Up @@ -500,7 +497,14 @@ def mousePressEvent(self, ev):
self.repaint()
self.prevPoint = pos

def getMousePos(self, ev):
return self.transformPos(ev.localPos() if QT5 else ev.posF())

def mouseReleaseEvent(self, ev):
tm = time.time()
is_double_click = (tm - self._lastMouseReleaseTime) < .4
self._lastMouseReleaseTime = tm

if ev.button() == QtCore.Qt.RightButton:
menu = self.menus[len(self.selectedShapesCopy) > 0]
self.restoreCursor()
Expand Down Expand Up @@ -533,6 +537,10 @@ def mouseReleaseEvent(self, ev):

self.movingShape = False

if is_double_click:
if ev.button() == QtCore.Qt.LeftButton and self.editing() and len(self.selectedShapes or []) == 1:
self._editLabelMethod()

def endMove(self, copy):
assert self.selectedShapes and self.selectedShapesCopy
assert len(self.selectedShapesCopy) == len(self.selectedShapes)
Expand Down