简体   繁体   English

Cv2 函数 imshow、imdecode 和 waitKey 不起作用

[英]Cv2 functions imshow, imdecode and waitKey don't work

I am working on a project that can display phone cam display on pc.我正在开发一个可以在电脑上显示手机摄像头的项目。 For this task I am using the infamous python module Cv2 and numpy.对于这个任务,我使用了臭名昭著的 python 模块 Cv2 和 numpy。 But for some reason Cv2 functions aint working.但由于某种原因,Cv2 功能无法正常工作。 Any kind of help would be welcome.欢迎任何形式的帮助。 Regards, Odysseus问候,奥德修斯

My code:我的代码:

import urllib.request
import numpy as np
# import time
from cv2 import __all__

cv2 = __all__
URL = "http://myip/shot.jpg"
while True:
    img_arr = np.array(bytearray(urllib.request.urlopen(URL).read()), dtype=np.uint8)
    img = cv2.imdecode(img_arr, -1)
    cv2.imshow('IPWebcam', img)
    q = cv2.waitKey(1)
    if q == ord("q"):
        break

cv2.destroyAllWindows()

The error:错误:

Traceback (most recent call last):
File "C:\Users\name\PycharmProjects\cam\df.py", line 10, in <module>
     img = cv2.imdecode(img_arr, -1)
AttributeError: 'list' object has no attribute 'imdecode'

You tried to import OpenCV using this:您尝试使用以下命令导入 OpenCV:

from cv2 import __all__
cv2 = __all__

That is entirely wrong .那是完全错误的。 __all__ is nothing but a list of all the names of all the defined functions and whatnot. __all__只不过是所有已定义函数的所有名称的列表等等。 That in itself is useless if you want to use the library.如果您想使用该库,这本身是无用的。

Simply do this:只需这样做:

import cv2 as cv

And then use like so: cv.imdecode(...)然后像这样使用: cv.imdecode(...)

Change your img to:将您的 img 更改为:

img = cv2.imdecode(np.frombuffer(img_arr, dtype=np.uint8), cv2.IMREAD_COLOR)

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

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