8000 Test polygon creation in test app py by wkentaro · Pull Request #1574 · wkentaro/labelme · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Test polygon creation in test app py #1574

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

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 64 additions & 40 deletions tests/labelme_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import tempfile

import pytest
from PyQt5.QtCore import QPoint
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QTimer
from pytestqt.qtbot import QtBot

import labelme.app
import labelme.config
Expand All @@ -12,7 +16,9 @@
data_dir = osp.join(here, "data")


def _win_show_and_wait_imageData(qtbot, win):
def _show_window_and_wait_for_imagedata(
qtbot: QtBot, win: labelme.app.MainWindow
) -> None:
win.show()

def check_imageData():
Expand All @@ -23,91 +29,109 @@ def check_imageData():


@pytest.mark.gui
def test_MainWindow_open(qtbot):
win = labelme.app.MainWindow()
def test_MainWindow_open(qtbot: QtBot) -> None:
win: labelme.app.MainWindow = labelme.app.MainWindow()
qtbot.addWidget(win)
win.show()
win.close()


@pytest.mark.gui
def test_MainWindow_open_img(qtbot):
img_file = osp.join(data_dir, "raw/2011_000003.jpg")
win = labelme.app.MainWindow(filename=img_file)
def test_MainWindow_open_img(qtbot: QtBot) -> None:
img_file: str = osp.join(data_dir, "raw/2011_000003.jpg")
win: labelme.app.MainWindow = labelme.app.MainWindow(filename=img_file)
qtbot.addWidget(win)
_win_show_and_wait_imageData(qtbot, win)
_show_window_and_wait_for_imagedata(qtbot=qtbot, win=win)
win.close()


@pytest.mark.gui
def test_MainWindow_open_json(qtbot):
json_files = [
def test_MainWindow_open_json(qtbot: QtBot):
json_files: list[str] = [
osp.join(data_dir, "annotated_with_data/apc2016_obj3.json"),
osp.join(data_dir, "annotated/2011_000003.json"),
]
json_file: str
for json_file in json_files:
labelme.testing.assert_labelfile_sanity(json_file)

win = labelme.app.MainWindow(filename=json_file)
win: labelme.app.MainWindow = labelme.app.MainWindow(filename=json_file)
qtbot.addWidget(win)
_win_show_and_wait_imageData(qtbot, win)
_show_window_and_wait_for_imagedata(qtbot=qtbot, win=win)
win.close()


def create_MainWindow_with_directory(qtbot):
directory = osp.join(data_dir, "raw")
win = labelme.app.MainWindow(filename=directory)
def _create_window_with_directory(qtbot: QtBot) -> labelme.app.MainWindow:
directory: str = osp.join(data_dir, "raw")
win: labelme.app.MainWindow = labelme.app.MainWindow(filename=directory)
qtbot.addWidget(win)
_win_show_and_wait_imageData(qtbot, win)
_show_window_and_wait_for_imagedata(qtbot=qtbot, win=win)
return win


@pytest.mark.gui
def test_MainWindow_openNextImg(qtbot):
win = create_MainWindow_with_directory(qtbot)
def test_MainWindow_openNextImg(qtbot: QtBot) -> None:
win: labelme.app.MainWindow = _create_window_with_directory(qtbot=qtbot)
win.openNextImg()


@pytest.mark.gui
def test_MainWindow_openPrevImg(qtbot):
win = create_MainWindow_with_directory(qtbot)
def test_MainWindow_openPrevImg(qtbot: QtBot) -> None:
win: labelme.app.MainWindow = _create_window_with_directory(qtbot=qtbot)
win.openNextImg()


@pytest.mark.gui
def test_MainWindow_annotate_jpg(qtbot):
tmp_dir = tempfile.mkdtemp()
input_file = osp.join(data_dir, "raw/2011_000003.jpg")
out_file = osp.join(tmp_dir, "2011_000003.json")
def test_MainWindow_annotate_jpg(qtbot: QtBot) -> None:
tmp_dir: str = tempfile.mkdtemp()
input_file: str = osp.join(data_dir, "raw/2011_000003.jpg")
out_file: str = osp.join(tmp_dir, "2011_000003.json")

config = labelme.config.get_default_config()
win = labelme.app.MainWindow(
config: dict = labelme.config.get_default_config()
win: labelme.app.MainWindow = labelme.app.MainWindow(
config=config,
filename=input_file,
output_file=out_file,
)
qtbot.addWidget(win)
_win_show_and_wait_imageData(qtbot, win)
_show_window_and_wait_for_imagedata(qtbot=qtbot, win=win)

label = "whole"
points = [
label: str = "whole"
points: list[tuple[float, float]] = [
(100, 100),
(100, 238),
(400, 238),
(400, 100),
]
shapes = [
dict(
label=label,
group_id=None,
points=points,
shape_type="polygon",
mask=None,
flags={},
other_data={},
)
]
win.loadLabels(shapes)
win.toggleDrawMode(edit=False, createMode="polygon")
qtbot.wait(100)

def click(xy: tuple[float, float]) -> None:
qtbot.mouseMove(win.canvas, pos=QPoint(*xy))
qtbot.wait(100)
qtbot.mousePress(win.canvas, Qt.LeftButton, pos=QPoint(*xy))
qtbot.wait(100)

[click(xy=xy) for xy in points]

def interact() -> None:
qtbot.keyClicks(win.labelDialog.edit, label)
qtbot.wait(100)
qtbot.keyClick(win.labelDialog.edit, Qt.Key_Enter)
qtbot.wait(100)

QTimer.singleShot(300, interact)

click(xy=points[0])

assert len(win.canvas.shapes) == 1
assert len(win.canvas.shapes[0].points) == 4
assert win.canvas.shapes[0].label == "whole"
assert win.canvas.shapes[0].shape_type == "polygon"
assert win.canvas.shapes[0].group_id is None
assert win.canvas.shapes[0].mask is None
assert win.canvas.shapes[0].flags == {}

win.saveFile()

labelme.testing.assert_labelfile_sanity(out_file)
Expand Down
0