Home > Software design >  Cv2 functions imshow, imdecode and waitKey don't work
Cv2 functions imshow, imdecode and waitKey don't work

Time:08-29

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'

CodePudding user response:

You tried to import OpenCV using this:

from cv2 import __all__
cv2 = __all__

That is entirely wrong.

Simply do this:

import cv2 as cv

And then use like so: cv.imdecode(...)

CodePudding user response:

Change your img to:

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