pythonopencvcvzone

How to solve "cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'imshow'"


I'm learning how to use opencv, but I'm running into this problem.

from cvzone.HandTrackingModule import HandDetector
import cv2


cap = cv2.VideoCapture("https://192.168.178.49:8080/video")
detector = HandDetector(maxHands=1, detectionCon=0.7)

while True:
    success, img= cap.read()
    
    img = detector.findHands(img) 
    
    cv2.imshow("AI", img)
    cv2.waitKey(1)

Results in this error:

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
  File "d:\Programming\Arm Code\testhandai.py", line 13, in <module>
    cv2.imshow("AI", img)
cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'imshow'
> Overload resolution failed:
>  - mat is not a numerical tuple
>  - Expected Ptr<cv::cuda::GpuMat> for argument 'mat'
>  - Expected Ptr<cv::UMat> for argument 'mat'

I'm using Python 3.8 64-bit and the latest version for all the packages. Thank you.


Solution

  • Output of detector.findHands(img) is a tuple. You should give second element of it as input to cv2.imshow():

    from cvzone.HandTrackingModule import HandDetector
    import cv2
    
    
    cap = cv2.VideoCapture("https://192.168.178.49:8080/video")
    detector = HandDetector(maxHands=1, detectionCon=0.7)
    
    while True:
        success, img= cap.read()
        
        img = detector.findHands(img) 
        
        cv2.imshow("AI", img[1])
        cv2.waitKey(1)