Home > Software engineering >  (OpenCV , cv2) The cv2.error while capture the image through camera lens
(OpenCV , cv2) The cv2.error while capture the image through camera lens

Time:10-28

Error (-215) size.width>0 && size.height>0 occurred when attempting to display an image using OpenCV

I have the similar issue with this discussion, after I read it still don't have clue for how to address mine,that I post my case here

I use raspberrypi 4 ; 32 bit, linux run python ; logitech USB camera lens ; run in virtual environment

  • the python script opencv_camera.py
import cv2
 
# define a video capture object
vid = cv2.VideoCapture(1) ########## 1- device id =1###if 1 deosn't work try 2
 
while(True):
     
    ret, frame = vid.read()
 

    cv2.imshow('frame', frame)
     
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
vid.release()

cv2.destroyAllWindows()

error occur:

cv2.error: OpenCV(4.6.0) .... in function 'imshow'

  • the output:
(venv) joy@raspberrypi:~/Desktop $ python opencv_camera.py
[ WARN:[email protected]] global /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Traceback (most recent call last):
  File "/home/joy/Desktop/opencv_camera.py", line 14, in <module>
    cv2.imshow('frame', frame)
cv2.error: OpenCV(4.6.0) /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

  • troubleshooting status: now find out ret is none
import cv2
 
 
# define a video capture object
vid = cv2.VideoCapture(2) ########## 1- device id =1###if 1 deosn't work try 2
 
while(True):
    ret, frame = vid.read()
           

    if ret == True:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
               
    else:
       
        print("ret is empty")
        break

 
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

output:

(venv) joy@raspberrypi:~/Desktop $ python opencv_camera.py
[ WARN:[email protected]] global /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
ret is empty

CodePudding user response:

If you want to take a Image, you should not use

while(True):

Because it means that you will be constantly reading what was from the camera.

Refer below for the code if you want to just take a picture, you may constantly run

ret, frame = vid.read()

to retake a new picture

CODE:

import cv2
 
 
# define a video capture object
vid = cv2.VideoCapture(0) 

#Read the current camera
ret, frame = vid.read()
cv2.imshow('frame', frame)
cv2.waitKey(1)
             
#Unattach the camera
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

CodePudding user response:

after I changed vid = cv2.VideoCapture() to 0 now as video function is working, but my original purpose is taking one image though

import cv2
 
 
# define a video capture object
vid = cv2.VideoCapture(0) ########## 1- device id =1###if 1 deosn't work try 2


 
while(True):
    ret, frame = vid.read()
             

    if ret == True:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
       
           
    else:
       
        print("ret is empty")
        break
 
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

  • also for taking pic
import cv2
import time
 
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret,frame = cap.read() # return a single frame in variable `frame`
 
# run 60 sec x 2 min
time_end = time.time()   100
 
while time.time() < time_end:
 
    while(True):
        cv2.imshow('img1',frame) #display the captured image
        if cv2.waitKey(60) & 0xFF == ord('y'): #save on pressing 'y'
            cv2.imwrite('/home/joy/Desktop/pic.png',frame)
            cv2.destroyAllWindows()
            break
   
    cap.release()
    break

  • Related