christosf360
Posts: 15
Joined: Sat Dec 16, 2017 10:35 pm

Error capturing video from the Pi camera with Open CV

Sun Dec 09, 2018 8:51 pm

Hello everyone,

I am using my Pi to experiment with a little project and I am trying to capture video via the Pi camera (to begin with). On the OpenCV website I found this very simple piece of code but unfortunately when I run it it produces an error (The error begins with "Assertion failed". Any suggestions please?

Here is the code:

Code: Select all

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # convert in gray
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

DirkS
Posts: 9732
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Error capturing video from the Pi camera with Open CV

Sun Dec 09, 2018 8:58 pm

Please post the *full* error message

christosf360
Posts: 15
Joined: Sat Dec 16, 2017 10:35 pm

Re: Error capturing video from the Pi camera with Open CV

Sun Dec 09, 2018 9:36 pm

Apologies, the error says:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp, line 269
Traceback (most recent call last):
File "myversion_1.py", line 31, in <module>
cv2.imshow("Frame", frame)
cv2.error: /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow

DirkS
Posts: 9732
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Error capturing video from the Pi camera with Open CV

Sun Dec 09, 2018 9:54 pm

I googled the error and it basically means there's no image (width and or height is zero)
So you will have to do a step back and get your input working.

christosf360
Posts: 15
Joined: Sat Dec 16, 2017 10:35 pm

Re: Error capturing video from the Pi camera with Open CV

Sun Dec 09, 2018 10:24 pm

OK, thank you, I will have a look.

However, I thought that these lines, provide the images:

Code: Select all

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

il_diavolo
Posts: 130
Joined: Mon Dec 02, 2013 7:56 pm

Re: Error capturing video from the Pi camera with Open CV

Mon Dec 10, 2018 10:27 pm

I will be interested to see if you solve this.

I have had the Pi camera working with OpenCV for a couple of years, first with Jessie and then with Stretch but after a recent apt-get update and upgrade the code (very similar to yours) ceased to work. I have had to rewrite using Picamera instead of OpenCV to read the video stream. My second camera, a USB web cam, continues to work with OpenCV.

il_diavolo
Posts: 130
Joined: Mon Dec 02, 2013 7:56 pm

Re: Error capturing video from the Pi camera with Open CV

Tue Dec 11, 2018 10:23 pm

I should have asked in yesterday's post, did you start the v4l2 driver?
Type "sudo modprobe bcm2835-v4l2" in a terminal window before you run the program.

christosf360
Posts: 15
Joined: Sat Dec 16, 2017 10:35 pm

Re: Error capturing video from the Pi camera with Open CV

Wed Dec 12, 2018 5:30 pm

Hi,

I am going to try these drivers and let you know

Return to “Python”