failure to decode an io object as an image raises an exception

This commit is contained in:
Samuel J. Woodward 2025-01-10 09:14:15 -05:00
parent f9af73c1c7
commit 242bd3eb84
2 changed files with 7 additions and 0 deletions

View File

@ -146,6 +146,8 @@ def load_image_from_io_object(obj: IO[bytes]) -> np.ndarray:
try: try:
nparr = np.frombuffer(obj.read(), np.uint8) nparr = np.frombuffer(obj.read(), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
raise ValueError("Failed to decode image")
return img return img
finally: finally:
if not seekable: if not seekable:

View File

@ -1,6 +1,7 @@
# built-in dependencies # built-in dependencies
import io import io
import cv2 import cv2
import pytest
# project dependencies # project dependencies
from deepface import DeepFace from deepface import DeepFace
@ -31,6 +32,10 @@ def test_standard_represent_with_io_object():
no_seek_io_embedding_objs = DeepFace.represent(io_obj) no_seek_io_embedding_objs = DeepFace.represent(io_obj)
assert default_embedding_objs == no_seek_io_embedding_objs assert default_embedding_objs == no_seek_io_embedding_objs
# Confirm non-image io objects raise exceptions
with pytest.raises(ValueError, match='Failed to decode image'):
DeepFace.represent(io.BytesIO(open(__file__, 'rb').read()))
logger.info("✅ test standard represent with io object function done") logger.info("✅ test standard represent with io object function done")