Skip to content

Commit 852286e

Browse files
committed
Automatically convert images to RGB color space
Inference only works in RGB, so RGBA and grayscale images need to be transformed first. Output is always RGB, regardless of input format.
1 parent edfe190 commit 852286e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

deface/centerface.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
default_onnx_path = f'{os.path.dirname(__file__)}/centerface.onnx'
1010

1111

12+
def ensure_rgb(img: np.ndarray) -> np.ndarray:
13+
"""Convert input image to RGB if it is in RGBA or L format"""
14+
if img.ndim == 2: # 1-channel grayscale -> RGB
15+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
16+
elif img.shape[2] == 4: # 4-channel RGBA -> RGB
17+
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
18+
return img
19+
20+
1221
class CenterFace:
1322
def __init__(self, onnx_path=None, in_shape=None, backend='auto'):
1423
self.in_shape = in_shape
@@ -71,6 +80,7 @@ def dynamicize_shapes(static_model):
7180
return dyn_model
7281

7382
def __call__(self, img, threshold=0.5):
83+
img = ensure_rgb(img)
7484
self.orig_shape = img.shape[:2]
7585
if self.in_shape is None:
7686
self.in_shape = self.orig_shape[::-1]

0 commit comments

Comments
 (0)