简体   繁体   中英

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. But for some reason Cv2 functions aint working. 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:

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. 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(...)

Change your img to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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