Face Detection using openCV in Python

faceDetection

Hello folks, How’s it going!

Today I am going to introduce my face detection algorithm. (not a big one though) Don’t think that this is really a huge task! I am not working from scratch (means I am not actually gathering a huge data set of all pictures (both negative and positive, i.e having faces and not ) and train my algorithm). I have used haar Cascading files.

What the heck are they? Here we go.

Taking samples of a lot of image files of both types (having faces and not having faces) and train the algorithm, make it learn when ever (most probably every time┬á­čśŤ ) it makes a mistake and store the whole data into xml files. In this case I am using haar Cascading xml files. As I already said they are huge, wanna know their size? 35k lines of code in each xml file! Yes, you read that right, 35K lines of random numbers. ­čśŤ

Coming to the juicy part, the code, here it is. I have commented briefly what each line is contributing.


#import numpy as np
import cv2
#import PIL

#import the xml files. here i am using frontal face and eye.
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

img= cv2.imread('5.jpg') #reading the image
gray_img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #converting into gratscale(algos work with grayscale images)

faces = face_cascade.detectMultiScale(gray_img, 1.3, 5) #detecting faces. lightning conditions may affect the output
print (faces) #just printing to console. this will print boundary points of detected face(s)

#searching for eyes only in faces. easy and efficient to search only in face rather than whole image
for (p,q,r,s) in faces:
 cv2.rectangle(img,(p,q),(p+r,q+s),(150,125,0),2)        #drawing a rectangle indicating face
 face_gray = gray_img[q:q+s, p:p+r] #cropping   face in gray image
 face_color = img[q:q+s, p:p+r] #cropping face in  color image
 eyes = eye_cascade.detectMultiScale(face_gray)  #searching for eyes in grayscale img
 for (ep,eq,er,es) in eyes:
  cv2.rectangle(face_color,(ep,eq),(ep+er,eq+es), (100,210,150),2) #for each eye drawing rectangle

cv2.imshow("output", img)
cv2.waitKey(0) #showing the img

#this only takes a image and shows the faces in the image. It dont modifies the image.
#if you want to save the resulted image use this...
#cv2.imwrite("output.jpg", img)

Wanna know what it did? I gave this image

Input image

and it returned THIS!…….

Output
Screenshot of the output

 

Currently, I am working on alphabet detection thing to won in a bet with my friend.

Bieeee  ^_^

Here somewhere in Milky Way

-Surya 

Advertisements

First ML Code on Gradient descent!

Hii there,

Hmm… my first program exceeding 20 lines.

Basically, it is a gradient descent problem (don’t know much about it ­čśŤ ). As I am taking Machine Learning course on Cousera I wanna solve some problems on ML. I found some AI problems (don’t know much about this too) on HakerRank site and started solving this one. This guy is an output of my 5 hours of work. ­čśÇ

import java.util.Scanner;


public class houseCosts {
	public static void main(String[] args){
		System.out.println("Enter");
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		//System.out.println(n+""+m);
		float[][] x = new float[n+2][m];
		float[] t = new float[n+1];                       
		float[] temp = new float[n+1];
		float alpha = (float) 0.3;                      //alpha
		//inputs
		for(int j = 0; j<m; j++){
			for(int i=1; i<n+2; i++){
				x[i][j] = in.nextFloat();
				//System.out.println("x "+i+" "+j+"= "+x[i][j]);
			}
		}
		
		int num = in.nextInt();
		//System.out.println(num);
		float[][] out = new float[n+1][num];
		for(int j = 0; j<num; j++){
			for(int i=1; i<n+1; i++){
				out[i][j] = in.nextFloat();
				//System.out.println("out "+i+" "+j+"= "+out[i][j]);
			}
		}
		for(int i=0; i<num; i++){
			out[0][i] = 1;
			//System.out.println("x "+0+" "+i+"= "+x[0][i]);
		}
		
		//Initializations 
		for(int i=0; i<m; i++){
			x[0][i] = 1;
			
			//System.out.println("x "+0+" "+i+"= "+x[0][i]);
		}
		for(int j=0; j<n+1; j++){
			t[j] = 0;                                  //theta value initializing
		}
		
		for(int p = 0; p<500; p++){                   //no. of times
			//body
			for(int k=0; k<n+1; k++){
				float dum = 0;
				for(int j=0; j<m; j++){
					float ans = 0;
					for(int i=0; i<n+1; i++){
						ans+= t[i] * x[i][j];
						//System.out.println(ans);
					}
					ans-= x[n+1][j];
					//System.out.println(ans);
					ans*= x[k][j];
					dum+=ans;
					//System.out.println("x "+k+" "+j+" ="+x[k][j]);
					//System.out.println(ans);
				}
				//System.out.println(dum);
				temp[k] = (float) (t[k]-(alpha * dum * (1.0/m)));
				//System.out.println(temp[k]);
			}
			for(int k=0; k<=n; k++){
				t[k]=temp[k];
				//System.out.print(t[k]+" ");
			}
			//System.out.println(" ");
		}
		
		for(int i = 0; i<num; i++){
			float foo = 0;
			for(int j=0; j<n+1; j++){
				foo+=out[j][i] * t[j];
			}
			System.out.println(foo);
		}
		
	}
}

Don’t judge me by this code coz, I don’t know much about algorithms and ML. And there are so many stdouts because I dunno how to debug in eclipse or any IDE for that mater. BTW, I got ten on ten for this problem.

Something Productive- CHECK!

Don’t forget to travel in time.

Cheerios

Surya

 

What I have found?!

Hello,

HaHaaa.. I received all of my summer toys from element14 for free. Yeah, you heard that right, absolutely for FREE. It’s part of roadtest by element14. This is a group dedicated to testing and reviewing new products. I submitted my idea of testing these products and they selected me. And I should agree to their conditions that I should provide them with a neat review within two months of receiving kits. Here are they:

My Toys
Got from element14. Hahaa

 

Briefly, an MSP430 launchpad from TI with Anaren air kit to control a robot or a robotic car with smartphones via bluetooth. Raspberry Pi with Enocean modules (self-powered sensors kit) for home automation. And my idea is switch-off computers or laptops when you leave your room.

After spending all these days lazily, it’s time to start working on these things and provide them with review helps me to spend remaining time lazily….!

Bye for now.

Peace

S

I am happy. I am sad.

Namaste,

Now, I am happy after all these boring holidays, did something productive(posting this) besides learning. You might think it’s insane, learning during holidays. But, thank god I am not studying. I am following some Java tutorials on YouTube and trying to understand my first technical book Head First Java. A hell of a book that. Basically I don’t like reading books, fact is that I completed reading How I Braved Anu Aunty and Co-Founded A Million Dollar Company┬áin two days in my second sem and that is my first book. The main reason I don’t like books is the way they appear, those paras, those lines with same font and size, which eventually makes ┬áme scare and to throw it away.

Head First JAVA

Coming to this book,┬áHead First Java, I didn’t throw it away because it’s a soft copy on my laptop. I didn’t delete it because I
love┬áthat book. A ‘Brain-Friendly Guide’,┬áthere’s this phrase on cover page and after reading the book (not whole book, though) I realised that the author┬áreally mean it. I completed reading 200 pages of 700 in two days(surprisingly, I understood) and realised that it’s really Brain-Friendly. All Happies. Hope my Java journey takes me further. Now, sad part is, there is something big to do in my mind but facing difficulties to practically apply it(Yeah, I know something┬ábig always comes with a lot of difficulties). I got selected for two roadtests┬á(reviewing new electronic products, mostly development boards, home automation systems) in which I am about to receive A raspberry-pi with home automation modules(fun part is these modules are self-powered) and A launch pad from TI, MSP430 with bluetooth modules. The real sad part living in India(no hard feelings), it takes a long time to ship these kits to India. All of those who got selected in roadtests have received their kits(not from India). There’s a nice thing to do with those self-powered modules(sensors). But before knowing what can we do with Continue reading