Thursday, December 26, 2013

Capture a Video from Webcam with JavaCV

Last post I told how crazy I was and why did I wanted put my hands on image processing. If you haven't read it yet try that first Getting Started with Opencv via Javacv. It is time to get start with some advanced stuffs. Don't get too much curious because I'm just getting started with this. Though we have used Webcam to capture a photos, main purpose of that is to get on video chat. So It means that we can record video from that. As I mentioned in my early post I have some experience with JMF to do that. But since now JMF is dead and we are dealing with my crazy idea it is better to try with JavaCV.
It is time that "You are being watched. The government has a secret system: a machine that spies on you every hour of every day" - Person of Interest
So I'm gonna tell you how to capture some videos from Webcam with JavaCV.
Lets get started. In-order to capture videos you must include appropriate ffmpeg...jar file to your class path. This can be found in the JavaCPP bundle that you already downloaded. Make sure you only include the suitable library version. That means if you are using x64 verison then used x64 version otherwise go with x86 version.

So all together we need following 4 libraries (Since I'm experiment using x64 here I mentioned latest x64 versions that I used)

  1. javacv.jar
  2. javacpp.jar
  3. opencv-2.4.6.0-windows-x86_64.jar
  4. ffmpeg-20130915-git-7ac6c63-windows-x86_64.jar

Create a new java class and have following code on that.

 import com.googlecode.javacv.CanvasFrame;  
 import com.googlecode.javacv.FFmpegFrameRecorder;  
 import com.googlecode.javacv.FrameGrabber;  
 import com.googlecode.javacv.FrameRecorder;  
 import com.googlecode.javacv.OpenCVFrameGrabber;  
 import com.googlecode.javacv.cpp.avutil;  
 import com.googlecode.javacv.cpp.opencv_core;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  

 class VideoTest {  
   public static void main(String[] args) {  
     try {  
       OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);  
       grabber.start();  
       opencv_core.IplImage grabbedImage = grabber.grab();  
       CanvasFrame canvasFrame = new CanvasFrame("Video with JavaCV");  
       canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());  
       grabber.setFrameRate(grabber.getFrameRate());  

       FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("D:/Video/mytestvideo.mp4", grabber.getImageWidth(), grabber.getImageHeight());  // specify your path
       recorder.setFormat("mp4");  
       recorder.setFrameRate(30);  
       recorder.setVideoBitrate(10 * 1024 * 1024);  

       recorder.start();  
       while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {  
         canvasFrame.showImage(grabbedImage);  
         recorder.record(grabbedImage);  
       }  
       recorder.stop();  
       grabber.stop();  
       canvasFrame.dispose();  

     } catch (FrameGrabber.Exception ex) {  
       Logger.getLogger(VideoTest.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (FrameRecorder.Exception ex) {  
       Logger.getLogger(VideoTest.class.getName()).log(Level.SEVERE, null, ex);  
     }  
   }  
 }  
Now compile and run this file. You can see that your camera get open and your beautiful face appearing on a window. Just wait for few seconds and close it. Then go to the file path that you specified. If you did everything as I mentioned you can see a video there. Run that from your favorite player.

you can see that I have set frame rate, bit rate and format as well. Try with different values and check what is get change. There are also some other parameters as well.

 recorder.setVideoQuality(videoQuality);  
 recorder.setVideoCodec(videoCodec);  
 recorder.setPixelFormat(pixelFormat);  

Try them as well. I'm didn't discuss how to detect face in this post. But don't get upset. It will be surely on next post.


Wednesday, December 25, 2013

Getting started with OpenCV via JavaCV

Couple of weeks back when I was watching Person of Interest's (My all time favorite TV show) latest episode I was getting a crazy idea of what if I can build a machine like that. (Of course it so damn crazy but seriously I got that feeling) Then I thought of why don't I give a try on some image processing stuffs that are heavily used in the so called "Machine".


I've never tried on image processing before. Even I didn't follow the course module at uni. So I started as most of us do Google it. But I didn't google it like 'Image processing for beginners'. I knew it is some what advanced stuffs and I didn't want to get touch with advanced stuffs with some languages that I have rarely used. So what I thought of was to try out image processing with Java first. As I mentioned earlier, since I was inspired by the Machine, I wanted to get started with my crazy idea by accessing webcam. I had some previous experience with JMF in 1st year project. But now JMF is almost a dead project. Therefore I had to look for something new. On my way I have found another couple of libraries that are used to access webcam via Java. But what caught my eyes was JavaCV. I have heard about OpenCV but never tried that before. But this time it's with Java. So why to wait ;)

As some of you may know OpenCV is stands for Open source computer vision library. (I don't know how they shorten it to OpenCV) It was started my Intel way back in late 1990's
"OpenCV project was initially an Intel Research initiative to advance CPU-intensive applications, part of a series of projects including real-time ray tracing and 3D display walls." - wikipedia
"It has C++, C, Python, Java and MATLAB interfaces and supports Windows, Linux, Android and Mac OS. OpenCV leans mostly towards real-time vision applications and takes advantage of MMX and SSE instructions when available. A full-featured CUDA and OpenCL interfaces are being actively developed right now. There are over 500 algorithms and about 10 times as many functions that compose or support those algorithms. OpenCV is written natively in C++ and has a templated interface that works seamlessly with STL containers." - OpenCV developer team

I think that is enough about background and my nonsense. It is time to get touch with JavaCV.
In order to get start with JavaCV download following things to your machine

  1. JavaCV from https://code.google.com/p/javacv/downloads/list --> javacv-0.6-bin.zip
  2. OpenCV from http://sourceforge.net/projects/opencvlibrary/files/ install this to any place that you like 
  3. JavaCPP from https://code.google.com/p/javacv/downloads/detail?name=javacv-0.6-cppjars.zip

I'm working with Netbeans IDE so if you are not having it download and install it as well. But it is not necessary as long as you are OK with setting up class paths and stuffs when compiling and running.

First I'm going to show you how to capture a photo from your webcam and save it. I'm amusing that all are familiar with Netbeans.
First create a new Java project and add following libraries to your project

  1. javacv.jar
  2. javacpp.jar (These 2 can be found in JavaCV .zip file that you downloaded)
  3. opencv-2.4.6.1-..jar (Use the library that is appropriate to your pc. If you are having x64 machine then use x64 version) This can be found in the JavaCPP .zip file that you download
Then create a new java file and have following code in that
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage; 
public class JavaCV {
   public static void main(String[] args) {
     OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
     try {
       grabber.start();
       IplImage img = grabber.grab();
       if (img != null) {
         cvSaveImage("D:/Photos/capture.jpg", img); // give a path that you like
       }
       grabber.stop();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }

Now run this out. As you run the application you can see that your webcam get open and get close quickly. Check the path that you specified on the code and you can see a image being save there.


Wednesday, August 14, 2013

නොසැලෙන්න

හරසුන් වදන්
සැම සිත් පාරවයි
නොමනා කතා
දෙසවන් රිදුම් දෙයි
නමුත් නුඹ නොසැලෙන්න 
නොතැවෙන්න කිසිදා 
ඇත නුඹට හැකියාව
දිරිය හා ඇල්ම
නොතැවී ඉඩදෙන්න 
නුඹේ ඔය පන්හිඳට
සිනිඳු පිටු කොල අතර
නිදහසේ දිවයන්න

Monday, March 25, 2013

සැබෑවක් වෙලා


සිහිනය සැබෑවක් වෙලා
දැසේ කැලුමන් සදා
නුඹගේ දැකුමන් පවා
මා හද පුබුදු කලා
නිශායම පුරා නුඹගේ කෝලකම් අසා
පින්න මල් පිපී දවසම සුවඳවත් කලා
නුඹේ ඔය කතා මාගේ දෙසවන් පුරා
විඳින්නට හිතයි දවසම සුවඳ රස කතා
සිහිනය සැබෑවක් වෙලා
දැසේ කැලුමන් සදා
නුඹගේ දැකුමන් පවා
මා හද පුබුදු කලා

Thursday, July 12, 2012

Some things I've learnt about programming

මේක මම අද hacker news තුල සැරිසරද්දී කියවපු අපූරු ලිපියක්. මේක සිංහලට translate කරන්න කියල හිතුවත් එතකොට කියලා තියන එක වෙනස් වෙන්න පුලුවන් නිසා එහෙමම copy paste කලා :)

මේක ලියලා තියෙන්නේ අවුරුදු 30ක් field මේ එකේ xpernz තියන ඩයල් එකක්. ඒක නිසා තාමා මේ field එකේ අඳුරේ අතපත ගාන අපිට මේක ගොඩක් වැදගත් වෙයි කියලා හිතුවා

I didn't composed this but just copy pasted here. All the appreciations goes to John Graham
පහල link එක click කරලා original artical එක බලාගන්න පුලුවන්.

some things I've learnt about

"
I've been programming for over 30 years from machines that seem puny today (Z80 and 6502 based) to the latest kit using languages that range from BASIC, assembly language, C, C++ through Tcl, Perl, Lisp, ML, occam to arc, Ruby, Go and more.

The following is a list of things I've learnt.

0. Programming is a craft not science or engineering

Programming is much closer to a craft than a science or engineering discipline. It's a combination of skill and experience expressed through tools. The craftsman chooses specific tools (and sometimes makes their own) and learns to use them to create.

To my mind that's a craft. I think the best programmers are closer to watchmakers than bridge builders or physicists. Sure, it looks like it's science or engineering because of the application of logic and mathematics, but at its core it's taking tools in your hands (almost) and crafting something.

Given that it's a craft then it's not hard to see that experience matters, tools matter, intuition matters.

1. Honesty is the best policy

When writing code it's sometimes tempting to try stuff to see what works and get a program working without truly understanding what's happening. The classic example of this is an API call you decide to insert because, magically, it makes a bug go away; or a printf that's inserted that causes a program to stop crashing.

Both are examples of personal dishonesty. You have to ask yourself: "Do I understand why my program is doing X?". If you do not you'll run into trouble later on. It's the programmer's responsibility to know what's going on, because the computer will do precisely what it's told not what you wish it would do.

Honesty requires rigor. You have to be rigorous about ensuring that you know what your program does and why.

2. Simplify, simplify, simplify

Tony Hoare said: "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."

Simplify, refactor, delete.

I'd rephrase Hoare's maxim as "Inside every large, complex program is a small, elegant program that does the same thing, correctly".

Related to this is the 'small pieces loosely joined' philosophy. It's better to structure a program in small parts that communicate than to create some gigantic monolith. This is partly what has made UNIX successful.

3. Debuggers are sometimes a crutch, profilers are not

I almost never use a debugger. I make sure my programs produce log output and I make sure to know what my programs do. Most times I can figure out what's wrong with my code from the log file without recourse to a debugger.

The reason I don't use a debugger much is I think it leads to lazy thinking. Many people when faced with a bug reach for the debugger and dive into setting breakpoints and examining memory or variable values. It's easy to become enamored with such a powerful tool, but a little bit of thinking tends to go a long way. And if your program is so complex that you need a debugger you might need to go back to #2.

(Aside: having said all that, one of the programmers I most respect, John Ousterhout, seemed to spend all day in the Windows debugger).

On the other hand, profilers are essential if you need to understand performance. You'll never cease to be amazed what a profiler will tell you.

4. Code duplication will bite you

Don't Repeat Yourself. Do everything just once in your code.

This is related to #2, but is a special case. Even a simple piece of code that's duplicated will lead to trouble later when you 'fix' one version and forget about the other one.

5. Be promiscuous with languages

Some people get obsessed with a specific language and have to do everything in it. This is a mistake. There is not single greatest language for all tasks.

The key thing is to know which language in your toolbox you'll use for which problem. And it's best to have lots of tools. Try out different languages, build things in them.

For example, perhaps you'll not use Python or ML very much but you'll have played with list comprehensions and seen their power. Or you'll dabble in Go and will have seen how it handles concurrency. Or you'll have used Perl and seen the power of really flexible string handling. Or you'll have used PHP to quickly build a dynamic web page.

I hate language wars. They're basically for losers because you're arguing about the wrong thing. For example, in my hands PHP is a disaster, in the hands of others people make it sing. Similar things can be said about C++.

6. It's easier to grow software than build it

This is related to #2. Start small and grow out. If you are attacking a problem then it's easier to grow from a small part of the problem that you've tackled (perhaps having stubbed out or simulated missing parts) than to design a massive architecture up front.

When you create a massive architecture from the start you (a) get it wrong and (b) have created a Byzantine maze that you'll find hard to change. If, on the other hand, you work from small pieces that communicate with each other, refactoring will be easier when you realize you got it wrong from the start.

The root of this is that you never know what the truly correct architecture will look like. That's because it's very rare to know what the external stimuli of your program will be like. You may think that you know, say, the pattern of arriving TCP traffic that your mail server will handle, or the number of recipients, or you may not have heard of spam. Something will come along from outside to mess up your assumptions and if your assumptions have been cast into a large, interlocked, complex program you are in serious trouble.

7. Learn the layers

I think that having an understanding of what's happening in a program from the CPU up to the language you are using is important. It's important to understand the layers (be it in C understanding the code it's compiled to, or in Java understanding the JVM and how it operates).

It helps enormously when dealing with performance problems and also with debugging. On one memorable occasion I recall a customer sending my company a screenshot of a Windows 2000 crash that showed the state of a small bit of memory and the registers. Knowing the version of the program he had we were able to identify a null pointer problem and its root cause just from that report.

8. I'm not young enough to know everything

I've still got plenty to learn. There are languages I've barely touched and would like to (Erlang, Clojure). There are languages I dabble in but don't know well (JavaScript) and there are ideas that I barely understand (monads).

PS It's been pointed out that I haven't mentioned testing. I should have added that I do think that test suites are important for any code that's likely to be around for a while. Perhaps when I've been programming for another 30 years I'll have an answer to the question "Do unit tests improve software?". I've written code with and without extensive unit tests and I still don't quite to know the answer, although I lean towards unit tests make a difference.
"

Sunday, June 10, 2012

හරිම අපූරුයි නුඹනම්

♥
දැනගතිමි මම
සසර පුරාවට මා පිං කර ඇති බව
සියල්ලට වැඩියෙන්
කෙතරම් වාසනාවන්තද වග
ලබන්නට කෙනෙක් නුඹවන්
අතිනත පටලවාගෙන ඇවිද යන්නට
ඉමක් නොපෙනෙන අනන්තය වෙත
පිරිමදින්නට නුඹේ නළලත
විඳින්නට ආදරය
නුඹ මා ලය මත
හොවාගෙන
♥
♥



 ♥   ♥                         ♥   ♥
♥              ♥             ♥             ♥
♥                        ♥                        ♥
♥                                                ♥
♥                                          ♥
♥                                   ♥
♥                            ♥
♥                   ♥
♥          ♥
♥

Tuesday, June 5, 2012

නුඹමයි


සොඳුරු මල් පිපෙන වසන්තය

රැගෙන ආවේ නුඹමයි
තෙලිතුඩින් ඇඳෙන්නේ නුඹේ සිතුවමමයි
පන්හිඳ ලියන කවියේ එලිසමය නුඹමයි
මා හදගැස්මේ රාවයත් දැන් නුඹමයි