Skip to content

Commit 44ebbd0

Browse files
authored
fix: add various fixes regarding image processing (#461)
* fix: remove unused function from utils.py * fix: suppress warnings in tests by using the right API version * fix: convert image to RGB if needed in object detection pipeline * chore: bump openfoodfacts version in uv.lock
1 parent 1e1c2e0 commit 44ebbd0

File tree

5 files changed

+21
-60
lines changed

5 files changed

+21
-60
lines changed

src/openfoodfacts/ml/object_detection.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dataclasses
22
import logging
33
import typing
4+
import warnings
45

56
import albumentations as A
67
import cv2
@@ -51,6 +52,7 @@ def object_detection_transform(
5152
position=pad_position,
5253
fill=fill,
5354
),
55+
A.ToRGB(p=1.0),
5456
A.Normalize(mean=normalize_mean, std=normalize_std, p=1.0),
5557
],
5658
)
@@ -299,9 +301,12 @@ def detect_from_image(
299301

300302
def preprocess(self, image_array: np.ndarray) -> np.ndarray:
301303
# Apply the transform to the image
302-
image_array = object_detection_transform(image_size=self.image_size)(
303-
image=image_array
304-
)["image"]
304+
305+
with warnings.catch_warnings():
306+
warnings.filterwarnings("ignore", message="The image is already an RGB")
307+
image_array = object_detection_transform(image_size=self.image_size)(
308+
image=image_array
309+
)["image"]
305310
image_array = np.transpose(image_array, (2, 0, 1))[np.newaxis, :] # HWC to CHW
306311
return image_array
307312

src/openfoodfacts/ml/utils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
import numpy as np
21
from PIL import Image
32

43

5-
def convert_image_to_array(image: Image.Image) -> np.ndarray:
6-
"""Convert a PIL Image into a numpy array.
7-
8-
The image is converted to RGB if needed before generating the array.
9-
10-
:param image: the input image.
11-
:return: the generated numpy array of shape (width, height, 3)
12-
"""
13-
if image.mode != "RGB":
14-
image = image.convert("RGB")
15-
16-
im_width, im_height = image.size
17-
18-
return np.array(image.getdata(), dtype=np.uint8).reshape((im_height, im_width, 3))
19-
20-
214
def resize_image(image: Image.Image, max_size: tuple[int, int]) -> Image.Image:
225
"""Resize an image to fit within the specified dimensions.
236

tests/ml/test_utils.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,6 @@
1-
import numpy as np
21
from PIL import Image
32

4-
from openfoodfacts.ml.utils import convert_image_to_array, resize_image
5-
6-
7-
class TestConvertImageToArray:
8-
def test_rgb(self):
9-
# Create a simple RGB image
10-
image = Image.new("RGB", (10, 10), color="red")
11-
array = convert_image_to_array(image)
12-
13-
assert array.shape == (10, 10, 3)
14-
assert array.dtype == np.uint8
15-
assert (array == [255, 0, 0]).all()
16-
17-
def test_non_rgb(self):
18-
# Create a simple grayscale image
19-
image = Image.new("L", (10, 10), color=128)
20-
array = convert_image_to_array(image)
21-
22-
assert array.shape == (10, 10, 3)
23-
assert array.dtype == np.uint8
24-
assert (array == [128, 128, 128]).all()
25-
26-
def test_size(self):
27-
# Create a simple RGB image with different size
28-
image = Image.new("RGB", (20, 15), color="blue")
29-
array = convert_image_to_array(image)
30-
31-
assert array.shape == (15, 20, 3)
32-
assert array.dtype == np.uint8
33-
assert (array == [0, 0, 255]).all()
3+
from openfoodfacts.ml.utils import resize_image
344

355

366
class TestResizeImage:

tests/unit/test_api.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ def test_parse_ingredients_fail_non_HTTP_200(self):
182182

183183
def test_upload_image_success(self):
184184
api = openfoodfacts.API(
185-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
185+
user_agent=TEST_USER_AGENT,
186+
version="v3",
187+
username="test",
188+
password="test",
186189
)
187190
code = "1223435"
188191
response_data = {
@@ -228,7 +231,7 @@ def test_upload_image_success(self):
228231

229232
def test_upload_image_with_selected(self):
230233
api = openfoodfacts.API(
231-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
234+
user_agent=TEST_USER_AGENT, version="v3", username="test", password="test"
232235
)
233236
code = "1223435"
234237
response_data = {
@@ -291,7 +294,7 @@ def test_upload_image_with_selected(self):
291294
}
292295

293296
def test_upload_image_no_auth(self):
294-
api = openfoodfacts.API(user_agent=TEST_USER_AGENT, version="v2")
297+
api = openfoodfacts.API(user_agent=TEST_USER_AGENT, version="v3")
295298
code = "1223435"
296299
with pytest.raises(
297300
ValueError,
@@ -301,7 +304,7 @@ def test_upload_image_no_auth(self):
301304

302305
def test_upload_image_invalid_code(self):
303306
api = openfoodfacts.API(
304-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
307+
user_agent=TEST_USER_AGENT, version="v3", username="test", password="test"
305308
)
306309
code = "invalidcode"
307310
with pytest.raises(
@@ -312,7 +315,7 @@ def test_upload_image_invalid_code(self):
312315

313316
def test_upload_image_no_data(self):
314317
api = openfoodfacts.API(
315-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
318+
user_agent=TEST_USER_AGENT, version="v3", username="test", password="test"
316319
)
317320
code = "1223435"
318321
with pytest.raises(
@@ -323,7 +326,7 @@ def test_upload_image_no_data(self):
323326

324327
def test_upload_image_both_data(self):
325328
api = openfoodfacts.API(
326-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
329+
user_agent=TEST_USER_AGENT, version="v3", username="test", password="test"
327330
)
328331
code = "1223435"
329332
with pytest.raises(
@@ -336,7 +339,7 @@ def test_upload_image_both_data(self):
336339

337340
def test_upload_image_invalid_selected(self):
338341
api = openfoodfacts.API(
339-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
342+
user_agent=TEST_USER_AGENT, version="v3", username="test", password="test"
340343
)
341344
code = "1223435"
342345
with pytest.raises(
@@ -352,7 +355,7 @@ def test_upload_image_invalid_selected(self):
352355

353356
def test_upload_image_with_path(self, tmp_path):
354357
api = openfoodfacts.API(
355-
user_agent=TEST_USER_AGENT, version="v2", username="test", password="test"
358+
user_agent=TEST_USER_AGENT, version="v3", username="test", password="test"
356359
)
357360
code = "1223435"
358361
response_data = {

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)