you're reading...
Computer vision, How-to's

openCV web streaming

2015-09-26 13.52.04

This post is a start of a series on using openCV with leJOS robots controlled from Android phones.

openCV is supported in the latest snapshot of leJOS and will be supported in the next release, which is coming soon.

I will start off with a version of my Webcam HTTP streaming code.

This uses openCV to grab the camera image.

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import lejos.hardware.Button;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;

public class HttpStreamOpenCV {
    public static void main(String[] args) throws Exception {
        Mat mat = new Mat();
        VideoCapture vid = new VideoCapture(0);
        vid.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 160);
        vid.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 120);
        System.out.println("Camera open");
        ServerSocket ss = new ServerSocket(8080);
        Socket sock = ss.accept();
        System.out.println("Socket connected");
        String boundary = "Thats it folks!";
        writeHeader(sock.getOutputStream(), boundary);
        System.out.println("Written header");
        long stime = System.currentTimeMillis();
        int cnt = 0;
        while (Button.ESCAPE.isUp()) {
            if (!mat.empty()) {
	            writeJpg(sock.getOutputStream(), mat, boundary);
	            System.out.println("Written jpg");
	            if (cnt++ >= 100)
	                long stop = System.currentTimeMillis();
	                System.out.println("Frame rate: " + (cnt*1000/(stop - stime)));
	                cnt = 0;
	                stime = stop;
            } else  System.out.println("No picture");
    private static void writeHeader(OutputStream stream, String boundary) throws IOException {
        stream.write(("HTTP/1.0 200 OK\r\n" +
                "Connection: close\r\n" +
                "Max-Age: 0\r\n" +
                "Expires: 0\r\n" +
                "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" +
                "Pragma: no-cache\r\n" + 
                "Content-Type: multipart/x-mixed-replace; " +
                "boundary=" + boundary + "\r\n" +
                "\r\n" +
                "--" + boundary + "\r\n").getBytes());
    private static void writeJpg(OutputStream stream, Mat img, String boundary) throws IOException {
        MatOfByte buf = new MatOfByte();
        Highgui.imencode(".jpg", img, buf);
        byte[] imageBytes = buf.toArray();
        stream.write(("Content-type: image/jpeg\r\n" +
                "Content-Length: " + imageBytes.length + "\r\n" +
        stream.write(("\r\n--" + boundary + "\r\n").getBytes());

Thanks to Andy for the openCV parts of this code.

The current openCV code is a bit sensitive to the camera being used. Some camera give errors and partial frames.

This post has been updated to use opencv-2411 rather than opencv-300, as we decided to go with opencv 2.4 for the leJOS 0.9.1 release. Andy added the ability to set the frame size, which is now in the above code.



4 thoughts on “openCV web streaming

  1. Where I can see list of supported webcams?

    Posted by Dmitriy | 2015/10/20, 09:38
  2. So you can plug a webcam in to a EV3, thats amazing!

    Posted by Simon Burfield | 2015/10/29, 11:04

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: