Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sahi/utils/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ def read_image_as_pil(image: Image.Image | str | np.ndarray, exif_fix: bool = Tr
else:
raise TypeError(f"image with shape: {image_sk.shape[3]} is not supported.")
elif isinstance(image, np.ndarray):
if image.shape[0] < 5: # image in CHW
image = image[:, :, ::-1]
# check if image is in CHW format (Channels, Height, Width)
# heuristic: 3 dimensions, first dim (channels) < 5, last dim (width) > 4
if image.ndim == 3 and image.shape[0] < 5: # image in CHW
if image.shape[2] > 4:
# convert CHW to HWC (Height, Width, Channels)
image = np.transpose(image, (1, 2, 0))
image_pil = Image.fromarray(image)
else:
raise TypeError("read image with 'pillow' using 'Image.open()'")
Expand Down