'How can I check That Every values of an numpy.ndarray are all the same in Python

Trying to make a basic hand-digit recognition AI and I just want the AI to not make a prediction when the canvas is empty, I basically tried everything I could but most of the time facing the "unhashable type: 'numpy.ndarray'" error. Found this solution that doesn't make an error but didn't work for me

canvas_result = st_canvas(
fill_color="rgba(255, 165, 0, 0.3)",  # Fixed fill color with some opacity
stroke_width=8,
stroke_color='#FFFFFF',
background_color='#000000',
height=250,
width=250,
drawing_mode='freedraw',
key="canvas",
)

array = canvas_result.image_data

if array is not None: # HERE

  if prediction is None:
      if streamlit is None:
          streamlit = resize(
            (np.dot(array[..., :3], [0.2989, 0.5870, 0.1140])) / 255, (28, 28))

      width, height, *_ = streamlit.shape
      prediction = streamlit.reshape(1, width, height, 1)
      prediction = prediction.astype('float32')

this gives me: canvas empty but still a prediction

I tried to convert my numpy dictionary to usefull values but didn't succeeded

EDIT: canvas_result.image_data is an object that looks like

([[[  0   0   0 255]
  [  0   0   0 255]
  [  0   0   0 255]
  ...
  [  0   0   0 255]
  [  0   0   0 255]
  [  0   0   0 255]]
 [[  0   0   0 255]
  [  0   0   0 255] ...)

describing the image as the rgb color and the possible range of the 3 color so I can't really check if they are all null as 255 is here

so I tried

if np.max(array[..., :3]) != np.min(array[..., :3]):

and it worked



Solution 1:[1]

Try

if np.all(array == 0):

or

if len(np.unique(array)) == 1:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 richardec