FlytSim VideoStream


#1
  • I recently installed FlytSim on a windows 10 System.
  • I am able to access the Application via the localhost/flytconsole URL.
  • I am trying the Video-Streaming Sample APP. In the Select Video drop-down it shows me a stream with the following
    link /flytsim/flytcam/image_capture but what should I do to simulate the video stream.

#2

Hi,

You have to configure the FlytSim in PX4 mode, as the light weight APM mode does not have video streaming feature. But mind you, in PX4 mode FlytSim is quite computationally heavy and the drone might be a bit wobbly if your system is not powerful enough.

To know how to configure FlytSim to PX4 mode, click here.


#3

I am trying the following on my servlet
HttpGet getRequest = new HttpGet(
http://localhost:8080/stream?topic=/flysim/typhoon_h480/cgo3_camera/image_raw&type=ros_compressed”);
getRequest.addHeader(“accept”, “application/json”);

		HttpResponse response = httpClient.execute(getRequest);

		if (response.getStatusLine().getStatusCode() != 200) {
			throw new RuntimeException("Failed : HTTP error code : "
			   + response.getStatusLine().getStatusCode());
		}

		BufferedReader br = new BufferedReader(
                         new InputStreamReader((response.getEntity().getContent())));
		System.out.println("Output from Server .... \n");
              String output;
		System.out.println("Output from Server .... \n");
		String output1 = br.readLine();
		System.out.println(output1);
		String output2 = br.readLine();
		System.out.println(output2);

But after the first line which gives the output of --boundarydonotcross the second line doesn’t give anything

Can I read the stream like this please suggest


#4

Hi,

Have you gone through our video streaming api documentation?

What is your end goal? It seems you are trying to read the video stream line by line. Can you please tell me why you are trying to do that?

You can also checkout our video streaming sample web app over here


#5

Yes I do see what Flytbase is doing in their Video-Streaming app and as well the following section in the Video-Streaming documentation

Note: To view the video of a particular stream from the list of streams you need to create an img tag add the link to its source.

But I want to consume the videoStream response on the server side since we want to save that in our document repository hence I am trying to read that using the Inputstreamreader at the server side.

Do u have some sample of code to read the output from the videostream in the server or if you could suggest how that could be done


#6

I tried this piece of code as well but the thread never comes out of the command entity.writeTo(fileOutputStream)

import org.apache.http.impl.client.DefaultHttpClient;

public class ImageUploader {

/**
 * @param args
 */
public static void main(String[] args) {
	DefaultHttpClient httpClient = new DefaultHttpClient();
	HttpGet getRequest = new HttpGet(
		"http://localhost:8080/stream?topic=/flysim/typhoon_h480/cgo3_camera/image_raw&type=ros_compressed");
	//getRequest.addHeader("accept", "application/json");

	HttpResponse response = null;
	try {
		response = httpClient.execute(getRequest);
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	if (response.getStatusLine().getStatusCode() != 200) {
		throw new RuntimeException("Failed : HTTP error code : "
		   + response.getStatusLine().getStatusCode());
	}
	System.out.println("Output from server");
	HttpEntity entity = response.getEntity();
	System.out.println("Streaming" + entity.isStreaming());
	System.out.println("Repeatable" + entity.isRepeatable());
	System.out.println("Content-Length" + entity.getContentLength());
	System.out.println("Content-Type" + entity.getContentType());
	System.out.println("encoding" + entity.getContentEncoding());
	try {
		System.out.println("Writing to File -- Start");
		File file = new File("C:\\FlytVideoStreaming\\flytsimimg.jpg");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		entity.writeTo(fileOutputStream);
		System.out.println("Writing to File -- End");
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (UnsupportedOperationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}


#7

The Videostreaming App from Flytbase Sample App is showing a static image on my system.
But, when I try to do the same with this piece of code it doesn’t work

package flytbase.stream;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class ShowFlytImage {

 public static void main(String[] args) throws Exception {
	 
        
        File f = new File("C:\\FlytVideoStreaming\\ShowFlytImage.htm");
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write("<html>");
        bw.write("<body>");
        bw.write("<h1>Show FlytSim Image</h1>");
        bw.write("<img src='C:\\Users\\prashriv\\Pictures\\mountain.jpg' alt='mountain' height='100' width='100'>");
        bw.write("<br/>");
        bw.write("<img id='video-page-img' src='http://localhost:8080/stream?topic=/flysim/typhoon_h480/cgo3_camera/image_raw&width=320&height=240&type=ros_compressed'>");
        bw.write("</body>");
        bw.write("</html>");

        bw.close();

        Desktop.getDesktop().browse(f.toURI());
    }

}


#8

So I can get the image seen using the tag the previous code I posted had a typo but still in the Java class were I am trying to write the entity.inputstream to the outputstream that doesn’t work.


#9

So please suggest what is wrong with the following code why the videostream will not write to the FileOutputStream

package flytbase.stream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class ImageUploader {

/**
 * @param args
 */
public static void main(String[] args) {
	DefaultHttpClient httpClient = new DefaultHttpClient();
	HttpGet getRequest = new HttpGet(
		"http://localhost:8080/stream?topic=/flytsim/typhoon_h480/cgo3_camera/image_raw&width=320&height=240&type=ros_compressed");
	//getRequest.addHeader("accept", "application/json");

	HttpResponse response = null;
	try {
		response = httpClient.execute(getRequest);
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	if (response.getStatusLine().getStatusCode() != 200) {
		throw new RuntimeException("Failed : HTTP error code : "
		   + response.getStatusLine().getStatusCode());
	}
	System.out.println("Output from server");
	HttpEntity entity = response.getEntity();
	System.out.println("Streaming" + entity.isStreaming());
	System.out.println("Repeatable" + entity.isRepeatable());
	System.out.println("Content-Length" + entity.getContentLength());
	System.out.println("Content-Type" + entity.getContentType());
	System.out.println("encoding" + entity.getContentEncoding());
	try {
		System.out.println("Writing to File -- Start");
		File file = new File("C:\\FlytVideoStreaming\\flytsimimg.jpg");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		entity.writeTo(fileOutputStream);
		System.out.println("Writing to File -- End");
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (UnsupportedOperationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}


#10

Hi,

We are looking at your issue and my team will get back to you ASAP.


#11

@pragati Below python code can be useful. Please change source ip, topic and namespace accordingly.

import cv2
import urllib 
import numpy as np

stream=urllib.urlopen('http://localhost:8080/stream?topic=/flytsim/flytcam/image_raw')
bytes=''
while True:
    bytes+=stream.read(1024)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
        cv2.imshow('i',i)
        if cv2.waitKey(1) ==27:
            exit(0)    

This script is able to read and decode any raw mjpg stream. Saving those images as mjpg stream on local machine or dumping on a virtual camera is easy. Refer to stackoverflow page for more details. You can take reference from above and implement similar functionality in Java.
For your reference visit available URLs section on this page. This module is used for video streaming in FlytOS.