The Web Dev Zone is brought to you by Stormpath—offering a pre-built Identity API for developers. Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management.

OpenCV is a popular library for image processing in C++. It promises very high performance and  even real-time video processing algorithms.  While it is a native library, available for Linux, Windows and OSX, it also features Java bindings that make it available to JVM applications.

Featuring hundreds of optimized computer vision algorithms, OpenCV would be an interesting addition to many HTML5 web applications. However, for web developers, there is still a gap from the web browser to the server. Fortunately, this is the sweet spot for Vaadin.

Vaadin is a server-side Java web framework. And Java we need here. While web is mostly considered as JavaScript-driven, browser-based environment, in this case Java has its benefits - Vaadin nicely fills the gap between HTML5/JavaScript user interfaces and powerful native Java libraries.

Setting up computer vision for the Java server

The first thing to do is to find a server  to run OpenCV. Like mentioned, it is a native C++ library, but fortunately available for many operating systems - and provided with excellent getting started documentation.

I started from compiling the latest 3.0 (beta) for my target platform with Java bindings. The results are essentially a native .so library file and a jar file for JNI wrappers. You can find the build instructions here.

You need to install the  native library into your Java library path. This varies depending on your platform and JDK version, but on my Linux machine it was something like:

 
sudo cp ./lib/libopencv_java300.so \
 
    /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/
 

If you are like me, using Maven to manage library dependencies in Java development, it makes sense to install the jar into a local repository with the correct version. This way it is easy to integrate the library into your Java applications:

 
mvn install:install-file \
 
-Dfile=./bin/opencv-300.jar \
 
-DgroupId=org.opencv \  
 
-DartifactId=opencv \
 
-Dversion=3.0.0 \
 
-Dpackaging=jar
 

After the build process, you have everything you need to start developing the application itself.

Web application with face recognition

So,  now we are ready to create something. I got my first idea for a ‘computer vision web application’ from a mobile Android application that uses a face recognition algorithm to locate a face in a picture and make some fun of it: The trollator. Yes, this should be fun and easy enough also as a web application. :)

Now, while you can study the full web application source code in GitHub,  let me explain some parts of it.

In Vaadin, all starts with the user interface: the com.vaadin.ui.UI class. You extend this class to create your own user interface, which the user will see when accessing the application using a web browser.

In this application it is actually quite simple and it has only two parts: 1) a webcam component to take a picture and 2) an image component to show the processed result. The image is processed automatically when it has been taken. Better check the structure in the code.

Remember that loading the native OpenCV library has to be done early. Or at least before you use it. I used a static block that is executed when a class is loaded. You can see it in the beginning of the TrollatorUI class:

 
static {
 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 
 }
 

Now it is available through the Java API and you can call the OpenCV methods in your application.

For the actual face recognition, I used the method based on Haar Cascade Classifiers. It is a very generic method for recognizing objects in pictures, and while you can create your own classifiers and train OpenCV to recognize any objects, OpenCV also conveniently ships with pre-trained classifiers for human faces.

In this faceDetect method the classifier is loaded from the file haarcascade_frontalface_default.xml and it is used to detect faces from the uploaded picture from a webcam. When matches are found, an overlay image is drawn on top of it.

OpenCV uses its own model for images called Mat and to map that to Java world, some conversion routines are needed. You can see that in toBufferedImage. Furthermore, to present the generated in-memory image to the user, I created a simple DynamicImage extension to Vaadin’s Image  component, to load the image data from java.awt.image.BufferedImage instead of normal file. Also note that in order to run the application, the JVM must be set to “headless” mode using -Djava.awt.headless=true system parameter.

Going further

Sorry, that’s it. :) I never got further than this with my computer vision apps, but I learned something, and hopefully this also write-up gives you an idea of how to get started.

It was pretty simple after all. All you need to do is: 1.) build OpenCV Java library and install it to local Maven repository and 2.) clone the sample project  from GitHub. And you have a full setup for the most amazing computer vision web applications of your own.

The Web Dev Zone is brought to you by Stormpath—offering a pre-built, streamlined User Management API for building web and mobile applications. Plan On Building User Management? Buy It Instead.Download Our White Paper To Learn More.

Combining HTML5 Web Applications with OpenCV的更多相关文章

  1. HTML5 Web 客户端五种离线存储方式汇总

    最近折腾HTML5游戏需要离线存储功能,便把目前可用的几种HTML5存储方式研究了下,基于HT for Web写了个综合的实例,分别利用了Cookie.WebStorage.IndexedDB以及Fi ...

  2. Websocket 与代理服务器如何交互? How HTML5 Web Sockets Interact With Proxy Servers

    How HTML5 Web Sockets Interact With Proxy Servers Posted by Peter Lubberson Mar 16, 2010 With the re ...

  3. Where to Store your JWTs – Cookies vs HTML5 Web Storage--转

    原文地址:https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage Update 5/12/20 ...

  4. JavaScript多线程之HTML5 Web Worker

    在博主的前些文章Promise的前世今生和妙用技巧和JavaScript单线程和浏览器事件循环简述中都曾提到了HTML5 Web Worker这一个概念.在JavaScript单线程和浏览器事件循环简 ...

  5. Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Which of the following statement(s) is(are) correct?

    Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Whi ...

  6. HTML5 web Form表单验证实例

    HTML5 web Form 的开发实例! index.html <!DOCTYPE html> <html> <head> <meta charset=&q ...

  7. html5 web database

    html5 web database <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  8. HTML5 Web Storage

    Web Storage是HTML5 API提供一个新的重要的特性: 最新的Web Storage草案中提到,在web客户端可用html5 API,以Key-Value形式来进行数据持久存储: 目前主要 ...

  9. 深入HTML5 Web Worker应用实践:多线程编程

    HTML5 中工作线程(Web Worker)简介 至 2008 年 W3C 制定出第一个 HTML5 草案开始,HTML5 承载了越来越多崭新的特性和功能.它不但强化了 Web 系统或网页的表现性能 ...

随机推荐

  1. 【Unity3d】WWW类发起web连接

    初学unity3d,解决一个游戏与web服务器连接问题. 看了项目中原始代码,发现每次之前的程序员每次调用WWW类都需要写一遍StartCoroutine,然后各种重复代码. 于是写了一个简单的封装类 ...

  2. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  3. python全栈开发-前方高能-函数进阶

    python_day_10 一.今日主要内容 1. 动态参数 位置参数的动态参数: *args 关键字参数的动态参数 : **kwargs 顺序: 位置,*args,默认值,**kwargs 在形参上 ...

  4. Selenium2+python自动化-环境搭建

    一.selenium简介 Selenium 是用于测试 Web 应用程序用户界面 (UI) 的常用框架.它是一款用于运行端到端功能测试的超强工具.您可以使用多个编程语言编写测试,并且 Selenium ...

  5. https双向认证网站搭建

    新建网站 在搭建网站证书之前,我们先搭建好我们的网站 1.网站基本搭建 为我们的项目新建一个网站,按照如下的步骤来 1,打开IIS,右键单击网站弹出菜单,选择网站(如图1.1.1) 图1.1.1 2, ...

  6. 【sed】常用命令

    替换 替换某一整行 sed '1c hello' test #将第一行替换为hello str1替换为str2 sed 's/^str1.*/str2/' filename #以str1开头 sed ...

  7. [MYSQL]练习(一)

    本文转载自:http://www.cnblogs.com/DreamDrive/p/6193530.html 我只是想做一个自己的运维知识库,所以迫不得已做了搬运工 建表 DROP TABLE DEP ...

  8. Spark Shell Examples

    Spark Shell Example 1 - Process Data from List: scala> val pairs = sc.parallelize( List( ("T ...

  9. Python 数据图表工具的比较

    Python 的科学栈相当成熟,各种应用场景都有相关的模块,包括机器学习和数据分析.数据可视化是发现数据和展示结果的重要一环,只不过过去以来,相对于 R 这样的工具,发展还是落后一些. 幸运的是,过去 ...

  10. react native中props的使用

    react native中props的使用 一.props的使用 1:父组件传递的方式 在子组件中可以用this.props访问到父组件传递的值 <View> <Text> { ...