I am trying to read an image which is on a website, I am passing image link to cv2.imgread(https://website.com/photo.jpg)
for example. But it returns a NoneType
. The error says TypeError: cannot unpack non-iterable NoneType object
HERE's my code:
def get_isbn(x):
print(x)
image = cv2.imread(x)
print(type(image))
detectedBarcodes = decode(image)
for barcode in detectedBarcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x w, y h), (255, 0, 0), 5)
# print(barcode.data)
# print(type(barcode.data))
byte_isbn = barcode.data
string_isbn = str(byte_isbn, encoding='utf-8')
return string_isbn
x is my url as a param.
CodePudding user response:
You need to convert link to array and then decode it with opencv.
function
import numpy as np
import urllib
import cv2
def url_to_image(url):
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
Merge in your code
import numpy as np
import urllib
import cv2
def url_to_image(url):
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
def get_isbn(x):
print(x)
image = url_to_image(x)
print(type(image))
detectedBarcodes = decode(image)
for barcode in detectedBarcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x w, y h), (255, 0, 0), 5)
# print(barcode.data)
# print(type(barcode.data))
byte_isbn = barcode.data
string_isbn = str(byte_isbn, encoding='utf-8')
return string_isbn
CodePudding user response:
In some cases we can read the image as if it were a video composed of single frame.
It's not going to work in all cases, and it's not efficient in terms of execution time.
The advantage is that the solution uses only OpenCV package (it may also work in C for example).
Code sample:
import cv2
image_url = 'https://i.stack.imgur.com/fIDkn.jpg'
cap = cv2.VideoCapture(image_url) # Open the URL as video
success, image = cap.read() # Read the image as a video frame
if success:
cv2.imshow('image ', image) # Display the image for testing
cv2.waitKey()
cap.release()
cv2.destroyAllWindows()