Saturday, January 21, 2017

Control Mouse Pointer Using Web Cam (Python/OpenCV)

I have created this program to move the mouse pointer according to a orange yellow blob, located on forehead. But you can simply change this to identify any other object and move the pointer according to that.



################################################################################################

from cv2 import cv
import win32api
import win32gui, win32con
import sys
import time

posx=0
posy=0

########### You can add any type of a blob in to this algo ####################
########### For example you can use a nose detection algorythm in here##########
def getthresholdedimg(im):
    imghsv=cv.CreateImage(cv.GetSize(im),8,3)
    cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)                # Convert image from RGB to HSV
    imgthreshold=cv.CreateImage(cv.GetSize(im),8,1)
    cv.InRangeS(imghsv,cv.Scalar(23,100,100),cv.Scalar(25,255,255),imgthreshold) ## catch the orange yellow blob
    return imgthreshold
#################################################################################
    
def getpositions(im):
    leftmost=0
    rightmost=0
    topmost=0
    bottommost=0
    temp=0
    for i in range(im.width):
        col=cv.GetCol(im,i)
        if cv.Sum(col)[0]!=0.0:
            rightmost=i
            if temp==0:
                leftmost=i
                temp=1      
    for i in range(im.height):
        row=cv.GetRow(im,i)
        if cv.Sum(row)[0]!=0.0:
            bottommost=i
            if temp==1:
                topmost=i
                temp=2  
    return (leftmost,rightmost,topmost,bottommost)
    
capture=cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720)
frame=cv.QueryFrame(capture)
test=cv.CreateImage(cv.GetSize(frame),8,3)
cv.NamedWindow("output")
previous_x = 0
previous_y = 0
while(1):
    frame=cv.QueryFrame(capture)
    cv.Flip(frame,frame,1)
    imdraw=cv.CreateImage(cv.GetSize(frame),8,3)    # we make all drawings on imdraw.
    imgyellowthresh=getthresholdedimg(frame)    # we get coordinates from imgyellowthresh
    cv.Erode(imgyellowthresh,imgyellowthresh,None,1)# eroding removes small noises
    (leftmost,rightmost,topmost,bottommost)=getpositions(imgyellowthresh)
    if (leftmost-rightmost!=0) or (topmost-bottommost!=0):
        lastx=posx
        lasty=posy
        posx=cv.Round((rightmost+leftmost)/2)
        posy=cv.Round((bottommost+topmost)/2)
        if lastx!=0 and lasty!=0:
                win32api.SetCursorPos((posx,posy))

    cv.Add(test,imdraw,test)
    cv.ShowImage("output",test)
    if cv.WaitKey(10) >= 0: 
        break
cv.DestroyWindow("output")

Wednesday, August 31, 2016

What "malloc" does ?

the word "malloc" derived from the term "memory allocation"

Again, imagine you have a computer that has 16 bit memory like follow.

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

What malloc does is reserve certain part of a memory that we requested.

Imagine about a situation where you need to create an array with 5 char type elements.

For the work you are going to use malloc
----------------------------------------------------------

First you need to tell what type of the elements that you are going to store

In our case its "char"

Why we need to tell it to computer ?

you know that hardware works with bites not with high level types like char/string/int .. those are some terms that we made for our ease.

so convert our thoughts to bites we need to tell that this is the data type we are going to use.. Then program can convert it to bites and tell it to computer

for our case we can do it like this

return_we_expect = (char*) .....

ok.........

then we need to use malloc for allocation for malloc we should give number of bytes that it should allocate from memory

if you know the exact bite value of your data type you can do it like this.

1char = 1bit
#of elements = 5
malloc(5 * 1)

but when it comes to the complex data types it is hard to memories the bit values.. so we can do a trick ..

as i toled, program knows bit values per each data type. why not we ask help from it :)

malloc(5 * sizeof(char))

yes..... now we can combine this thing

return_we_expect = (char*)malloc(5*sizeof(char))

Then what mallock does ?

----------------------------------------------------------------------
it simply allocates memory according to our scenario.

in our case malloc need to allocate 5*1 = 5 bits.

Unfortunately, meantime we are discussing above, 5 of our 16 bit memory has occupied by a nother program :( . you can identify them by X marks


| X | 0 | X | 0 |

| 0 | X | 0 | 0 |

| 0 | 0 | 0 | X |

| 0 | X | 0 | 0 |

But malloc dose not give up ..

In the basics of array it should do a adjacent memory locations.

so malloc need to find out a better place that has 5 empty slots..

| X | 0 | X | 0 |

| 0 | X | 1 | 1 |

| 1 | 1 | 1 | X |

| 0 | X | 0 | 0 |

Ok done ....

now malloc done its work .. but how we can identify the location malloc allocated... That's why it returns the address of start point and size of the allocation.

Than we can use the location



linked list are not stored in adjacent memory locations as in array

Imagine that your computer has a 16 bit memory which is initially nil, like follow

|  0  |  0  |  0  |  0  |

|  0  |  0  |  0  |  0  |

|  0  |  0  |  0  |  0  |

|  0  |  0  |  0  |  0  |

0 represents the free spaces
1 represents the occupied spaces
* represents the filed spaces

If you define a array with 5 elements whare 1 element takes 1 bit of memory it will allocate the memory as follow.

|  1  |  1  |  1  |  1  |

|  1  |  0  |  0  |  0  |

|  0  |  0  |  0  |  0  |

|  0  |  0  |  0  |  0  |

When you fill the array with first elements it will fill the 0th allocation

| * | 1 | 1 | 1 |

| 1 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

and when its second

| * | * | 1 | 1 |

| 1 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

But if you define an linked list for the same perpose, allocation is one at the initiation. That allocation is for the details of "Where to start".

| 1 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

When you fill the first element it uses the allocated space as follow. Additionaly it allocates a free space for next element

| * | 1 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

| 0 | 0 | 0 | 0 |

Then we can add second element

|  *  |  *  |  1 |  0  |

|  0  |  0  |  0  |  0 |

|  0  |  0  |  0  |  0 |

|  0  |  0  |  0  |  0 |

Think that while we adding the 3rd element 4th and 5th location were allocated by a nother process.

We use x for the locations allocated by third party

|  *  |  *  |  1  |  X |

|  X |  0  |  0  |  0 |

|  0  |  0  |  0  |  0 |

|  0  |  0  |  0  |  0 |

No Problem with that ... When we fill the 3rd element it will allocate 5th space for the 4th element of array list as follow


|  *  |  *  |  *  |  X |

|  X |  1  |  0  |  0  |

|  0  |  0  |  0  |  0  |

|  0  |  0  |  0  |  0  |

This is ok becase linked list keeps the location of next element as metadata..

As you see in Linked list we don't need any order for memory allocation. so we can say that  linked list are not stored in adjacent memory locations as in array.

Thursday, March 24, 2016

The concept of "e-man"

A man after 20th century saw lot of things. He i s the person who really experienced the benefits of industrial revelation. 

He was the man who invented computer which change the world so fast. 

Internet that he made was able to connect people. Then it connected people with devices. The novel trend is connecting things with things. This concept we identified as "Internet of things".

Due to this interconnection, knowingly or unknowingly we all has a representation in internet. Previously it's only a electronic ID that we had as representation. But with the vast software systems like Facebook and google,now it is more than an ID.

Every user feed their personal data in to vast scale software systems. So that the ID getting to rich with details like name,occupation,address, email address etc. 

Here we can see a representation of a person. whatever happen to the actual living person, this ID continues. we can use this id to describe relevant person.we will call this as e-appearance 

People getting to think that they can use this e-appearance  to identify a real person. Actually this is a false idea. Imagine a person who has few email accounts. This made him to have many forms of representation on internet. So situation like this, made it hard to identify the actual responsible body.

Then how to solve this????

Think about a real human. how we distinguish one man from another. is it just the appearance and name ?

We will make a small experiment. 

  • You need to find 3 people who knows each other very well, for a long time (more than 5 years). (Imagine their names as A B C)
  • two of them need to be unisex and has identical height and body shape.(B and C)
  • Select those two (B and C) and fetch them to a separate room.
  • dress them same and give them (B and C) a unique mask to ware.
  • After that take them (B and C) to their home and ask to do the day today works as usual.
  • You need to keep a video footage of each person.
  • Then show the video to 3rd (A) person and ask him to identify the person. 


He(A) will identify the person.

What is the fact that A uses to distinguish B and C ?

it is the behavioral pattern. A knows B and C for long time. So A has an idea about the behavior of each. So what ever the appearance they have A can identify them.

So the answer is name and appearance is not the only facts that made a person unique, but combination of name, appearance and behavioral pattern.

In internet also we have unique behavioral pattern. we call this pattern as e-behavior .What ever that we do, where ever that we are using internet, what ever the device we use, knowingly or unknowingly we keep e-behavior . 

If we can combine e-behavior with the  e-appearance we can identify the real living person who's behind the scene. 

The combination of e-appearance and  e-behavior I would like to introduce as e-man.


I wold Like to make an open invitation for the propel who are interesting on this research.
+94714969763
sandaru.weerasooriya@gmail.com








  

Tuesday, July 21, 2015

Debug Windows Service Application


  Windows service is a computer program that operates in the background. Programmers are widely using service like approach for the programs that needs to be run during the entire active time of computer but require a less user interaction.


   Visual studio provides a template for the developers to implement these kinds of applications.

According to myself, one of the main challenge of the windows service application is debugging. Of cause you cant run the service application as it is, without installing it as a windows service or change it to a console application. 

We have two approaches to debug windows services. As the first one to identify the problems in actual working environment we can create a log file foe the windows service. Personally i prefer separate log file like text file for the purpose.We can implement a simple method to the log every important steps of service in to this file. Sample class that i used to log activities can shown below 

Code:
public class ProgramLog
    {
        string fileName = "";//Global variable to store file name

        #region method to handle usual log records
        public void log(string text)//create normal Log text
        {
            fileName = "Log\\" + DateTime.Now.Date.ToString("d").Replace('/', '_') + ".txt";
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory+fileName))
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Append))
                using (TextWriter tw = new StreamWriter(fs))
                {
                    tw.WriteLine(text);
                    tw.Flush();
                    tw.Close();
                    fs.Close();
                }
            }
            else
            {
                createFolder();
                log(text);
            }
        }
        #endregion

        #region log Error record 
        public void logError(string text, string className,int LineNumber, string Stacktrace)//create error text
        {
            fileName = "Log\\" + DateTime.Now.Date.ToString("d").Replace('/', '_') + ".txt";
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName))
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Append))
                using (TextWriter tw = new StreamWriter(fs))
                {
                    tw.WriteLine("**************************ERROR****************************");
                    tw.WriteLine(text);
                    tw.WriteLine("In Class :{0}", className);
                    tw.WriteLine("In Line :{0}", LineNumber);
                    tw.WriteLine("ERROR :{0}",Stacktrace);
                    tw.WriteLine("***********************************************************");
                }
            }
            else
            {
                createFolder();
                logError(text,className,LineNumber,Stacktrace);
            }
        }
        #endregion

        #region create folder to store log files
        public void createFolder()//create a folder for Log files
        {
            try
            {
                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Log"))
                {
                    string folderName = "Log";
                    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + folderName);
                    FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Create);
                    StreamWriter sr = new StreamWriter(fs);
                    sr.Flush();
                    sr.Close();
                    fs.Close();
                }
                else
                {
                    FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Create);
                    StreamWriter sr = new StreamWriter(fs);
                    sr.Flush();
                    sr.Close();
                    fs.Close(); 
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }
        #endregion



But if you need to write the logs to Application log or event log you can do it as follow which shows in  MSDN.

But this will cost you a lot on behalf of time, because its still hard to figure our where the exact error location is. So we need to find a way that we can debug the application line by line as i explain below as the second approach.

In this approach, first we need to convert our application into console application.For that we can simply right click on the project and go to the "properties window". At that window, under the "Application tab" we can select the Output type as "Console application".

Then you need to add a new method that initiate your program, in to the service. For that you need to select the [yourserviceName].cs file and go to the code view by double click it.

Generally we can see  two methods and one constructor in auto generated template of service.cs file. OnStart() method is the place that commits the service operations where OnStop() stops them. When we started the windows service, it triggers the OnStart() method and keeps it up and ruining until we call the OnStop() method. So that, in here, our idea is to call OnStart() but terminate it without hanging.

To do that we can add a new method like follow


Code:
        internal void TestStartupAndStop(string[] args)
        {
            this.OnStart(args);
            Console.ReadLine();
            this.OnStop();
        }

As you see here this method starts the operations and give you the desired output. After you click any key it triers the OnStop() methord to stop activities

Next thing you need to do is change the main method of the application. Normally main method of a windows service looks like follow



Code:
        static void Main(string []args)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new CPEService()
            };
            ServiceBase.Run(ServicesToRun);
        }

but here we need to change this method as follow.

Code:
static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                MyNewService service1 = new MyNewService(args);
                service1.TestStartupAndStop(args);
            }
            else
            {
                // Put the body of your old Main method here.
            }

Now you can run the service as a normal console application. But make sure that to replace main method before you deploy the application as service.

Tuesday, March 3, 2015

Few good clips for AI fans


Simulated Simulated Annealing


  

Genetic algorithm


Simple introduction to neural networks  


Perceptron Algorithm 

The neuron 


Saturday, January 3, 2015

Tips to debug two WTF errors in MVC (Asp.net MVC 4 - Oracle)

         Normally most of the existing programming languages has there own ways to prompt errors to the programmer. IDE makes them sharp by adding knacks that helps user to sort out the error. 

      But some times, there may be rare situations that was missed to address by the IDE or cant address by the IDE. Generally this can be seen when it comes to a new technology which don't have much developer crowd at the movement or when plunging a 3rd party technology to a existing technology. 

        Since i'm a newbie for MVC+Oracle, i had few general errors which are caused to stuck me for few hours , sometimes days.  I had database first approach and i didn't  use Razor but ASP.net. 

_________________________________________________________________________________

WTF ERROR ???

I use this term for the program errors that are not giving an enough details to debug it. 

Ex: "Invalid Operation"   

! No error code         
!! No line number
!!! No special module
!!!! No any hint 

____________________________________________________________________

  1. Cant see the changes on browser

 2.  "Operation invalid due to the current state of the object "
    • I got this error when try to save the changes by using "SaveChanges()" Method.
[Full Description]

    • There was no any hint where i need to correct to avoid the error. That made this error "WTF".
    • I found one common possibility that directs to this error.
    • but it was not the problem for me.Error continued and i decided to check the database for errors
    • when i tried to perform insertion on database it gave me an error with the message "Permission denied".
    • Finally i found that, i didn't  give the enough permission to  the oracle user to perform the operation on database