Python: ValueError:数组的真值对于多个元素是模糊的。请使用a.any() 或 a.all()

1 投票
1 回答
4081 浏览
提问于 2025-04-18 06:42

这是我的代码:

for ts, blob in izip(ts_list, blobs):
    ts = simplecvimg.track("camshift", i, fgmask,b.boundingBox())
    print(ts)

这是我遇到的错误:

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

虽然我从这里了解到 ValueError: 一个包含多个元素的数组的真值是模糊的。请使用 a.any() 或 a.all() 为什么会出现这个错误,但我不知道在我的情况下该如何处理这个错误。任何帮助都将不胜感激!

1 个回答

0

要么 ts 要么 img 是一个 numpy 数组……所以它不知道 not <numpy.array> 这个意思。

你可能想要的是 if len(ts) > 0 and len(img) > 0,或者也可以用 if ts is not None 之类的判断。

>>> import numpy
>>> not numpy.array([1,2,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous.
 Use a.any() or a.all()

撰写回答