Netcat stream on /dev/stdin not working with OpenCV 3.1.0-dev on Ubuntu 16.04

asked 2016-12-05 04:28:37 -0600

NotANumber_1 gravatar image

Hello to everyone. I m encountered a "strange" problem trying to stream a video using netcat from my Raspberry Pi (using the Pi Camera).

The problem is the following: If I use Opencv 2.4.13, everything is okay, and I m able to see on my laptop the video coming from the Raspberry with an acceptable latency (~100ms) using simply:

ON RASPBERRY PI:    
raspivid -n -t 0 -w 800 -h 600 -fps 30 -o - | nc 145.94.195.184 5000

ON LAPTOP
nc -l -p 5000 | python testVideo.py

Reading from /dev/stdin using the function "VideoCapture"

cap = cv2.VideoCapture("/dev/stdin")

But If I want to use Opencv 3.1.0-dev (installed on another ubuntu 16.04 partition), nothing works. Obtaining the following error:

Unable to stop the stream: Inappropriate ioctl for device
GStreamer: Error opening bin: Unrecoverable syntax error while parsing pipeline /dev/stdin
      OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/vgarofano/opencv/modules/imgproc/src/color.cpp, line 9748
    Traceback (most recent call last):
      File "testVideo.py", line 22, in <module>
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.error: /home/vgarofano/opencv/modules/imgproc/src/color.cpp:9748: error: (-215) scn == 3 || scn == 4 in function cvtColor

The code I am using is a simple "read the video and show it":

import numpy as np
import cv2

cap = cv2.VideoCapture("/dev/stdin")

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

How it is possible that the same code works perfectly with OpenCV 2.4.13 while it gives errors using OpenCV 3.1.0-dev? I think that there is something different with the function VideoCapture, but I am not able to understand how to fix it. Someone that maybe already faced this type of problem could help me? I tried almost everything found on this forum, but nothing changed.

Remarks: I need to use the OpenCV 3.1.0-dev because with opencv_contrib package I want to use the Aruco library (already well imported in Python) to perform some applications detecting markers. Another constraint is that I need it using Python, because I am developing a GUI interface for a ROV using pygame (for other type of applications). By the way the final goal is to stream from the Raspberry camera, in a local network, with acceptable latency and the possibility to manage the video with OpenCV. So, if you have also some other suggestions on how implement it, could be also also very helpful.

edit retag flag offensive close merge delete

Comments

please check a: if the capture opened() b: if the frame you read is valid

berak gravatar imageberak ( 2016-12-05 04:53:23 -0600 )edit

Thank you berak for your comment. So a): I add this line to the code to check if the capture is opened

if cap.isOpened():
     print "cap is Open"
     ...while loop....
   else:
       print "cap is closed"

And I obtain this error: Unable to stop the stream: Inappropriate ioctl for device GStreamer: Error opening bin: Unrecoverable syntax error while parsing pipeline /dev/stdin cap is closed

So it seems that the "capture" is always close. There is something wrong in opening it?

about b) : I expect that the frame I want to read is valid , both because using mplayer, for example, I am able to stream the video and if I will write the netcat stream on a test.txt file, and later just passing this last to Videocapture function, the video appears (with strange delay

NotANumber_1 gravatar imageNotANumber_1 ( 2016-12-05 05:46:36 -0600 )edit

ok. the cvtColor error might be because of an empty/invalid image. (can't help much with netcat/pi, unfortunately)

berak gravatar imageberak ( 2016-12-05 06:12:33 -0600 )edit