1

I'm trying to get a script running on my raspberry pi (Ubuntu system). Right now, I was just refreshing myself on the basics of opencv, since it's been a little while since I worked with it. So I copy pasted this code straight from the OpenCV website and ran it.

import numpy as np
import cv2 as cv

cap = cv.VideoCapture(0)

if not cap.isOpened():
    print("Cannot open camera")
    exit()

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
        
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    
    # Display the resulting frame
    cv.imshow('frame', gray)
    
    if cv.waitKey(1) == ord('q'):
        break
        
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

Only, running it gave me a solid gray window as a pop-up and threw these errors:

[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (1761) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

The code does still run, since I can close the window by pressing 'q'. But the video streaming part has gone horribly wrong. I don't even know what GStreamer is, let alone what a GStreamer pipeline is. I have no idea how to fix this and haven't found anything online that works.

stateMachine
  • 5,227
  • 4
  • 13
  • 29
Grant Allan
  • 189
  • 1
  • 4
  • 10
  • There's a similar error [here](https://forums.developer.nvidia.com/t/gstreamer-warning-embedded-video-playback-halted-module-v4l2src7-reported-failed-to-allocate-required-memory/155932), I don't know if your problem is related, but it doesn't hurt to try their solution. – stateMachine Jan 07 '22 at 22:51
  • @stateMachine I'll check it out once I get my hands on that raspberry pi again. My boss took it on his trip, so now I'm running my code on one of our other computers, where I'm not getting that error. – Grant Allan Jan 10 '22 at 17:27

1 Answers1

2

my code worked running on jetson nano

def __gstreamer_pipeline(
        camera_id,
        capture_width=1920,
        capture_height=1080,
        display_width=1920,
        display_height=1080,
        framerate=30,
        flip_method=0,
    ):
    return (
            "nvarguscamerasrc sensor-id=%d ! "
            "video/x-raw(memory:NVMM), "
            "width=(int)%d, height=(int)%d, "
            "format=(string)NV12, framerate=(fraction)%d/1 ! "
            "nvvidconv flip-method=%d ! "
            "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
            "videoconvert ! "
            "video/x-raw, format=(string)BGR ! appsink max-buffers=1 drop=True"
            % (
                    camera_id,
                    capture_width,
                    capture_height,
                    framerate,
                    flip_method,
                    display_width,
                    display_height,
            )
    )
   
stream = cv2.VideoCapture(__gstreamer_pipeline(camera_id=0, flip_method=2), cv2.CAP_GSTREAMER)
Ali Mustofa
  • 185
  • 1
  • 6