【问题标题】:Facing problem in uploading image returned from cv2.imdecode()上传从 cv2.imdecode() 返回的图像时面临问题
【发布时间】: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 那是错误的。还添加了回溯。请立即提出建议

标签: python numpy opencv cv2


【解决方案1】:

您在这里传递的类型不正确。 upload 不接受 numpy 数组。 cv2 图像是 numpy.ndarray 对象。

cloudinary_response = cloudinary.uploader.upload(image)

参数可以是本地文件路径的字符串,也可以是url。例如,这将起作用。

def url_to_image(url):
    return cloudinary.uploader.upload(url)

其他选项请咨询cloudinary docs。如果要将图像转换为 numpy 数组,则必须先将其编码回某种有效类型,然后再将其传递给 cloudinary.uploader。

【讨论】:

  • 在上传到cloudinary之前,我必须对图像进行cv2操作
  • 很好。只需将其转换为图像文件并在处理后上传即可。这已经包含在我的回答中。您可以使用cv2.imwrite() 将numpy 数组保存为图像文件。
猜你喜欢
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
相关资源
最近更新 更多