GithubHelp home page GithubHelp logo

bhadreshwara / happinessdetector Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 14.39 MB

Smile Detection using OpenCV

Python 34.20% CMake 3.33% Jupyter Notebook 62.47%
opencv opencv-python python3 hacktoberfest

happinessdetector's Introduction

HappinessDetector

What you need to build happiness detector:

Anaconda Navigator: https://docs.anaconda.com/anaconda/navigator/

OpenCV: https://opencv.org/

Haar Cascades (Data)

Iโ€™m using Spyder on Anaconda, but you could also use Jupyter NoteBook. Once you have everything, get on the IDE (code editor).

Haar Cascades - "Viola-Jones algorithm uses haar-like features to detect facial properties. The cascade is a series of filters that will apply one after the other to detect a face through its features."

Importing OpenCV and Loading cascades

#Importing the Libraries

import cv2 as cv

#Loading the Cascade

face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv.CascadeClassifier('haarcascade_smile.xml')

Defining the detection function on face, eyes, and smile

def detect(gray, frame): # We create a function that takes as input the image in black and white (gray) and the original image (frame), and that will return the same image with the detector rectangles. 

faces = face_cascade.detectMultiScale(gray, 1.3, 5) # We apply the detectMultiScale method from the face cascade to locate one or several faces in the image.

for (x, y, w, h) in faces: # For each detected face:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # We paint a rectangle around the face.
    roi_gray = gray[y:y+h, x:x+w] # We get the region of interest in the black and white image.
    roi_color = frame[y:y+h, x:x+w] # We get the region of interest in the colored image.
    eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3) # We apply the detectMultiScale method to locate one or several eyes in the image.
    for (ex, ey, ew, eh) in eyes: # For each detected eye:
        cv2.rectangle(roi_color,(ex, ey),(ex+ew, ey+eh), (0, 255, 0), 2) # We paint a rectangle around the eyes, but inside the referential of the face.
return frame # We return the image with the detector rectangles.

Displaying the output on your webcam

while True: # We repeat infinitely (until break):
  _, frame = video_capture.read() # We get the last frame.
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # We do some colour transformations.
  canvas = detect(gray, frame) # We get the output of our detect function.
  cv2.imshow('Video', canvas) # We display the outputs.
  if cv2.waitKey(1) & 0xFF == ord('q'): # If we type on the keyboard:
      break # We stop the loop.

 video_capture.release() # We turn the webcam off.
 cv2.destroyAllWindows() # We destroy all the windows inside which the images were displayed.

Output

  Run .py file and Have Fun!
  we can also use jupyter lab/ jupyter notebook to run iPython notebook.

happinessdetector's People

Contributors

bhadreshwara avatar nims972 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

nims972

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.