'cv2.error: OpenCV (4.5.3) occurs in raspberry pi

I tried to test Picam with the code below with Raspberry Pie.

import cv2

def main():
    camera = cv2.VideoCapture(-1)
    camera.set(3,640)
    camera.set(4,480)
    
    while( camera.isOpened() ):
        _, image = camera.read()
        cv2.imshow( 'camera test' , image)
        
        if cv2.waitKey(1) == ord( 'q '):
            break
        
    cv2.destroyAllWindows()
    
if __name__ == '__main__':
    main()

However, this error occurs.

        cv2.error: OpenCV(4.5.3) /tmp/pip-wheel-hwcmjluw/opencv-
python_dc56ddd000dd4893b1f852d88d4a5959/opencv/modules/core/src/arra
y.cpp:2494:error: (-206:Bad flag (parameter or structure field)) 
Unrecognized orunsupported array type in function 'cvGetMat'

What should I do?



Solution 1:[1]

OpenCV doesn't like whitespaces in ord( 'q '). I am using OpenCv 4.5.5/Bullseye. Using picamera v1. It will worked picamera v2.import cv2

import cv2

def main():
    camera = cv2.VideoCapture(-1)
    camera.set(3,640)
    camera.set(4,480)
    
    while(camera.isOpened()):
        _, image = camera.read()
        cv2.imshow('camera test' , image)
        if cv2.waitKey(1) == ord('q'):# esc Key  
            break 
         
    cv2.destroyAllWindows()
    
if __name__ == '__main__':
    main()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 toyota Supra