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

时间:2018-10-16 15:53:50

标签: python numpy opencv cv2

我正在尝试在cloudinary上上传cv2.imdecode返回的图像,但是我开始出现此错误。

 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:

1 个答案:

答案 0 :(得分:0)

您在此处传递了错误的类型。 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。