Skip to content

Commit 279d2fc

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 616b3a1 commit 279d2fc

8 files changed

Lines changed: 449 additions & 1439 deletions

File tree

src/resolver_athena_client/client/athena_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ async def classify_single(
244244
correlation_id=correlation_id,
245245
encoding=request_encoding,
246246
data=processed_image.data,
247-
format=ImageFormat.IMAGE_FORMAT_RAW_UINT8,
247+
format=ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR,
248248
hashes=[
249249
ImageHash(
250250
value=hash_value,

src/resolver_athena_client/client/transformers/classification_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _create_classification_input(
5656
),
5757
data=image_data.data,
5858
encoding=self.request_encoding,
59-
format=ImageFormat.IMAGE_FORMAT_RAW_UINT8,
59+
format=ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR,
6060
)
6161

6262
@override

src/resolver_athena_client/client/transformers/core.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
_expected_raw_size = EXPECTED_WIDTH * EXPECTED_HEIGHT * 3
2020

2121

22-
def _is_raw_brg_expected_size(data: bytes) -> bool:
23-
"""Detect if data is already a raw BRG array of expected size."""
22+
def _is_raw_bgr_expected_size(data: bytes) -> bool:
23+
"""Detect if data is already a raw BGR array of expected size."""
2424
return len(data) == _expected_raw_size
2525

2626

@@ -37,7 +37,7 @@ async def resize_image(image_data: ImageData) -> ImageData:
3737

3838
def process_image() -> tuple[bytes, bool]:
3939
# Fast path for raw RGB arrays of correct size
40-
if _is_raw_brg_expected_size(image_data.data):
40+
if _is_raw_bgr_expected_size(image_data.data):
4141
return image_data.data, False # No transformation needed
4242

4343
# Try to load the image data directly
@@ -57,15 +57,15 @@ def process_image() -> tuple[bytes, bool]:
5757

5858
rgb_bytes = resized_image.tobytes()
5959

60-
# Convert RGB to BRG by swapping channels
61-
brg_bytes = bytearray(len(rgb_bytes))
60+
# Convert RGB to BGR by swapping channels
61+
bgr_bytes = bytearray(len(rgb_bytes))
6262

6363
for i in range(0, len(rgb_bytes), 3):
64-
brg_bytes[i] = rgb_bytes[i + 2]
65-
brg_bytes[i + 1] = rgb_bytes[i]
66-
brg_bytes[i + 2] = rgb_bytes[i + 1]
64+
bgr_bytes[i] = rgb_bytes[i + 2]
65+
bgr_bytes[i + 1] = rgb_bytes[i + 1]
66+
bgr_bytes[i + 2] = rgb_bytes[i]
6767

68-
return bytes(brg_bytes), True # Data was transformed
68+
return bytes(bgr_bytes), True # Data was transformed
6969

7070
# Use thread pool for CPU-intensive processing
7171
resized_bytes, was_transformed = await asyncio.to_thread(process_image)

tests/client/transformers/test_classification_input.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def test_classification_input_transform(
4949
) # Should be a non-empty string
5050
assert result.data == test_data.data
5151
assert result.encoding == RequestEncoding.REQUEST_ENCODING_BROTLI
52-
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8
52+
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR
5353

5454

5555
@pytest.mark.asyncio
@@ -69,7 +69,7 @@ async def test_classification_input_iteration(
6969
assert result.data == b"test1"
7070
assert result.affiliate == transformer_config.affiliate
7171
assert result.encoding == RequestEncoding.REQUEST_ENCODING_BROTLI
72-
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8
72+
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR
7373

7474
# Test second item
7575
result = await anext(transformer)
@@ -106,7 +106,7 @@ async def test_classification_input_empty(
106106
assert result.data == b""
107107
assert result.affiliate == transformer_config.affiliate
108108
assert result.encoding == RequestEncoding.REQUEST_ENCODING_BROTLI
109-
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8
109+
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR
110110

111111

112112
@pytest.mark.parametrize(
@@ -133,4 +133,4 @@ async def test_classification_input_encodings(
133133
test_data = ImageData(b"test")
134134
result = await transformer.transform(test_data)
135135
assert result.encoding == encoding
136-
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8
136+
assert result.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR

tests/test_classify_single.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def test_classify_single_success(
100100
assert call_args.affiliate == "test-affiliate"
101101
assert call_args.data == b"fake_image_bytes"
102102
assert call_args.encoding == RequestEncoding.REQUEST_ENCODING_UNCOMPRESSED
103-
assert call_args.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8
103+
assert call_args.format == ImageFormat.IMAGE_FORMAT_RAW_UINT8_BGR
104104
assert len(call_args.hashes) == 1
105105
assert call_args.hashes[0].type == HashType.HASH_TYPE_MD5
106106
assert len(call_args.hashes[0].value) > 0 # MD5 hash should be generated

tests/utils/image_generation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
from resolver_athena_client.client.models import ImageData
1212

13+
14+
def _rgb_to_bgr(raw_rgb: bytes) -> bytes:
15+
data = bytearray(raw_rgb)
16+
for i in range(0, len(data), 3):
17+
data[i], data[i + 2] = data[i + 2], data[i]
18+
return bytes(data)
19+
20+
1321
# Global cache for reusable objects and constants
1422
_image_cache: dict[
1523
tuple[int, int], tuple[Image.Image, ImageDraw.ImageDraw]
@@ -61,8 +69,8 @@ def create_random_image(
6169
x2, y2 = (width * 3) // 4, (height * 3) // 4
6270
draw.rectangle([x1, y1, x2, y2], fill=accent_color)
6371

64-
if img_format.upper() == "RAW_UINT8":
65-
return image.tobytes()
72+
if img_format.upper() in {"RAW_UINT8", "RAW_UINT8_BGR"}:
73+
return _rgb_to_bgr(image.tobytes())
6674

6775
# Convert to PNG bytes
6876
buffer = io.BytesIO()

0 commit comments

Comments
 (0)