Ask Your Question
0

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow (Python)

asked 2017-03-29 10:17:36 -0600

potu1304 gravatar image

Hello!

I searched the Internet but did not find a solution for me. First of all I simply want to run my webcam and see it. Here is my code:

import numpy as np
import cv2

cap = cv2.VideoCapture(3)
print cap.isOpened()
if cap.isOpened():
    print "cap is opened"
    while(True):
        re,img=cap.read()
        cv2.imshow("video output", img)
        k = cv2.waitKey(10)&0xFF
        if k==27:
            break
cap.release()
cv2.destroyAllWindows()

ca.isOpened() still says that it is false, so I do not enter the loop. If I enter the loop without checking then I get the error which I mentioned in the headline. So what am I doing wrong? I try to get my webcam working for 1 hour, its getting a little bit frustrating ;)

Best regards

edit retag flag offensive close merge delete

Comments

how many webcams do you have ? the 1st would be id 0, the 2nd id 1 , etc. probably it's just the wrong id ...

berak gravatar imageberak ( 2017-03-29 10:42:23 -0600 )edit

I have the one from the notebook self, and would like to use the one I plugged in. So I thought id = 1 but it also does not work

potu1304 gravatar imagepotu1304 ( 2017-03-30 04:20:25 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-10-17 13:24:17 -0600

Hello same issue with my raspberry


python --version
Python 3.5.3

Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.0'

cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Python source code :

import numpy as np
import cv2

device = 0
cap = cv2.VideoCapture(device)
# if capture failed to open, try again
if not cap.isOpened():
    cap.open(device)

# only attempt to read if it is opened
if cap.isOpened:
    while True:
        re, img = cap.read()
        # Only display the image if it is not empty
        if re:
            cv2.imshow("video output", img)
        # if it is empty abort
        else:
            print( "Error reading capture device")
            break
        k = cv2.waitKey(10) & 0xFF
        if k == 27:
            break
    cap.release()
    cv2.destroyAllWindows()
else:
    print("Failed to open capture device")

But works whith this source :

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

# allow the camera to warmup
time.sleep(0.1)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array

    # show the frame
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-29 10:17:36 -0600

Seen: 9,674 times

Last updated: Mar 29 '17