0

I'm going to make a scissors paper rock games using tensorflow, so i wanna input an image and the output like 'paper', 'scissors', or 'rock' depend on the image, but i have a problem to do it => The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() , so How to fix it?, so pls n help me

this is mycode

import numpy as np
from google.colab import files
from keras.preprocessing import image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

uploaded = files.upload()

for fn in uploaded.keys():

  # predicting images
  path = fn
  img = image.load_img(path, target_size=(150,150))
  imgplot = plt.imshow(img)
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)

  print(fn)
  if classes==0:
    print('paper')
  elif classes==1:
    print('scissors')
  else:
    print('rock')

in the end the error like this

    ValueError                                Traceback (most recent call last)
<ipython-input-69-ca5bd193fb08> in <module>()
     23 
     24   print(fn)
---> 25   if classes==0:
     26     print('paper')
     27   elif classes==1:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

guide me in my beginner guys plss Thx

  • When in doubt, print it! Could you please try to print classes i.e. print(classes)? Now, is it a single value like 1 or 2 or -1 or is it an array like (i.e. list())? – Sowmya May 05 '20 at 04:42
  • ` for class in classes: if class==0: print('paper') elif class==1: print('scissors') else: print('rock') ` – Sowmya May 05 '20 at 04:47
  • 1
    Truth value testing in arrays in `numpy` is a commonly asked question on S/O, check out: https://stackoverflow.com/questions/12647471/the-truth-value-of-an-array-with-more-than-one-element-is-ambigous-when-trying-t https://stackoverflow.com/questions/33025855/valueerror-from-simple-numpy-comparison, https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous?rq=1 – Brayoni May 05 '20 at 05:32
  • i just print classes => print(classes) and it shows [[0. 1. 0.]] – Kholish May 05 '20 at 12:03
  • it's [[0. 1. 0.]] not [[0, 1, 0]] – Kholish May 05 '20 at 12:44
  • help me guys @Sowmya and others :) – Kholish May 05 '20 at 15:41
  • Impossible ` import numpy as np a = np.array([[0. 1. 0.]]) File "", line 3 a = np.array([[0. 1. 0.]]) ^ SyntaxError: invalid syntax ` – Sowmya May 05 '20 at 17:04
  • [[0. 1. 0.]] doesn't make sense – Sowmya May 05 '20 at 17:05
  • Do I really need to install `google-colab` to run your code? – Sowmya May 05 '20 at 17:32
  • I tried installing `google-colab` and now my jupyter is not working (`jupyter kernel seems to have died`). I am sorry. I am out of this. – Sowmya May 05 '20 at 18:03
  • @Sowmya maybe you can help me by checking my code here https://github.com/fauzanshobi/rock-paper-scissors/tree/master – Kholish May 05 '20 at 21:22
  • i've just update my code just print(classes) when i print(classes) the output is [[1. 0. 0.]] the output show the location of the inserted image, but does not show the location folder label like 'paper', 'scissors', or 'rock'. the output just like [[1. 0. 0.]], [[0. 1. 0.]], and [[0. 0. 1.]] – Kholish May 06 '20 at 15:12

0 Answers0