Skip to content

Add method and context manager to suppress warnings #46

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 3 commits into from
Apr 29, 2025
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
5 changes: 5 additions & 0 deletions bbox_visualizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
draw_multiple_flags_with_labels,
draw_multiple_rectangles,
draw_rectangle,
suppress_warnings,
warnings_suppressed,
)
from .core._utils import suppress_warnings, warnings_suppressed

__all__ = [
"__version__",
Expand All @@ -22,6 +25,8 @@
"draw_multiple_flags_with_labels",
"draw_multiple_rectangles",
"draw_rectangle",
"suppress_warnings",
"warnings_suppressed",
]

__author__ = """Shoumik Sharar Chowdhury"""
Expand Down
3 changes: 3 additions & 0 deletions bbox_visualizer/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from .labels import add_label, add_multiple_labels
from .rectangle import draw_multiple_rectangles, draw_rectangle
from ._utils import suppress_warnings, warnings_suppressed

__all__ = [
"add_T_label",
Expand All @@ -18,4 +19,6 @@
"draw_multiple_flags_with_labels",
"draw_multiple_rectangles",
"draw_rectangle",
"suppress_warnings",
"warnings_suppressed",
]
36 changes: 36 additions & 0 deletions bbox_visualizer/core/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
"""Internal utilities for bbox-visualizer."""

import logging
from contextlib import contextmanager
from typing import List, Tuple

# Global flag to track warning suppression state
_warnings_suppressed = False

def suppress_warnings(suppress: bool = True) -> None:
"""Suppress or enable warning messages from bbox-visualizer.

Args:
suppress: If True, suppress all warnings. If False, enable warnings.
"""
global _warnings_suppressed
_warnings_suppressed = suppress

@contextmanager
def warnings_suppressed():
"""Context manager to temporarily suppress warnings.

Example:
>>> with warnings_suppressed():
... # Warnings will be suppressed in this block
... pass
"""
previous_state = _warnings_suppressed
suppress_warnings(True)
try:
yield
finally:
suppress_warnings(previous_state)

def _should_suppress_warning() -> bool:
"""Internal function to check if warnings should be suppressed."""
return _warnings_suppressed

from typing import List, Tuple


Expand Down
16 changes: 9 additions & 7 deletions bbox_visualizer/core/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cv2
import numpy as np

from ._utils import _check_and_modify_bbox, _validate_color
from ._utils import _check_and_modify_bbox, _validate_color, _should_suppress_warning
from .labels import add_label
from .rectangle import draw_rectangle

Expand Down Expand Up @@ -59,9 +59,10 @@ def add_T_label(
y_top = y_bottom - label_height - 2 * padding

if y_top < 0:
logging.warning(
"Labelling style 'T' going out of frame. Falling back to normal labeling."
)
if not _should_suppress_warning():
logging.warning(
"Labelling style 'T' going out of frame. Falling back to normal labeling."
)
return add_label(img, label, bbox)

cv2.line(img, (x_center, bbox[1]), (x_center, line_top), text_bg_color, 3)
Expand Down Expand Up @@ -139,9 +140,10 @@ def draw_flag_with_label(
y_bottom = int(bbox[1] * 0.75 + bbox[3] * 0.25)
y_top = bbox[1] - (y_bottom - bbox[1])
if y_top < 0:
logging.warning(
"Labelling style 'Flag' going out of frame. Falling back to normal labeling."
)
if not _should_suppress_warning():
logging.warning(
"Labelling style 'Flag' going out of frame. Falling back to normal labeling."
)
img = draw_rectangle(img, bbox, bbox_color=line_color)
return add_label(img, label, bbox)

Expand Down
9 changes: 9 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ API Reference
Core Functionality
----------------

Warning Control
~~~~~~~~~~~~~

.. automodule:: bbox_visualizer.core._utils
:members: suppress_warnings, warnings_suppressed
:undoc-members:
:show-inheritance:
:no-index:

Rectangle Drawing
~~~~~~~~~~~~~~~

Expand Down
18 changes: 18 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ To use bbox-visualizer in a project:
# Load an image
image = cv2.imread('image.jpg')

Warning Control
-------------

The library provides functionality to control warning messages:

.. code-block:: python

# Suppress all warnings
bbv.suppress_warnings(True)

# Enable warnings
bbv.suppress_warnings(False)

# Temporarily suppress warnings using context manager
with bbv.warnings_suppressed():
# Warnings will be suppressed in this block
image = bbv.draw_flag_with_label(image, "Object", bbox)

Drawing Rectangles
----------------

Expand Down
30 changes: 30 additions & 0 deletions tests/test_bbox_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from bbox_visualizer.core import flags, labels, rectangle
from bbox_visualizer.core._utils import suppress_warnings, warnings_suppressed


@pytest.fixture
Expand Down Expand Up @@ -328,3 +329,32 @@ def test_color_parameters(sample_image, sample_bbox):
flags.add_T_label(
sample_image, "test", sample_bbox, text_bg_color=(0, 0, 300)
) # Invalid RGB


def test_warning_suppression(sample_image, sample_bbox, sample_label, caplog):
"""Test warning suppression functionality."""
# Test global warning suppression
suppress_warnings(True)
with caplog.at_level(logging.WARNING):
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
assert len(caplog.records) == 0 # No warnings should be logged

# Test warning re-enabling
suppress_warnings(False)
with caplog.at_level(logging.WARNING):
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
assert len(caplog.records) > 0 # Warnings should be logged
assert "Labelling style 'Flag' going out of frame" in caplog.text

# Test context manager
caplog.clear()
with warnings_suppressed():
with caplog.at_level(logging.WARNING):
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
assert len(caplog.records) == 0 # No warnings should be logged

# Test warning restoration after context manager
with caplog.at_level(logging.WARNING):
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
assert len(caplog.records) > 0 # Warnings should be logged again
assert "Labelling style 'Flag' going out of frame" in caplog.text
Loading