Wednesday, 30 May 2012

Of focus and well digging

Swami Gulagulaananda said:
"If you lack focus, you will end up like Trishanku"

This is a short story I read a long time ago in Tinkle. The story starts off with a man who wants to dig a well. He starts digging at one spot. As hours roll by, the man who has dug up several metres of earth decides to catch a break. As he rests under the shade of a nearby tree, a zamindar who was walking nearby comes up to him and asks him what he's doing. The man tells him that water has always been a problem and he wanted to resolve the issue by digging a well there. The zamindar nods his head after listening to the man's words and then gives his piece of advice "Dear sir, your idea is very noble. But you have picked the wrong place. You see, this place is not suited for wells. What you need to do is go further towards that  side. Since it's closer to the river, you can get water there a lot faster. You are unnecessarily wasting energy here" and walks away

The man, on seeing value in the zamindar's words decides to take his advice. After finishing his lunch, he decides to start digging at the new location. As his limbs work tirelessly in the sweltering heat, along comes a priest from the village temple. "You shouldn't be digging a well here kind sir. This place is not vaastu compliant. What you should be doing is digging on a flat surface yonder by the banyan tree. Heed my advice and the gods shall be with you."

The young man with a renewed vigour begins digging in a new spot, for his resolve to dig a well is firm. As a few hours rolled by, the village headman comes sauntering with his cronies. And asks him to dig at a different place because it is closer to the fields. The poor man is exasperated. As he is digging in a new place, a learned pandit who was returning home comes up to him and asks him "Why have you started digging a well at such a late hour." Wiping his sweat off his forehead, the young man narrates his tale. At this, the learned pandit smiles and says "Instead of digging at four places, if you had continued digging in one place, no matter what the situation, by now you would have struck water"

In life, people around you are constantly giving you pearls of wisdom. One shouldn't blindly heed words and constantly change opinions. This is especially true while choosing streams during education, or career paths. The effort that gets wasted in vacillating could have been used up very well if it was focused instead. Otherwise, you will end up like Trishanku (or Jack ol' lantern) - neither here, nor there... With effort wasted instead of spent. One should always remember that a person who perseveres with focus will always win in the end. Which is why a sharp knife cuts better than a blunt one

You might also like to read:
Lessons in humility

Saturday, 26 May 2012

Kinect, Python and first few lines of code

Baba Gyani Triviani remembered:
"Example is always better than precept"

Many times, we are interested to do something but we don't find  the right resources. Some times, we find resources badly presented or presented at a very high level and when we beginners try to skim through the material, we are reminded of the feelings we had while skimming through Engineering text books, fondly remembered by EC students as "Over head transmission".

I intend to keep this post short at this moment, for my knowledge in the subject is very very limited. However, as I pick on more stuff, I will perhaps update this post with more stuff or provide links to newer posts.

In this post, I would like to show two simple programs to capture a normal photograph as well as to capture an IR photograph and show it on screen using Kinect, Python and OpenCV. If you don't know how to get up and running with Kinect, refer to my previous post here.

The program to capture normal photo

If you run the program above, you should be able to get a normal photo. If instead of a normal, you wish to get the IR image, replace the line img = freenect.sync_get_video() with img = freenect.sync_get_depth() and you are done. As you can see, this program is small and very self explanatory.

Programming with Kinect - Up and running

Swami Gulagulaananda said:
"If you a lot of potential, convert it to Kinectic"

After recently messing around with OpenCV and Python, I thought the next thing to mess around should be Kinect.

Today I played around a tad bit, but was happy that the stuff I was trying out worked fine. So if you are interested to play around with Kinect and don't know how to proceed, read on...

First of all, Kinect as most of you would already be knowing is a Microsoft product. So, programming it with MS Visual Studio (C# and VB) is probably way easier... I haven't tried that out yet since I don't have a good machine with Windows at the moment. So this post mainly is how to install it on Linux and see an output to show that "My kinect is up and running". The language is Python.

Do you have a Kinect (with power supply)?
You first need to have a Kinect. If you don't own an XBox with Kinect, you should buy one :-) It's awesome for playing games. But instead if you want to just program with Kinect, you can purchase a Kinect. I think it costs somewhere around Rs. 9500 (up to 11K depending on the store) and Flipkart sells it for Rs. 10990. When you purchase a Kinect separately (instead of with an XBox), it comes with an external power cable of its own.

However, if you already own an XBox with Kinect, you are fortunate enough to have a Kinect, but you don't have the power cable. You'll have to separately purchase that. Ebay sells it for around Rs. 1500.

Next step - Install stuff on your machine
Next, as the title says, we need to have some stuff installed. The problem is that there are a lot of incomplete resources all over the internet and you can face a bit of trouble if you follow them.

The project that is being used most often is called OpenKinect and you will find that there are quite a few resources, tutorials available there to proceed. But the problem is that the tutorial there didn't work very smoothly for me - at least for Python which was what I was trying.

Note that you need some additional libraries as well. So after doing quite a lot of work, I was able to find this other website (which might actually have the same libraries as OpenKinect since the developers involved are the same) called Pynect. This one worked very smoothly for me. All the additional required libraries are also there in the statement and will be installed if you don't already have it on your system. So, in my opinion, try out with Pynect first. In case you experience difficulties, try the OpenKinect site.

To summarise this paragraph, open your terminal and enter the two statements below:
sudo apt-get install build-essential git-core libusb-1.0-0-dev \
    python-pip python-dev python-numpy python-matplotlib

sudo pip install \
    -e git+git:// \

Installation done! Now to connect the Kinect
Ok, if everything went well, let's try it out... So plug in your Kinect to the external power cable, connect the USB to your machine and power it up. Your Kinect should have a blinking green LED (it was blinking for me) to show it's on. Once done, open your terminal and....

Free the Kinect!
Pause here for a moment. Before you write code, you should remember that Linux recognises your Kinect as a web cam or camera. So it gets held up. So even if you write code, your code won't be able to recognise that the Kinect is plugged in. Therefore, we first need to free the kinect. So in your terminal, issue the command:

sudo modprobe -r gspca_kinect

Test the Kinect
Run Andy Miller's demo program to test if everything runs fine. If everything is ok, you should see a small window popup that shows a live black and white (IR?) and coloured (normal webcam) version of whatever is in front of the Kinect. That's it :-) Smile and wave at the Kinect.

Did you know? There is something called Fakenect which will apparently allow some kind of simulation if you don't have a Kinect. Read more about it.

What next?
Why don't you try to capture images using your Kinect? Check it out here.

Saturday, 12 May 2012

The Sita Conundrum

Swami Gulagulaananda wondered:
"We all become who WE choose to become. And that choice cannot be forced by anyone upon us."

With the internet becoming more easily available, significant changes have come along. Now, the world is divided into two zones - The real world with people, and the virtual world with tweeple. The difference between these two worlds is that, in the virtual world, a lot of people hide behind curtains of anonymity. And since it's difficult for others to trace them, they become bolder, they speak out vehemently - Something that they probably wouldn't have done if asked to talk amidst other people - because they fear repercussions, the fear accountability or they fear the obvious things that happen if you raise fingers at politicians or hoodlums. However, under the cloak of anonymity they are free to have opinions.

Those of you who have been following news regularly would be familiar with a certain judge's controversial remark. I am not very sure about the entire case, but let's assume that the following is the way it happened. What I am more interested in, is to listen to what others have as opinions. I did ask some people for their opinions, but what's a better place than asking people of the virtual world? Even if you don't comment or post your opinions, it would be great if you could put in a certain amount of thought if you feel that it is (or is going to be) relevant to you.

The case (Hypothetical?)
A man has been married for x number of years (I have kept x as variable because your opinions might differ based on x... Read on) and both him and his wife are employed in different companies. For simplicity, let's take that both of them are 'techies', so as to bring about an image of a financially well to do upper middle class family, completely self sufficient. One day, the man gets to know from his boss that he's being transferred to the US (or some other major Indian city - another variable) for the next 5 years (again, let's keep this variable). He tells his wife, and his wife says that she doesn't want to leave her job. Naturally the man doesn't want to be away from her for so long, so he insists that she comes along with him, for he doesn't want his family broken up. They have a nasty spat which continues to worsen. They go to court as a divorce petition comes into the scene. The judge after studying says "Why can't you be like Sita, who followed her husband Ram even into the forest?" and this sparks a huge debate - with feminists crying foul, saying that the comment is regressive.

What would you do?
For those who know not, when prince Rama was exiled for 14 years, he alone was supposed to go into the forest. His wife, Sita who was a princess and being brought up amidst great luxury and royalty chose to follow her husband for her opinion was that her place is with her husband. Rama hadn't asked her, but she chose to follow him anyway, chose to live through the hardships of the jungle.

But the question is - how relevant is Ramayana in today's world? Rama was an ideal man, which was why he was called Adarsha Purusha. And he lived in a society where decadence was not as prevalent as it is today. Perhaps his wife's choice is suitable to that era. Perhaps Rama and Sita are too ideal for today's corrupt society.

Some people are of the opinion that conventionally, the male is always the protector and provider of the family while the female takes care of the child and the home. (Notice the word conventionally, this is not male chauvinist talk) It's not that women are incapable of doing things, it's that roles were defined. As time moved on and a wave of feminism swept across, modern women showed that they can do anything that a man can do. However, in India, the situation is very .... well, due to lack of a better word coming to my mind, I will pick curious. We have socially backward women, and we have too advanced women. On one hand, we have women who are being sold, burnt due to dowry, and on the other hand we have socialites posing with champagne. The rules that are formed to protect the backward women - such as dowry law, becomes an extremely dangerous weapon in the hands of the modern woman who has very often misused it. But one finds it hard to draw lines. The same holds true with a lot of rules such as reservation - which never seems to benefit the economically AND socially backward. This ends up being very unfair to others.

But enough of digressing there. Let me come back to the techie couple. So like I spoke man, woman and roles, it brings us back to the "power of attorney", the decision making power. Previously, when the man was the only bread winner, and being older, was making all the decisions. On the negative, he becomes like a dictator. On the positive though, the number of fights and clashes are minimum. Since he's older probably his decisions are more mature than the women? (Notice I said probably :P) And since only one decision making entity is there, there are no clashes. However, in the modern situation which everyone is familiar with now, there are 2 decision making entities. Both the husband and wife are earning huge salaries, each one independently can sustain not just themselves but also the children.

Therefore, there is no leverage with either to use on the other. The entire situation boils down to mutual respect, understanding and compromise. If you don't respect each other and don't compromise, and if you end up being stubborn, then the relationship just goes to the cleaners.

One very interesting argument I heard from one guy was his decision to marry a villager (or uneducated girl) because he felt that this way, he is creating the situation I described earlier - one decision making entity, no clashes. While it makes it seem very curious for a modern educated software engineer marrying an uneducated. I asked a couple of girls who said that they would follow the husband because they value relationships a lot more than jobs. It's not that they are going to be sitting at home, rather they will find a new job there because for them, the family is more important. At the same time, one conceded it depends on how hard the person has worked to get to that position.

In the end, the objective is to be happiest with the ideal combination of career and life. How you choose to do it is based on the mindsets of your spouse and you. But what's more important is that we try to imbibe the qualities of Rama and Sita into ourselves. The society is decadent, but at the same time, the society is made up of us. If we are good, the society is good. In the end, Rama and Sita are very much relevant - it's just that the tentacles of corruption has spread into each of us so deep, that we fear that we will be taken advantage of for being good. I believe more people feel that the Mahabharata is more relevant instead, since the decadence is far more prevalent in those societies, where brothers went to war over property!

We all become who WE choose to become. And that choice cannot be forced by anyone upon us. Think about it... How would YOU tackle the same situation? Vary the parameters and think how you would resolve it...

Tuesday, 8 May 2012

Lessons in humility

Swami Gulagulaananda said:
"It's good to be proud, it's bad to be vain"

There are times when arrogance creeps into our minds and hearts, when we begin to think that we are the greatest and none come close to us. And this could be due to your qualities - be it wealth, beauty, talent, popularity or intellect. But what we fail to understand is that there's always someone better right around the corner. If you think you are rich, there's someone richer... And even if you are the richest, how long will you be like that? For, like Ozymandias and his fate, this too shall pass. Both good and bad are passing phases in the eternally volatile time. Let's read a couple of short stories and pick some nice pearls of wisdom along the way.

Bhima humbled
Bhima was the strongest among the Pandavas and indeed one of the most powerful warriors. He could easily overpower anyone without breaking a sweat. And as is the theme of this post, he had grown quite arrogant. One day, while the Pandavas (who were in exile) were walking through the jungle, they came across a sleeping monkey whose tail was in the way of the Pandavas. Bhima shouted - "You, monkey! Get your tail out of our way so we can pass" But there was no response. "You insolent monkey! How dare you! Do you know whom I am? Get your tail out of my path so that I can pass through" roared Bhima. The old monkey said - "Dear sir, I am an old monkey. I don't have the energy to move my tail. Why don't you move it aside yourself?" Bhima looked flustered and went ahead in a huff. He tried to lift the tail with one hand, but he couldn't move it by even an inch. The perplexed warrior tried with both hands but in vain. Naturally he was surprised. He then attempted to move it with all his might, grunting and panting but he found that while all his energy was merely getting drained, the tail couldn't even be budged.

The mighty Vanara was none other than Hanuman. He then stood up and revealed himself to the Pandavas. Hanuman incidentally is Bhima's half brother (Both are sons of Vayu). Bhima was humbled in this way - and Hanuman agreed to be on the side of the Pandavas (not as a warrior, but in spirit) and thus the flag on Arjuna's chariot is Hanuman.

Ravana humbled
Ravana was a very powerful emperor who had defeated even Indra. (Incidentally so had his son, hence his nickname was Indrajit) Ravana however was a great devotee of Lord Shiva. Once he went to the base of Mount Kailasa to pray to Shiva, but felt that travelling all the way from Lanka to Kailasa was too tiresome to do regularly. Thus he thought - why not take the Kailasa back to Lanka? He thus proceeded to pick up the mountain. The entire mountain began to tremble, the animals and birds were frightened. Even Parvati, Shiva's consort began to be scared and asked the Lord to make it stop. Shiva merely pressed his big toe to the ground and a loud scream was heard, as the mountain came crushing on his hands. Ravana then begged Shiva for mercy and was forgiven.

In another story, Ravana attempts to insult Vali, the mighty Vanara by holding his tail. Vali goes flying around without even realising that the hapless Ravana was clutching his tail, shouting, begging Vali to stop.

Indra humbled
In this story from the Brahmavaivarta Purana, Indra defeats Vṛtrá and releases the waters. Elevated to the rank of King of the gods, Indra orders the heavenly craftsman, Vishvakarma, to build him a grand palace. Full of pride, Indra continues to demand more and more improvements for the palace. At last, exhausted, Vishvakarma asks Brahma the Creator for help. Brahma in turn appeals to Vishnu, the Supreme Being.

Vishnu visits Indra's palace in the form of a brahmin boy; Indra welcomes him in. Vishnu praises Indra's palace, casually adding that no former Indra had succeeded in building such a palace. At first, Indra is amused by the brahmin boy's claim to know of former Indras. But the amusement turns to horror as the boy tells about Indra's ancestors, about the great cycles of creation and destruction, and even about the infinite number of worlds scattered through the void, each with its own Indra. The boy claims to have seen them all. During the boy's speech, a procession of ants had entered the hall. The boy saw the ants and laughed. Finally humbled, Indra asks the boy why he laughed. The boy reveals that the ants are all former Indras.

Another visitor enters the hall. He is Shiva, in the form of a hermit. On his chest lies a circular cluster of hairs, intact at the circumference but with a gap in the middle. Shiva reveals that each of these chest hairs corresponds to the life of one Indra. Each time a hair falls, one Indra dies and another replaces him.

No longer interested in wealth and honor, Indra rewards Vishvakarma and releases him from any further work on the palace. Indra himself decides to leave his life of luxury to become a hermit and seek wisdom. Horrified, Indra's wife Shuchi asks the priest Brihaspati to change her husband's mind. He teaches Indra to see the virtues of both the spiritual life and the worldly life. Thus, at the end of the story, Indra learns how to pursue wisdom while still fulfilling his kingly duties.

What you see in these short stories is that - Bhima, Ravana and Indra are no ordinary people - they are incredibly strong and powerful, but yet there's someone stronger and greater than them. We too are no different. Thus we should remember this whenever we feel success begins to go to our heads. There's always someone better. It's good to be proud, it's bad to be vain.

Do you want to share some mythological stories? Visit

You might also like to read:
Of focus and well digging

Monday, 7 May 2012

Gyroscope like a spirit level

Baba Gyani Triviani said:
"Speaking of our spirits being balanced, I thought, can we use a spirit level to check orientation with electronics?"

I was just thinking about that spider camera in a cricket stadium and thought "How cool would it be if we had a bunch of autonomous flying bots in the air?" The crowds would be thrilled and it's just plain cool!

And as those thoughts were in my mind, I was wondering how you would be able to know the orientation of your bot? What if it was tilted and flying about haphazardly? And a comical image came to my mind - some guy keeping a spirit level on the bot to check if it is level. But then I thought - why not? What would happen if we have two spirit levels perpendicular? And by extension, a circular spirit level?

So I thought - what would happen if you have a circular spirit level, where the spirit is in reality some conducting liquid. And we have some kind of small electrodes into the unit at regular intervals along the border/circumference. And an electrode from the bottom which sends in some current. If the unit is perfectly flat, all electrodes will conduct. As the unit tilts, the bubble goes towards an electrode. The moment there is air at that point, the conduction at that electrode stops, and we know which direction it is tilted to. Simple right?

Maybe if we have multiple electrodes per node stacked one above the other having the unit relatively thicker, we can even have caliberation per tilt - as in, slightly tilted, more tilted, *code red alert* tilted! What say? Comments as to feasibility? Or does this already exist? Or how about having it spherical? That would be most awesome!

Saturday, 5 May 2012

Open CV Python Face Detection making LED Glow via Arduino

Swami Gulagulaananda said:
"There was light... I blocked my face and *poof* Off it went!"

I had some spare time on me, and I thought "You know what? Let me do something" - I decided to have a look at Open CV and Python. I am just a beginner at this point of time and have done nothing very clever. But if you share my enthusiasm, you will find this little thing pretty nice.

In my previous post, you will see how to write a very small program to get Face Detection done. Notice that you don't need to know anything about Image Processing (It is an awesome subject if you are interested in) but you can still get some stuff up and running.

After I did that and it worked, I thought, let me take this one step higher. What is the use of Face Detection if you don't do anything after that? So in this post, I show you how to make an LED glow or turn off, depending on whether a face is detected or not. It is not at all as complicated as the title seems.

All you need to have is an Arduino kit (and an external LED, resistor and breadboard which are optional because you can use the built in LED if you don't have them)

Just connect the LED and resistor to pin 13 and ground if you have them - otherwise it's just fine.

int incoming = 0;

void setup() {
   pinMode(13, OUTPUT);

void loop() {
   incoming =;
   if (incoming != -1) {
      digitalWrite(13, LOW);
   } else {
      digitalWrite(13, HIGH);


In the Python program, just import serial. For this, you should get the pyserial libraries.

Just do a serial write to the serial port when the face is detected as soon as you are drawing the rectangle. That's it! You are done.

Have a look at the working here:

Get the code here -

Face Detection using Open CV and Python

Swami Nikhilaananda said:
"Face Detection and other awesome stuff have been greatly simplified with OpenCV and Python"

I am no expert in this field, but I was just excited to have a piece of code working. In this little program, we attempt to do a face detection using Python and OpenCV.

First get Open CV and Python up and running following simple tutorials found here.

The following is a piece of code - Replace the path of the Haar Classfier file (xml) with the one in your system. Save the file, and run the script.

As you could have probably guessed, absolutely nothing intelligent is there in the program above. Everything is taken care of by OpenCV. Which is what makes it really good for people who don't know much of Image Processing, nor want to know the mathematical nitty gritties.

Perhaps in the next post, you should be able to see how to Glow an LED when a face is detected - by interfacing with Arduino

You can see LED Glowing with Face Detection using Arduino here

Taking Face Detection to the next level? Instead of merely putting a rectangle, you can perhaps overlay stuff... Here's my first rough attempt to mimic Google's Reindeer stunt on Hangout

Friday, 4 May 2012

Comment & Uncomment multiple lines in Vim

Baba Gyani Triviani said:
"When someone said 'No Comments' I just had to intervene. No comments? Have multi-line comments my friend!"

Commenting and uncommenting multiple lines in Vim can be a little tricky. Here is a small snippet of code to show how you can do this. I have done this for just a few of languages. But you can extend this to any number as per your choice.

Save the following into a file and call it vcomments.vim

Once this is done, open your ~/.vimrc  file if it exists, or create a new one. There, put the following code.

:source ~/vcomments.vim
:map <C-a> :call Comment() <Enter>
:map <C-b> :call Uncomment() <Enter>

Where you can replace a and b of and by whatever is convenient to you. This just calls the functions when the Ctrl + <key> combination is hit.

To use it, just select the lines to comment by using Shift + v and moving cursor up or down to select lines. Then, hit the Ctrl + <key> combination to comment. Similarly, you can uncomment them

Note - This is my first attempt at Vimscript - So it may not really be all that graceful in case of issues :-) But do let me know when you hit problems so that  I can put in a bunch of fixes.

You may also like to read
Common Vim Mappings to make your life easier (For Beginners)
Vim Editor - Some cool features (Relatively Advanced)

Thursday, 3 May 2012

Common VIM Mappings to make life easier

Swami Nikhilaananda said:
"When you transition from a known to an unknown, the similarities allow the transition to become smoother"

If you have not checked out the previous post about Vi(m) editor, now is the time to read it.

Many times, we are used to common keyboard short-cuts like Ctrl + C to copy, Ctrl + v to paste, and so on. However, when we move to Vim, it is tedious to remember all the new shortcuts like y to yank (copy) and p to put (paste).

To avoid that, I created a bunch of remappings to enable me to perform common tasks quickly using the same set of keyboard shortcuts that we are so used to.

You can do it too. Just open your ~/.vimrc file. If it is not there, create it. Then, paste the contents below as is into the file. Save it and restart vim. These shortcuts will start working (in escape mode)

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

set mouse=a
se nu

:map <C-a> GVgg
:map <C-n> :enew
:map <C-o> :e . <Enter>
:map <C-s> :w <Enter>
:map <C-c> y
:map <C-v> p
:map <C-x> d
:map <C-z> u
:map <C-y> R
:map <C-t> :tabnew <Enter>
:map <C-i> >>
:map <C-w> :close <Enter>
:map <C-f> /
:map <F3> n
:map <C-h> :%s/
:map <C-q> :q <Enter>
:map <S-t> vat
:map <S-T> vit
:map <S-{> vi{
:map <S-(> vi(
:map <S-[> vi[

Here, C stands for Control, says Ctrl + a, GVgg is the Vim equivalent of select all. Thus, we are just mapping them. S stands for Shift. will select all text within the current tag inclusive of tag. exclusive of tag. S-{, S-(, S-[ will select text within the corresponding brace.

Hope you find these shortcuts useful. Let me know if you want any thing that's missing here.

You may also like to read
Comment and Uncomment Multiple Lines in Vim
Vim Editor - Some cool features (Relatively Advanced)