Skip to content

Commit da1799d

Browse files
committed
Make code more modular, update tests and docs to reflect the change
1 parent b37535c commit da1799d

File tree

12 files changed

+565
-334
lines changed

12 files changed

+565
-334
lines changed

.github/workflows/black.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Format
1+
name: Black
22

33
on: [push, pull_request]
44

@@ -13,25 +13,25 @@ jobs:
1313
- uses: actions/checkout@v4
1414
with:
1515
fetch-depth: 0
16-
16+
1717
- name: Set up Python
1818
uses: actions/setup-python@v4
1919
with:
20-
python-version: '3.x'
21-
20+
python-version: "3.x"
21+
2222
- name: Install Black
2323
run: pip install black
24-
24+
2525
- name: Run Black
2626
id: black
2727
continue-on-error: true
2828
run: |
29-
black --check bbox_visualizer/bbox_visualizer.py tests/test_bbox_visualizer.py
29+
black --check bbox_visualizer tests
3030
echo "has_changes=$?" >> $GITHUB_OUTPUT
3131
3232
- name: Format with Black if needed
3333
if: steps.black.outputs.has_changes == '1'
34-
run: black bbox_visualizer/bbox_visualizer.py tests/test_bbox_visualizer.py
34+
run: black bbox_visualizer tests
3535

3636
- name: Create Pull Request
3737
if: steps.black.outputs.has_changes == '1'

bbox_visualizer/__init__.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
"""Top-level package for bbox-visualizer."""
1+
"""bbox-visualizer - Different ways of visualizing objects given bounding box data."""
22

33
from ._version import __version__
4+
from .core import (
5+
draw_rectangle,
6+
draw_multiple_rectangles,
7+
add_label,
8+
add_multiple_labels,
9+
add_T_label,
10+
add_multiple_T_labels,
11+
draw_flag_with_label,
12+
draw_multiple_flags_with_labels,
13+
)
414

5-
__author__ = """Shoumik Sharar Chowdhury"""
6-
__email__ = 'shoumikchow@gmail.com'
15+
__all__ = [
16+
"__version__",
17+
"draw_rectangle",
18+
"draw_multiple_rectangles",
19+
"add_label",
20+
"add_multiple_labels",
21+
"add_T_label",
22+
"add_multiple_T_labels",
23+
"draw_flag_with_label",
24+
"draw_multiple_flags_with_labels",
25+
]
726

8-
from .bbox_visualizer import (
9-
draw_rectangle,
10-
add_label,
11-
add_T_label,
12-
draw_flag_with_label,
13-
add_multiple_labels,
14-
add_multiple_T_labels,
15-
draw_multiple_rectangles,
16-
draw_multiple_flags_with_labels
17-
)
27+
__author__ = """Shoumik Sharar Chowdhury"""
28+
__email__ = "shoumikchow@gmail.com"

bbox_visualizer/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Version information."""
22

3-
__version__ = "0.2.0"
3+
__version__ = "0.2.0"

bbox_visualizer/core/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Core functionality for bbox-visualizer."""
2+
3+
from .rectangle import draw_rectangle, draw_multiple_rectangles
4+
from .labels import add_label, add_multiple_labels
5+
from .flags import (
6+
add_T_label,
7+
add_multiple_T_labels,
8+
draw_flag_with_label,
9+
draw_multiple_flags_with_labels,
10+
)
11+
12+
__all__ = [
13+
"draw_rectangle",
14+
"draw_multiple_rectangles",
15+
"add_label",
16+
"add_multiple_labels",
17+
"add_T_label",
18+
"add_multiple_T_labels",
19+
"draw_flag_with_label",
20+
"draw_multiple_flags_with_labels",
21+
]

bbox_visualizer/core/_utils.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Internal utilities for bbox-visualizer."""
2+
3+
from typing import List, Tuple
4+
5+
6+
def validate_bbox(bbox: List[int]) -> None:
7+
"""Validate bounding box format and values.
8+
9+
Args:
10+
bbox: List of [x_min, y_min, x_max, y_max] coordinates
11+
12+
Raises:
13+
ValueError: If bbox is empty, has wrong length, or has invalid coordinates
14+
"""
15+
if not bbox:
16+
raise ValueError("Bounding box cannot be empty")
17+
if len(bbox) != 4:
18+
raise ValueError(
19+
"Bounding box must have exactly 4 coordinates [x_min, y_min, x_max, y_max]"
20+
)
21+
if bbox[0] > bbox[2] or bbox[1] > bbox[3]:
22+
raise ValueError(
23+
"Invalid bounding box coordinates: x_min > x_max or y_min > y_max"
24+
)
25+
26+
27+
def validate_color(color: Tuple[int, int, int]) -> None:
28+
"""Validate BGR color values.
29+
30+
Args:
31+
color: BGR color tuple (blue, green, red)
32+
33+
Raises:
34+
ValueError: If any color component is outside [0, 255]
35+
"""
36+
if not all(0 <= c <= 255 for c in color):
37+
raise ValueError("Color values must be between 0 and 255")
38+
39+
40+
def _check_and_modify_bbox(
41+
bbox: List[int], img_size: Tuple[int, int, int], margin: int = 0
42+
) -> List[int]:
43+
"""Internal function to check and adjust bounding box coordinates.
44+
45+
.. private::
46+
47+
Trimming rules:
48+
- xmin/ymin: Set to margin if negative
49+
- xmax/ymax: Set to image width/height - margin if exceeds image size
50+
51+
Args:
52+
bbox: List of [x_min, y_min, x_max, y_max] coordinates
53+
img_size: Tuple of (height, width, channels)
54+
margin: Minimum distance from image edges (default: 0)
55+
56+
Returns:
57+
Adjusted bounding box coordinates [x_min, y_min, x_max, y_max]
58+
"""
59+
validate_bbox(bbox)
60+
bbox = [value if value > 0 else margin for value in bbox]
61+
bbox[2] = bbox[2] if bbox[2] < img_size[1] else img_size[1] - margin
62+
bbox[3] = bbox[3] if bbox[3] < img_size[0] else img_size[0] - margin
63+
return bbox

0 commit comments

Comments
 (0)