Tuesday, February 11, 2020

Rest Service using WEB API C#

This post will show a simple example of REST service built on Micrsoft WEB API framework.
WEB API is an extensible framework from microsoft ASP.net which allows to built services to be accessed from different platforms.
Web API services can be accessed from any other platforms like mobile phone application (Android apps), desktop application or and web application.

For this example I am going to choose ASP.net Core web application.
Just follow the two steps and you are done.
Step 1 : Add new project with project type selected as in the image below.
Step 2: Select API type, as this post is intended to create a simple API
Now you are done. The project selected already prepare a nice example for the start.
Press F5 to execute and see the output in browser.
The project is already created with a sample Model and controller. 
The differnce between MVC and WEB API is that MVC returns HTML VIEW but the WEBAPI returns response either is json or raw or xml.

Now we test the rest api call. We are open to choose any tools for this. Postman, SoapUI and lot more other tools are available. I am going to use SoapUI.
At first we need to ensure that the project we just created is running. Now we open the SopUI.
Create a new rest project.


In the URI link we can put the project link. In my case it is  "http://localhost:56506/weatherforecast"
And click OK.
Then we click on the Play/Start button.


So the APi is ready to access from outside.
And thats all we need.
In the next post I will put some example of WEB API hosting.


Monday, December 9, 2019

Buffer in Reactive C#

React is smart design pattern and quit handy for observable pattern. To reduce frequent communication Buffer is used to subscribe an IObservable. I am going to discuss a simple example with c#.net IObservable and react. Assuming that the reader have the basic idea of React. IObservable, IDisposable.
Example: Assume that there is already an implementation ReadIEventData which is IObservable .IEventData is a custom Interface which provide some data integar values.
Lets say, we have to show the event data into a chart and the amount of data is too much frequent to handle each and every data via a communication channel(i.e: tcp/ip, I2C).
To reduce the communication we want to get a bulk amount data after a certain period and then process those data into another thread.

A simple solution for that is using the Buffer for a timespan.

See the difference between two lines of code below:

myDataProvider.ReadIEventData.Finally(OnFinally).Subscribe(onNext);

Above line, subscribes the ReadIEventData and respond for every data into the method onNext.

myDataProvider.ReadIEventData.Buffer(TimeSpan.FromSeconds(2)).Finally(OnFinally).Subscribe(onNextBuffer);


Above line, subscribes the ReadIEventData  but wait for 2 seconds. Collect all the subscribed data within 2 seconds and then forward it to onNextBuffer.

So, the implementation should be like
private OnNext(IEventData singleEventData) { //Do something here }
and
private OnNextBuffer(IList bufferedEventData) { //Do something on the collection of evnetData }


-----------------------------------------------------------------------------------

Tuesday, October 1, 2019

Frame difference approach for Movement detection using Kinect and OpenCV and ROS.

Frame Difference

Movement detection is very salient feature in modern image processing based application. There are several approaches can be taken for detecting movement. Background subtraction is a well-known approach for computer vision based object detection. Therefore, there are already a lot of works in the literature focused on it. Background subtraction is very useful when the background is fixed and also the camera is stationary position.
To detect any kind of change from a camera vision or video, the difference of two consecutive frames is one of the most effective approaches. The frame difference can be defined as equation below: 
Frame Difference

f(x, y, t) is a frame at time t. The immediate next frame at (t+1) is f(x, y, t+1) and th represents the threshold value parameter for decision. Three Frame difference is an improved method from two frame difference and it uses three consecutive frames to find the difference. From the difference of three frame images, the changes or movement detection is done using the contour object.
Next two equations are for three frame difference method. The additional option is that there is a scope for adding multiple threshold value. For n frame difference method there is an option to use (n-1) threshold values which can give more flexibility to find the moving contours.
Three Frame difference

fk(x,y)is the k-th frame. T1, T2 is thresholds with different values.  and  are the binarization of two adjacent frames difference. 


ROS

Robot operating system is chosen for robot based implementation of movement detection.Robot Operating System (ROS) is an open-source, heterogeneous and scalable P2P network-based robotics framework. ROS provides wide range of libraries and tools to help software developers creating innovative robot applications in an organized pattern. The benefit of using ROS is to get some useful essential features which will reduce the development time and complexity by providing device drivers, libraries, visualizers, message-passing, package management, access to third party tools, hardware abstraction, and more. ROS is licensed under an open source, BSD license. The basic advantage of ROS is code reuse for the robotic developer and researcher in a common and generalized platform. ROS is a language-independent architecture (C++, python, lisp, java, and more), scalable platform (ARM CPUs to Xeon Clusters) and intent to enable researchers to rapidly develop new robotic systems by increasing the reusability through use of standard tools and interfaces.
There are not many Robot frameworks out there which support the development of general purpose robot software across many platforms. This makes it difficult to accomplish even simple and trivial tasks which might seem simple to humans but the Robot developer has to navigate and sort through many variations between instances of tasks and environments. Having a general purpose framework like ROS helps us to create complex and robust robot behavior across many platforms which have got the support of ROS.

How to use Kinect in Robot Operating System?

Quick Intro about Kinect:

Kinect is a RGB-D based motion sensing input device product line by Microsoft. It was announced and demonstrated for Xbox 360 video game platform named as Project Natal in June 2009. The first generation Kinect was first released on November 2010. Since then it attracts the attention of researchers and robotics developer community due to its immense possibility compare to traditional vision based system.

 Kinect was developed based on the software technology by Rare and on range camera technology by Prime Sense which developed a system that can interpret specific gestures, making completely hands-free control of electronic devices. Kinect features an RGB camera, depth sensor and multi-array microphone. The depth sensor consists of IR laser projector combined with CMOS sensor which captures 3D video data. The RGB camera is located between the IR projector and IR camera, which has no role in depth measurement.
Kinect was originally designed and released to be used in Microsoft Xbox 360 game console. No external driver was provided by the manufacturer. But as Kinect grabs attraction to the researchers and robotic development community, in November 2010, Adafruit Industries offered a bounty for an open-source driver for Kinect and in the same month Adafruit announced Hector Martin as the winner who produced a Linux driver for Kinect that allows using both of the RGB camera and depth sensitivity functions. It was named Open Kinect libfreenect which is the core library for accessing the Microsoft Kinect. Another open source framework, OpenNI framework was introduced by OpenNI organization. It provides for Kinect an application programming interface (API). There are 12 different nodes available in OpenNI framework for different functionalities.

Quick Intro on ROS:

Robot Operating System (ROS) is an open-source, heterogeneous and scalable P2P network-based robotics framework. ROS provides wide range of libraries and tools to help software developers creating innovative robot applications in an organized pattern. The benefit of using ROS is to get some useful essential features which will reduce the development time and complexity by providing device drivers, libraries, visualizers, message-passing, package management, access to third party tools, hardware abstraction, and more. ROS is licensed under an open source, BSD license. The basic advantage of ROS is code reuse for the robotic developer and researcher in a common and generalized platform. ROS is a language-independent architecture (C++, python, lisp, java, and more), scalable platform (ARM CPUs to Xeon Clusters) and intent to enable researchers to rapidly develop new robotic systems by increasing the re-usability through use of standard tools and interfaces.
There are not many Robot frameworks out there which support the development of general purpose robot software across many platforms. This makes it difficult to accomplish even simple and trivial tasks which might seem simple to humans but the Robot developer has to navigate and sort through many variations between instances of tasks and environments. Having a general purpose framework like ROS helps us to create complex and robust robot behavior across many platforms which have got the support of ROS.
  • Peer to Peer
  • Multi Language Support
  • Free and open source
Now to work with Kinect and ROS in Linux environment, a ROS based kinect driver is needed.  Once the Linux environment is set with ROS installation the Freenect stack setup is enough to communicate with kinect sensor through ROS. A sudo apt get command is enough for that.
sudo apt-get install libfreenect-dev
sudo apt-get install ros-indigo-freenect-launch
For executing the command a internet connection is needed. This will install the kinect driver into ROS. After that connect the kinect device with USB and also make sure the power cable is connected. Then run the freenect launch command to start driver.
roslaunch freenect_launch freenect.launch
The package link is available here.

Cross-thread operation not valid: Control '[UI Control Name]' accessed from a thread other than the thread it was created on

For windows program using Threads or Task there is a common exception raised "Cross-thread operation not valid: Control '[UI Control Name]' accessed from a thread other than the thread it was created on"

The reason is due to multiple task or threading the the drawing object or UI control is accessed via different threads and which is not acceptable during runtime.

The common solution is to use INVOKE method.

A sample code example is given here with a very simple explanation:


private void button_action_Click(object sender, EventArgs e)
        {
          
            Task.Factory.StartNew(() =>
            {
                AnyMethod(this.text_live_frequency, this.button_live_action);
            });            

        }

 private void AnyMethod()
        {
            
                System.Threading.Thread.Sleep(5);

               //chart is ChartInfo type             
                chart.Invoke(new Action(() => { ChartRefreshLive(chart, time, chartCount); })); 

                //textbox
                 this.text_live_frequency).Invoke(new Action(() => 
                                                 {
                                                      this.text_live_frequency.Enabled = false; 
                                                   }));

        }



As AnyMethod is invoked from task factory, modifying the textbox or chart would raise an exception if the Invoke was called.
The above code will work as invoke method puts it to the original thread.

Friday, April 6, 2018

Stored procedure CREATE/ALTER in a same script.

In a continuous system integration process, we need to create/alter stored procedure.This can make sometime problem/error in the deployment phase.
For this reason it is a good practice to check with the SP name if the object exist and then always go for the ALTER procedure.


Thursday, June 6, 2013

How to enable Filestream feature in SQL server 2008


  1. Go to  SQL Server Configuration Manager from (Start >  Programs > Microsoft SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager)
  2. Now go to the SQL Server Services node and select the SQL Server instance you want to modify SQL Server (MSSQLSERVER)
  3. Right click and open Properties
  4. Click the FILESTREAM tab and select both of the
         checkboxes to enable FILESTREAM and enter a share name for the files, as
         shown below:
SQL Server Configuration Manager
Restart the server.

Rest Service using WEB API C#

This post will show a simple example of REST service built on Micrsoft WEB API framework. WEB API is an extensible framework from microsof...