简体   繁体   English

如何使用基本身份验证从 url 中读取 cv2.imread

[英]How to cv2.imread from a url with basic authentication

I am trying to use cv2 and pyzbar for real time reading of QR codes from an IP camera.我正在尝试使用 cv2 和 pyzbar 从 IP 摄像机实时读取二维码。

This works:这有效:

os.system("wget --quiet http://user:password@url -O file.jpg")
image = cv2.imread("file.jpg")
barcodes = pyzbar.decode(image)

But it's clearly inefficient - much better to imread the url directly.但这显然效率低下 - 直接读取网址要好得多。 But I can't work out how to do that with basic authentication.但是我不知道如何使用基本身份验证来做到这一点。 Would really appreciate some help.真的很感激一些帮助。

(There have been a number of similar questions but I can't find any that have been answered!) (已经有许多类似的问题,但我找不到任何已回答的问题!)

thanks谢谢

Dan

or there's this alternative simple solution that doesn't involve streaming the video:或者有一个不涉及流式传输视频的替代简单解决方案:

from requests.auth import HTTPBasicAuth
import numpy as np

resp = requests.get(url, auth=(user, password))

image = np.asarray(bytearray(resp.content), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)

Hope it helps other people!希望它可以帮助其他人!

this may help you这可能会帮助你

import numpy as np
import urllib.request as rq
import cv2
from matplotlib import pyplot as plt

# load image from url
def urlToImage(url):
    # download image,convert to a NumPy array,and read it into opencv
    resp = rq.urlopen(url)
    img = np.asarray(bytearray(resp.read()),dtype="uint8")
    img = cv2.imdecode(img,cv2.IMREAD_COLOR)

    #return the image
    return img
img = urlToImage("https://www.pyimagesearch.com/wp-content/uploads/2015/01/google_logo.png")
plt.imshow(img)

Thanks all - but turns out there is a nice simple answer:谢谢大家 - 但事实证明有一个很好的简单答案:

cap=cv2.VideoCapture("http://user:password@url")
_, image=cap.read()
barcodes = pyzbar.decode(image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM