python - 上传从 cv2.imdecode() 返回的图像时遇到问题

标签 python numpy opencv cv2

我正在尝试将 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:

最佳答案

您在这里传递的类型不正确。 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 之前将其编码回某种有效类型。

关于python - 上传从 cv2.imdecode() 返回的图像时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839470/

相关文章:

python - 使用 Cython 在 numpy 网格上评估 C 函数

opencv - OpenCV:使用OpenCL在GPU中分配内存

opencv - openCV C++中的视差图代码

image - GUI,可在ubuntu12.04中使用OpenCV查看图像中的值

python - 如何从 MZ 可执行文件中提取 Unicode 字符序列?

python - 归一化:numpy 数组与一个点之间的欧几里德距离

python - Pandas 绘图栏 : Show every nth xlabel

python - 如何从 Python 中的语法中去除 yield 的糖分?

python - pygame Sprite 和绘制方法未按预期工作

python - 计算 Pandas 数据框选定列的加权和的推荐方法是什么?