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

Use loguru #1523

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
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
43 changes: 37 additions & 6 deletions labelme/__main__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
import argparse
import codecs
import logging
import os
import os.path as osp
import sys

import yaml
from loguru import logger
from qtpy import QtCore
from qtpy import QtWidgets

from labelme import __appname__
from labelme import __version__
from labelme.app import MainWindow
from labelme.config import get_config
from labelme.logger import logger
from labelme.utils import newIcon


def _setup_loguru(logger_level: str) -> None:
try:
logger.remove(handler_id=0)
except ValueError:
pass

if sys.stderr:
logger.add(sys.stderr, level=logger_level)

cache_dir: str
if os.name == "nt":
cache_dir = os.path.join(os.environ["LOCALAPPDATA"], "labelme")
else:
cache_dir = os.path.expanduser("~/.cache/labelme")

os.makedirs(cache_dir, exist_ok=True)

log_file = os.path.join(cache_dir, "labelme.log")
logger.add(
log_file,
colorize=True,
level="DEBUG",
rotation="10 MB",
retention="30 days",
compression="gz",
enqueue=True,
backtrace=True,
diagnose=True,
)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--version", "-V", action="store_true", help="show version")
Expand Down Expand Up @@ -109,7 +139,7 @@ def main():
print("{0} {1}".format(__appname__, __version__))
sys.exit(0)

logger.setLevel(getattr(logging, args.logger_level.upper()))
_setup_loguru(logger_level=args.logger_level.upper())

if hasattr(args, "flags"):
if os.path.isfile(args.flags):
Expand Down Expand Up @@ -177,9 +207,10 @@ def main():
win.settings.clear()
sys.exit(0)

win.show()
win.raise_()
sys.exit(app.exec_())
with logger.catch():
win.show()
win.raise_()
sys.exit(app.exec_())


# this main block is required to generate executable by pyinstaller
Expand Down
3 changes: 1 addition & 2 deletions labelme/ai/_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import imgviz
import numpy as np
import skimage

from labelme.logger import logger
from loguru import logger


def _get_contour_length(contour):
Expand Down
2 changes: 1 addition & 1 deletion labelme/ai/efficient_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import numpy as np
import onnxruntime
import skimage
from loguru import logger

from ..logger import logger
from . import _utils


Expand Down
2 changes: 1 addition & 1 deletion labelme/ai/segment_anything_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import numpy as np
import onnxruntime
import skimage
from loguru import logger

from ..logger import logger
from . import _utils


Expand Down
3 changes: 1 addition & 2 deletions labelme/ai/text_to_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import numpy as np
import osam

from labelme.logger import logger
from loguru import logger


def get_rectangles_from_texts(
Expand Down
2 changes: 1 addition & 1 deletion labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import imgviz
import natsort
import numpy as np
from loguru import logger
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets
Expand All @@ -23,7 +24,6 @@
from labelme.config import get_config
from labelme.label_file import LabelFile
from labelme.label_file import LabelFileError
from labelme.logger import logger
from labelme.shape import Shape
from labelme.widgets import AiPromptWidget
from labelme.widgets import BrightnessContrastDialog
Expand Down
3 changes: 1 addition & 2 deletions labelme/cli/draw_label_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import imgviz
import matplotlib.pyplot as plt
import numpy as np

from labelme.logger import logger
from loguru import logger


def main():
Expand Down
2 changes: 1 addition & 1 deletion labelme/cli/export_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import imgviz
import PIL.Image
from loguru import logger

from labelme import utils
from labelme.logger import logger


def main():
Expand Down
2 changes: 1 addition & 1 deletion labelme/cli/json_to_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import imgviz
import PIL.Image
from loguru import logger

from labelme import utils
from labelme.logger import logger


def main():
Expand Down
3 changes: 1 addition & 2 deletions labelme/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import shutil

import yaml

from labelme.logger import logger
from loguru import logger

here = osp.dirname(osp.abspath(__file__))

Expand Down
2 changes: 1 addition & 1 deletion labelme/label_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import os.path as osp

import PIL.Image
from loguru import logger

from labelme import PY2
from labelme import QT4
from labelme import __version__
from labelme import utils
from labelme.logger import logger

PIL.Image.MAX_IMAGE_PIXELS = None

Expand Down
62 changes: 0 additions & 62 deletions labelme/logger.py

This file was deleted.

2 changes: 1 addition & 1 deletion labelme/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import numpy as np
import skimage.measure
from loguru import logger
from qtpy import QtCore
from qtpy import QtGui

import labelme.utils
from labelme.logger import logger

# TODO(unknown):
# - [opt] Store paths instead of creating new ones at each paint.
Expand Down
3 changes: 1 addition & 2 deletions labelme/utils/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import numpy as np
import PIL.Image
import PIL.ImageDraw

from labelme.logger import logger
from loguru import logger


def polygons_to_mask(img_shape, polygons, shape_type=None):
Expand Down
17 changes: 15 additions & 2 deletions labelme/widgets/canvas.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import contextlib

import imgviz
from loguru import logger
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets

import labelme.ai
import labelme.utils
from labelme import QT5
from labelme.logger import logger
from labelme.shape import Shape

# TODO(unknown):
Expand Down Expand Up @@ -137,7 +139,18 @@ def initializeAiModel(self, name):
logger.debug("AI model is already initialized: %r" % model.name)
else:
logger.debug("Initializing AI model: %r" % model.name)
self._ai_model = model()

class LoggerIO:
def write(self, message: str):
if message := message.strip():
logger.debug(message)

def flush(self):
pass

# NOTE: gdown.download uses sys.stderr, so redirect it to logger.debug
with contextlib.redirect_stderr(new_target=LoggerIO()):
self._ai_model = model()

if self.pixmap is None:
logger.warning("Pixmap is not set yet")
Expand Down
2 changes: 1 addition & 1 deletion labelme/widgets/label_dialog.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re

from loguru import logger
from qtpy import QT_VERSION
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets

import labelme.utils
from labelme.logger import logger

QT5 = QT_VERSION[0] == "5"

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def get_install_requires():
install_requires = [
"gdown",
"imgviz>=1.7.5",
"loguru",
"matplotlib",
"natsort>=7.1.0",
"numpy",
Expand Down
Loading