Ask Your Question
0

GStreamer unable to query pipeline state on Ubuntu18.04 with Opencv4

asked 2018-10-02 05:18:25 -0600

fra gravatar image

I have the same problem described here but with Ubuntu 18.04.

The program runs and does not stop but it does not catch the keyboard input and as soon as it starts I get the error

(process:11477): GStreamer-CRITICAL **: 18:44:25.567: gst_element_get_state: assertion 'GST_IS_ELEMENT (element)' failed
OpenCV | GStreamer warning: GStreamer: unable to query pipeline state (/home/fra/Documents/openCV/openCV/modules/videoio/src/cap_gstreamer.cpp:420)

I have installed the gstreamer libraries because they were required by opencv and I am including and linking them in the Makefile like this (see the complete Makefile later):

Have anyone had the same problem? What is generating the exception?

I am thinking the error is related to Opencv deendencies but I am not sure is not a problem related to SDL2.0.

SOURCE CODE OF THE C++ PROGRAM

Here is a minimal working example: clicking the left and right buttons of the mouse works (catched directly with Opencv callbacks) while pressing and releasing the keyboard buttons does not call the proper SDL callback. The script can be launched with ./executable camID anyNameYouWant where camID is an integer (typically 0 or 1) that identifies the cam you want to use.

#ifndef __OPENCV__
#define __OPENCV__
#include "opencv2/opencv.hpp"
#endif
#include<iostream>
//#include "utils.hpp"
//#include "constants.hpp"
#include<unistd.h>
#include <vector>
#include<SDL.h>
#include <SDL_events.h>

using namespace cv;
using namespace std;
static const int delay = 2;
#define WINDOW_SCALE 1.7

void onTrackbar_changed(int, void* data);
void onMouse(int evt, int x, int y, int flags, void* param);
void PrintKeyInfo( SDL_KeyboardEvent *key );
int keyboardCallback(SDL_KeyboardEvent ev);

//InputStateContext context;

int main(int argc, char* argv[])
{
    /* Initialise SDL */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0)
    {
        fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() );
        exit( -1 );
    }

  string host;
  unsigned int port;
  const String sourceReference = argv[1];
  int camNum;
  string sensorName;
  try
  {
    camNum = stoi(sourceReference); // throws std::length_error
  }
  catch (const std::exception& e)// reference to the base of a polymorphic object
    {
    std::cout<<"Exception: " << e.what()<<endl; // information from length_error printed
    return -1;
  }
  if (argc>4)
  {
    try
    {
      host = argv[2];
      port = atoi(argv[3]);
      sensorName = argv[4];
    }
    catch (const std::exception& e)
    {
      cout<<"impossible to convert host or port"<<endl;
      return -1;
    }
  }
  else if(argc>2)
  {
    cout<<"argumetns less than 4"<<endl;
    host = "http://localhost";
    port = 3456;

    sensorName = argv[2];
    cout<<argc<<endl;
    cout<<"sensor name set from arguments: "<< sensorName<<endl;

  }
  else
  {
    cout<<"stopping execution: too few arguments."<<endl;
    return -1;
  }
  VideoCapture cam(camNum);
  /* or
  VideoCapture captUndTst;
  captUndTst.open(sourceCompareWith);*/
  if ( !cam.isOpened())
  {
    cout << "Could not open reference " << sourceReference << endl;
   return -1;
  }

  namedWindow("Camera", WINDOW_NORMAL);
  Mat frame;

  SDL_Event keyboardEvent;

  cam>>frame;
  resize(frame, frame, cv::Size(frame.cols/WINDOW_SCALE, frame.rows/WINDOW_SCALE));
  resizeWindow("Camera", cv::Size(frame.cols/WINDOW_SCALE, frame.rows/WINDOW_SCALE));
  setMouseCallback("Camera", onMouse, &frame);
  while(1)
  {
      while( SDL_PollEvent( &keyboardEvent) )
      {
          switch( keyboardEvent.type )
          {
              /* Keyboard event */
              /* Pass the event data onto PrintKeyInfo() */

              case SDL_KEYDOWN:
              break;
              case SDL_KEYUP:
              keyboardCallback(keyboardEvent.key);
              break;

              /* SDL_QUIT event (window close) */
              case SDL_QUIT:
              return 0;
              break;

              default:
              break;
          }

      }
     Mat frame_out;
     frame_out = frame.clone();
    cam>>frame ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-10-02 05:26:15 -0600

berak gravatar image

updated 2018-10-02 05:32:06 -0600

try to reduce it to:

    VideoCapture cam(camNum);
    while (cam.isOpened() {
        Mat frame;
        if (! cam.read(frame)) break;
        imshow("W",frame);
        int k = waitKey(10); // NO YOU DON'T NEED SDL !
        if (k == 27) break; // escape pressed
    }

in other words, get rid of all that SDL debris first.

then try to avoid gstreamer, and use v4l instead:

   VideoCapture cam(0, CAP_V4L);
edit flag offensive delete link more

Comments

also, waitKey() returns the key, not the delay.

berak gravatar imageberak ( 2018-10-02 05:35:23 -0600 )edit

I am using SDL not related to waitkey nor to catch escape but to catch other keyboard events ('a', 'm', 'u' released) in the function keyboardCallback because openCV does not offer proper callbacks for keyboard events.

fra gravatar imagefra ( 2018-10-02 06:43:02 -0600 )edit

honestly, then you should go all the way and draw your image on an sdl window, and avoid all opencv gui.

mixing both is seriously asking for trouble.

berak gravatar imageberak ( 2018-10-02 06:45:28 -0600 )edit

Is it more reliable than openCV gui?

fra gravatar imagefra ( 2018-10-02 06:53:37 -0600 )edit

no idea, but you must not mix.

berak gravatar imageberak ( 2018-10-02 06:55:40 -0600 )edit

Sorry but I do not understand: for mixing you mean capturing the cam frames with open cv and using SDL for callbacks or using Opencv window with SDL callbacks or both?

fra gravatar imagefra ( 2018-10-02 07:07:29 -0600 )edit

callbacks, drawing, windows, anything gui related.

berak gravatar imageberak ( 2018-10-02 07:10:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-10-02 05:18:25 -0600

Seen: 1,667 times

Last updated: Oct 02 '18