My Brain Cells

Easiest (and best) learning materials for anyone with a curiosity for machine learning and artificial intelligence, Deep learning, Programming, and other fun life hacks.

Auto-capture Selfie by Detecting Smile

Project Prerequisites

  • Python
  • open-cv

We use Haar Cascade, ML object detection algorithm which is used to identify the objects in an image or video.

In this, we train from the data sets and lot of positive and negative images which then used to detect objects

algorithm steps:

  • haar feature selection
  • creating integral images
  • AdaBoost training
  • cascading classifiers

Download the dataset of face and smile from here

Create a main.py file

First, install opencv-python

pip install opencv-python

code:

import cv2

video = cv2.VideoCapture(0)
faceCascade = cv2.CascadeClassifier("dataset/haarcascade_frontalface_default.xml")
smileCascade = cv2.CascadeClassifier("dataset/haarcascade_smile.xml")

while True:
    success,img = video.read()
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(grayImg,1.1,4)
    cnt=1
    keyPressed = cv2.waitKey(1)

    for x,y,w,h in faces:
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,0),3)
        smiles = smileCascade.detectMultiScale(grayImg,1.8,15)
        for x,y,w,h in smiles:
            img = cv2.rectangle(img,(x,y),(x+w,y+h),(100,100,100),5)
            print("Image "+str(cnt)+"Saved")
            path=r'C:\Users\BOSS\Desktop\SmileCapture\images\img'+str(cnt)+'.jpg'
            cv2.imwrite(path,img)
            cnt +=1
            if(cnt>=2):    
                break
                
    cv2.imshow('live video',img)
    if(keyPressed & 0xFF==ord('q')):
        break

video.release()                                  
cv2.destroyAllWindows()
output

Anthony

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top