我正在嘗試讀取網站上的影像cv2.imgread(https://website.com/photo.jpg),例如,我正在將影像鏈接傳遞給。但它回傳一個NoneType. 錯誤說TypeError: cannot unpack non-iterable NoneType object
這是我的代碼:
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 是我的 url 作為引數。
uj5u.com熱心網友回復:
您需要將鏈接轉換為陣列,然后使用 opencv 對其進行解碼。
功能
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
合并你的代碼
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
uj5u.com熱心網友回復:
在某些情況下,我們可以像讀取由單幀組成的視頻一樣讀取影像。
它不會在所有情況下都有效,并且在執行時間方面效率不高。
優點是該解決方案僅使用 OpenCV 包(例如,它也可以在 C 中作業)。
代碼示例:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/387274.html
上一篇:分段黑色和移動像素
下一篇:在嘈雜的二值影像中檢測不同的形狀
