Month: February 2013

Using webcam in C# applications

Introduction

During our final year research project I had to find on things associated with visual images. While searching for those things I came across ways which can use web cam with a c# application. While doing digging in this area I came across various solutions, but initial ones didn’t took my interest. After going across some links I found that the most interesting thing which can use for this purpose is the AForge.NET framework. Therefore I thought of keeping a bookmark on that for anyone who is interested.

AForge.NET framework allows users to do many interesting things easily. It has already contains waste number of examples from just taking an image from a web cam to image recognition and neural networks. In most cases this AForge.NET framework will not require other libraries except basic c# libraries. In references you can find some cool things done using Aforge.NET framework.

In this post I’m going to explain on very basic application, which allows you to start & stop displaying web cam input and store a captured image. In a previous post I have done the same thing in Java using JMF framework.

Design of the application

First of all let’s have a look at the design of the application. It has two buttons, a pictureBox, and a comboBox on a Form. Their names are given as follows.

Application Design
Application Design

After the design you can start coding. But before start coding you need to download the Aforge.NET framework and add the following references to the project.

  • AForge.Video.dll
  • AForge.Video.DirectShow.dll

These files are reside inside Release folder of the downloaded folder. You can add those references through Solution Explorer window in Visual Studio. Thereafter you can add those references with ‘using’ keyword to any .cs file when required.

Coding

Now let me give a brief explanation on the coding. It initially takes the all video input devices when loading and display as a list in combo box. That is done through getCamList() method. Thereafter depending on the selected device, input will be shown in the pictureBox1. Connection between device input and pictureBox is done through a FrameEventHandler connected to video source.

Other than that basic functionality, Capture button will save the current image inside pictureBox along with the time.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video;
namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
 {
 private bool DeviceExist = false;
 private FilterInfoCollection videoDevices;
 private VideoCaptureDevice videoSource = null;
public Form1()
 {
 InitializeComponent();
 }
private void Form1_Load(object sender, EventArgs e)
 {
 getCamList();
 }
private void getCamList()
 {
 try
 {
 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
 comboBox1.Items.Clear();
 if (videoDevices.Count == 0)
 throw new ApplicationException();
DeviceExist = true;
 foreach (FilterInfo device in videoDevices)
 {
 comboBox1.Items.Add(device.Name);
 }
 comboBox1.SelectedIndex = 0;
 }
 catch (ApplicationException)
 {
 DeviceExist = false;
 MessageBox.Show("No capture device on your system");
 }
 }
private void start_Click(object sender, EventArgs e)
 {
 if (start.Text == "Start")
 {
 if (DeviceExist)
 {
 videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
 videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
 CloseVideoSource();
 videoSource.DesiredFrameSize = new Size(320, 240);
 videoSource.Start();
 start.Text = "Stop";
 }
 else
 {
  MessageBox.Show("Error: No Device selected.");
 }
 }
 else
 {
 if (videoSource.IsRunning)
 {
  CloseVideoSource();
  start.Text = "Start";
 }
 }
 }
//close the device safely
 private void CloseVideoSource()
 {
 if (!(videoSource == null))
 if (videoSource.IsRunning)
 {
 videoSource.SignalToStop();
 videoSource = null;
 }
 }
//eventhandler if new frame is ready
 private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
 {
 Bitmap img = (Bitmap)eventArgs.Frame.Clone();
 pictureBox1.Image = img;
 }
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
 CloseVideoSource();
 }
private void capture_Click(object sender, EventArgs e)
 {
  pictureBox1.Image.Save("Capture-" + DateTime.Now.ToString("HH-mm-ss tt") + ".jpg";);
 }
 }
}

The above code is the basis for any webcam application which uses Aforge.NET libraries. You are free to develop variety of other applications which using AForge.NET framework. Some of them may be object detecting systems, intelligent game applications, gesture recognizing systems, motion detecting systems, etc. Following is a snapshot of an object detecting application I created. Good luck with AForge.NET!

Simple Shape Detecting Application
Simple Shape Detecting Application

References

  1. AForge.NET framework official site – http://www.aforgenet.com/framework/
  2. AForge.NET samples – http://www.aforgenet.com/framework/samples/image_processing.html
  3. Simple webcam application – http://haryoktav.wordpress.com/2009/03/21/webcam-in-c-aforgenet/
  4. Shape Recognition application – http://leakingmemory.wordpress.com/2012/03/17/shape-recognition-using-c-and-aforge/
  5. How shape detector work – http://www.aforgenet.com/articles/shape_checker/
  6. Edge detection application – http://premsivakumar.wordpress.com/2010/12/13/edge-detection-using-c-and-aforge-net/
  7. Playing card recognition – http://www.codeproject.com/Articles/265354/Playing-Card-Recognition-Using-AForge-Net-Framewor
  8. Hand gesture recognition – http://www.codeproject.com/Articles/26280/Hands-Gesture-Recognition
  9. Recognition of handwritten digits – http://www.codeproject.com/Articles/143059/Neural-Network-for-Recognition-of-Handwritten-Digi
  10. Robotic samples – http://www.funnyrobotics.com/2010/12/brain-power-for-your-robot-aforgenet-c.html

Creating a Web or a Mobile Application with REST API using IXAPI

Introduction

During busy period with final year project works, somehow I managed to write this blog post. Today there are so many ways of building a web application and create a REST API. But in here I’m going to tell you a way you can easily create a web application with a REST API using IXAPI. The main advantage of this system is you don’t need to set up database connections, servers, write codes etc. All those fundamentals are provided with IXAPI. You just need to concentrate on the design only. After creating Web App, IXAPI allows clients apps to store, retrieve, modify or delete content stored with it, in RESTful manner.

In addition to that you can test your web application using the Playground facility. Auto generating guidance will help even for a new PHP, Ruby or Python developer to build clients very easily.

Steps to do

  1. First you need to create an account in IXAPI
  2. Create a new web application as you wish
  3. Try your web application with Playground and build clients

First Step

First you need to sign up to IXAPI website, nothing but the same procedure as other websites do. After your fist sign-in you will get an interface as follows.

First Look At IXAPI
First Look At IXAPI

Creating Web Application

In here I’m going to demonstrate on creating a simple web app which contain User (compulsory object), Student and Book objects. Since IXAPI is using an object-oriented model, “student has books” connection exists.

So, first click “New” button on the Active list of Applications (Home screen). Then add the application Name, description and so on. For this case I’m creating 2 objects – Student and Book as shown below.

Student Object
Student Object
Book Object
Book Object

As you can see you can specify the Field Description about the field, Field Type and whether the field is Mandatory. After creating Student and Book object, click next.

In Connect Object phase you can specify the connection between objects. In here I created the “Student has Books” as follow by simply drag and drop items.

Creating Connections
Creating Connections

That’s all. You are done with creating Web Application :-). Now you will be redirected back to first page and you will see your app is listed along with Application Key and Application Secret.

Description on Application
Description on Application

By clicking View you can see the details about objects and all REST calls associated with those objects. Through Example page, you will be given the client application code (REST calls) in PHP, Ruby and Python!

Playing in Playground

With the Playground option provided in IXAPI you can play around with your app as much as you want (not in free version ;)). Through that you can understand how to interact with web application in RESTful manner.

First of all REST calls you need to get an Access Token for your application by sending a GET request with Application Key and Application Secret.

Get Access Token
Get Access Token

Then add the access token you get as a Custom Token to go further in Playground.

Save Access Token for future REST calls
Save Access Token for future REST calls

Following figure will show creating a new Student and 2 Book objects, using POST request. The ids return from these calls will be used in future REST calls.

Creating Objects REST calls
Creating Objects REST calls

Then you can make those two books belongs to “Sam” student object. In the figure below ‘3’ and ‘4’ are identifiers of the Book objects created earlier. You can do this adding one by one too.

Connecting Books with a Student
Connecting Books with a Student

Now you can view the Books which belongs to a particular Student, using a GET request. ‘2’ is the identifier of the student “Sam”

Get Book list
Get Book list

Conclusion

Rather than wasting time on setting up databases and writing 100 lines of code, you can simply design the web application like mentioned. With further improvements in design you can make these REST calls secure too.  Not only creating and connecting objects, but also there are lot more you can do with IXAPI. Build awesome applications and save money & time for your personal life.  Have fun with IXAPI!

Resources

IXAPI – https://www.ixapi.com/

Video Tutorial on creating mobile application – http://www.youtube.com/watch?v=1POlrwW63VA