Reader small image

You're reading from  OpenCV 3.0 Computer Vision with Java

Product typeBook
Published inJul 2015
Reading LevelIntermediate
Publisher
ISBN-139781783283972
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Daniel Lelis Baggio
Daniel Lelis Baggio
author image
Daniel Lelis Baggio

Daniel Lélis Baggio has started his works in computer vision through medical image processing at InCor (Instituto do Coração – Heart Institute) in São Paulo, Brazil, where he worked with intra-vascular ultrasound (IVUS) image segmentation. After that he has focused on GPGPU and ported that algorithm to work with NVidia's Cuda. He has also dived into 6 degrees of freedom head tracking with Natural User Interface group through a project called EHCI (http://code.google.com/p/ehci/ ). He also wrote “Mastering OpenCV with Practical Computer Vision Projects” from Packt Publishing.
Read more about Daniel Lelis Baggio

Right arrow

Chapter 7. OpenCV on the Server Side

As the Internet gets more and more interactive, a subject of great interest is how to deal with image processing on the server side that enables you to create web applications dealing with OpenCV. As Java is among the languages of choice when developing web apps, this chapter shows the entire architecture of an application that lets users upload an image and add a fedora hat on top of detected faces using techniques learned throughout the book.

In this chapter, we will cover the following topics:

  • Setting up an OpenCV web application

  • Mixed reality

  • Image uploading

  • Dealing with HTTP requests

By the end of this chapter you will know how to create a complete web application with image processing, obtain input from the user, process the image on the server side, and return the processed image to the user.

Setting up an OpenCV web application


Since this chapter covers the development of a web application using Java OpenCV, it is important to address a couple of differences when going to the server side. The first thing is to tell the web container, generally Tomcat, Jetty, JBoss, or Websphere, about the location of native libraries. Other details deal with loading the native code. This should happen as soon as the web server goes up and should not occur again.

The advantages of using the web architecture are significant. As certain image-processing tasks are compute intensive, they could easily drain the device's battery in no time, so, taking them to a more robust hardware on the cloud would relieve local processing. Besides that, there's no need for users to install anything more than the web browser, and the updates happening on the server side are also very handy.

On the other hand, there are a few drawbacks. If, instead of hosting the web application on the administrator infrastructure...

Mixed reality web applications


The web application we are going to develop draws Fedora hats on top of the detected heads in a given image. In order to do this, the user uploads the image through a simple form, and then it is converted to an OpenCV matrix in memory. After conversion, a cascade classifier looking for faces is run over the matrix. A simple scale and a translation are applied to estimate the hat's position and scale. A transparent fedora image is then drawn on the specified position for each of the detected faces. The result is then returned through HTTP by giving the mixed reality picture to the user. Notice that all the processing happens on the server side, so the client is only left to upload and download the image, which is very useful for clients that rely on batteries, such as smartphones.

Note

Mixed reality (MR), sometimes referred to as hybrid reality (encompassing both augmented reality and augmented virtuality), refers to the merging of real and virtual worlds to produce...

Image processing


In this section, we are going to describe how to process the received image in order to draw an image file on top of it. Now, a cascade classifier is run just as in the previous chapter. It is important to pay attention to the XML cascade file location. Throughout the code, we have used a helper function called getResourcePath, and we have used the convention of storing all the resources in the src/main/resources/ folder. This way, the helper function works in a manner similar to that of the following code:

private String getResourcePath(String path) {
  String absoluteFileName = getClass().getResource(path).getPath();
  absoluteFileName = absoluteFileName.replaceFirst("/", "");
  return absoluteFileName;
}

Using this function, one can load a cascade through the following call:

private void loadCascade() {
  String cascadePath = getResourcePath("/cascades/lbpcascade_frontalface.xml");
  faceDetector = new CascadeClassifier(cascadePath);
}

After the cascade has been correctly...

Summary


In this chapter, we sent our computer vision applications to the server-side world. We started covering the basics of a simple servlet-based web application configuration using Maven, which provided us with a general application structure. We then added OpenCV dependencies to our pom.xml configuration file as used in a standard OpenCV desktop application. We then checked other runtime configurations as we deployed our web server using Maven.

With every webapp configuration aspect solved, we moved on to the development of our mixed reality application that explored the details of image uploading, converting it to an OpenCV Mat object and then writing a response to our clients with a processed image.

It seems that all aspects of creating basic computer vision applications have been covered now. We dealt with setting up OpenCV for Java and then learned how to work with matrices. We then touched on the basics of creating Java Swing desktop applications and worked with image-processing...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
OpenCV 3.0 Computer Vision with Java
Published in: Jul 2015Publisher: ISBN-13: 9781783283972
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Daniel Lelis Baggio

Daniel Lélis Baggio has started his works in computer vision through medical image processing at InCor (Instituto do Coração – Heart Institute) in São Paulo, Brazil, where he worked with intra-vascular ultrasound (IVUS) image segmentation. After that he has focused on GPGPU and ported that algorithm to work with NVidia's Cuda. He has also dived into 6 degrees of freedom head tracking with Natural User Interface group through a project called EHCI (http://code.google.com/p/ehci/ ). He also wrote “Mastering OpenCV with Practical Computer Vision Projects” from Packt Publishing.
Read more about Daniel Lelis Baggio