【发布时间】:2018-10-16 15:53:49
【问题描述】:
我正在尝试将 cv2.imdecode 返回的图像上传到 cloudinary,但我遇到了这个错误。
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我的代码是这样的 -
def url_to_image(url):
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cloudinary_response = cloudinary.uploader.upload(image)
return cloudinary_response
它在 cloudinary.uploader.upload 行中给出错误。我无法理解 cv2.imdecode() 的返回类型,是否可以将其上传到 S3 BUCKET 或 cloudinary 等图像服务器。
Traceback -
ValueError at /api/v1/users/getstyledimages
The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
Request Method: POST
Request URL:
https://rhymella.mobikasa.net/api/v1/users/getstyledimages
Django Version: 2.0.3
Python Executable: /var/www/rhymella/rhymella/bin/python
Python Version: 3.5.2
Python Path: ['/var/www/rhymella',
'/var/www/rhymella/rhymella/lib/python35.zip',
'/var/www/rhymella/rhymella/lib/python3.5',
'/var/www/rhymella/rhymella/lib/python3.5/plat-x86_64-linux-gnu',
'/var/www/rhymella/rhymella/lib/python3.5/lib-dynload',
'/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/var/www/rhymella/rhymella/lib/python3.5/site-packages',
'/usr/local/lib/python3.5/dist-packages/']
Server time: Tue, 16 Oct 2018 15:58:09 +0000
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
'cloudinary',
'newadmin']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/django/core/handlers/base.py" in _get_response
128. response =
self.process_exception_by_middleware(e, request)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request,
*callback_args, **callback_kwargs)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/rest_framework/views.py" in dispatch
494. response = self.handle_exception(exc)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/rest_framework/views.py" in handle_exception
454. self.raise_uncaught_exception(exc)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/rest_framework/views.py" in dispatch
491. response = handler(request, *args, **kwargs)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/rest_framework/decorators.py" in handler
53. return func(*args, **kwargs)
File "/var/www/rhymella/api/views.py" in get_styled_images
6818. res =
style.add_artistic_style(each.styled_images)
File "/var/www/rhymella/filters/style.py" in add_artistic_style
21. cloudinary_response = cloudinary.uploader.upload(img)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/cloudinary/uploader.py" in upload
40. return call_api("upload", params, file=file, **options)
File "/var/www/rhymella/rhymella/lib/python3.5/site-
packages/cloudinary/uploader.py" in call_api
269. if file:
【问题讨论】:
-
你永远不会定义
img。这应该给你一个 NameError。请检查您的代码,并确保您问题中的代码与您实际运行的代码相同。还包括错误的完整回溯。回溯包含诊断错误的基本信息。 -
@HåkenLid 我收到值错误
-
在 cloudinary.uploader 行中,您使用名为
img的变量名称。所以要么这不是你运行的代码,要么是上一行导致的错误。 -
@HåkenLid 那是错误的。还添加了回溯。请立即提出建议