Reputation: 305
I'm attempting to use kmeans clustering to get the most common colors out of an image. It works fine with local images, but returns this error with the new functionality of pulling an image from a url. Here's the code up to the line that is throwing the error:
# import the necessary packages
from sklearn.cluster import KMeans
import numpy as np
import urllib
import argparse
import utils
import cv2
def getCommonColors(url):
req = urllib.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1)
image = cv2.imread(np.array_str(img))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Any help would be greatly appreciated!
Upvotes: 2
Views: 9136
Reputation: 1751
maybe you could try it, just some change from your code..
import numpy as np
import urllib2 #maybe requests is another good choice
import cv2
def getCommonColors(url):
req = urllib2.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1)
# image = cv2.imread(np.array_str(img)) <-- I think you shoudn't use this method, it will return NoneType in python
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
Upvotes: 3