Coverage for /home/ubuntu/cv-depot/python/cv_depot/core/tools.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-08 20:26 +0000

1from typing import Union # noqa F401 

2from numpy.typing import NDArray # noqa F401 

3# ------------------------------------------------------------------------------ 

4 

5 

6def get_channels_from_array(array): 

7 # type: (NDArray) -> list[Union[str, int]] 

8 ''' 

9 Returns a list of strings representing the given array's channels. 

10 If array has only one channel then ['l'] is returned. 

11 First 4 channels are [r, g, b, a], in that order. All subsequent channels 

12 are integers starting at 4. 

13 

14 Args: 

15 array (numpy.NDArray): Numpy array with 2+ dimensional shape. 

16 

17 Returns: 

18 list[str and int]: Channels. 

19 ''' 

20 if len(array.shape) < 3 or array.shape[2] == 1: 

21 return ['l'] 

22 else: 

23 temp = list(range(array.shape[2])) 

24 lut = {0: 'r', 1: 'g', 2: 'b', 3: 'a'} # type: dict[int, str] 

25 channels = [] # type: list[Union[str, int]] 

26 for i in temp: 

27 c = i # type: Union[int, str] 

28 if i in lut: 

29 c = lut[i] 

30 channels.append(c) 

31 return channels