Reputation: 69
I tried to read an image from an url using open-cv.My code is
url = "some url"
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib.request.Request(url, None, headers=hdr)
resp = urllib.request.urlopen(req)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image1 = cv2.imread(image)
However I get an error like this- image = cv2.imread(image) TypeError: Can't convert object of type 'numpy.ndarray' to 'str' for 'filename'
Upvotes: 0
Views: 422
Reputation: 392
Try the following code. This will help
import cv2
import numpy as np
import requests
url = 'https://i.imgur.com/S3OGqV6.jpeg'
resp = requests.get(url, stream=True).raw
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow('image',image)
cv2.waitKey(0)
Upvotes: 2