Skip to content

Commit bebce59

Browse files
committed
Add method and context manager to suppress warnings
1 parent 8709dc8 commit bebce59

File tree

6 files changed

+106
-7
lines changed

6 files changed

+106
-7
lines changed

bbox_visualizer/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
draw_multiple_flags_with_labels,
1111
draw_multiple_rectangles,
1212
draw_rectangle,
13+
suppress_warnings,
14+
warnings_suppressed,
1315
)
1416

1517
__all__ = [
@@ -22,6 +24,8 @@
2224
"draw_multiple_flags_with_labels",
2325
"draw_multiple_rectangles",
2426
"draw_rectangle",
27+
"suppress_warnings",
28+
"warnings_suppressed",
2529
]
2630

2731
__author__ = """Shoumik Sharar Chowdhury"""

bbox_visualizer/core/_utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
"""Internal utilities for bbox-visualizer."""
22

3+
import logging
4+
from contextlib import contextmanager
5+
from typing import List, Tuple
6+
7+
# Global flag to track warning suppression state
8+
_warnings_suppressed = False
9+
10+
def suppress_warnings(suppress: bool = True) -> None:
11+
"""Suppress or enable warning messages from bbox-visualizer.
12+
13+
Args:
14+
suppress: If True, suppress all warnings. If False, enable warnings.
15+
"""
16+
global _warnings_suppressed
17+
_warnings_suppressed = suppress
18+
19+
@contextmanager
20+
def warnings_suppressed():
21+
"""Context manager to temporarily suppress warnings.
22+
23+
Example:
24+
>>> with warnings_suppressed():
25+
... # Warnings will be suppressed in this block
26+
... pass
27+
"""
28+
previous_state = _warnings_suppressed
29+
suppress_warnings(True)
30+
try:
31+
yield
32+
finally:
33+
suppress_warnings(previous_state)
34+
35+
def _should_suppress_warning() -> bool:
36+
"""Internal function to check if warnings should be suppressed."""
37+
return _warnings_suppressed
38+
339
from typing import List, Tuple
440

541

bbox_visualizer/core/flags.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import cv2
77
import numpy as np
88

9-
from ._utils import _check_and_modify_bbox, _validate_color
9+
from ._utils import _check_and_modify_bbox, _validate_color, _should_suppress_warning
1010
from .labels import add_label
1111
from .rectangle import draw_rectangle
1212

@@ -59,9 +59,10 @@ def add_T_label(
5959
y_top = y_bottom - label_height - 2 * padding
6060

6161
if y_top < 0:
62-
logging.warning(
63-
"Labelling style 'T' going out of frame. Falling back to normal labeling."
64-
)
62+
if not _should_suppress_warning():
63+
logging.warning(
64+
"Labelling style 'T' going out of frame. Falling back to normal labeling."
65+
)
6566
return add_label(img, label, bbox)
6667

6768
cv2.line(img, (x_center, bbox[1]), (x_center, line_top), text_bg_color, 3)
@@ -139,9 +140,10 @@ def draw_flag_with_label(
139140
y_bottom = int(bbox[1] * 0.75 + bbox[3] * 0.25)
140141
y_top = bbox[1] - (y_bottom - bbox[1])
141142
if y_top < 0:
142-
logging.warning(
143-
"Labelling style 'Flag' going out of frame. Falling back to normal labeling."
144-
)
143+
if not _should_suppress_warning():
144+
logging.warning(
145+
"Labelling style 'Flag' going out of frame. Falling back to normal labeling."
146+
)
145147
img = draw_rectangle(img, bbox, bbox_color=line_color)
146148
return add_label(img, label, bbox)
147149

docs/api.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ API Reference
44
Core Functionality
55
----------------
66

7+
Warning Control
8+
~~~~~~~~~~~~~
9+
10+
.. automodule:: bbox_visualizer.core._utils
11+
:members: suppress_warnings, warnings_suppressed
12+
:undoc-members:
13+
:show-inheritance:
14+
:no-index:
15+
716
Rectangle Drawing
817
~~~~~~~~~~~~~~~
918

docs/usage.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ To use bbox-visualizer in a project:
1515
# Load an image
1616
image = cv2.imread('image.jpg')
1717
18+
Warning Control
19+
-------------
20+
21+
The library provides functionality to control warning messages:
22+
23+
.. code-block:: python
24+
25+
# Suppress all warnings
26+
bbv.suppress_warnings(True)
27+
28+
# Enable warnings
29+
bbv.suppress_warnings(False)
30+
31+
# Temporarily suppress warnings using context manager
32+
with bbv.warnings_suppressed():
33+
# Warnings will be suppressed in this block
34+
image = bbv.draw_flag_with_label(image, "Object", bbox)
35+
1836
Drawing Rectangles
1937
----------------
2038

tests/test_bbox_visualizer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from bbox_visualizer.core import flags, labels, rectangle
7+
from bbox_visualizer.core._utils import suppress_warnings, warnings_suppressed
78

89

910
@pytest.fixture
@@ -328,3 +329,32 @@ def test_color_parameters(sample_image, sample_bbox):
328329
flags.add_T_label(
329330
sample_image, "test", sample_bbox, text_bg_color=(0, 0, 300)
330331
) # Invalid RGB
332+
333+
334+
def test_warning_suppression(sample_image, sample_bbox, sample_label, caplog):
335+
"""Test warning suppression functionality."""
336+
# Test global warning suppression
337+
suppress_warnings(True)
338+
with caplog.at_level(logging.WARNING):
339+
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
340+
assert len(caplog.records) == 0 # No warnings should be logged
341+
342+
# Test warning re-enabling
343+
suppress_warnings(False)
344+
with caplog.at_level(logging.WARNING):
345+
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
346+
assert len(caplog.records) > 0 # Warnings should be logged
347+
assert "Labelling style 'Flag' going out of frame" in caplog.text
348+
349+
# Test context manager
350+
caplog.clear()
351+
with warnings_suppressed():
352+
with caplog.at_level(logging.WARNING):
353+
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
354+
assert len(caplog.records) == 0 # No warnings should be logged
355+
356+
# Test warning restoration after context manager
357+
with caplog.at_level(logging.WARNING):
358+
flags.draw_flag_with_label(sample_image, sample_label, [10, 0, 50, 10])
359+
assert len(caplog.records) > 0 # Warnings should be logged again
360+
assert "Labelling style 'Flag' going out of frame" in caplog.text

0 commit comments

Comments
 (0)