Coder Social home page Coder Social logo

face-detection-using-mediapipe's Introduction

Face Detection using Mediapipe

Google open-source MediaPipe was first introduced in June, 2019. It aims to make our life easy by providing some integrated computer vision and machine learning features. Media Pipe is a framework for building multimodal(e.g video,audio or any time series data),cross-platform (i.eAndroid,IOS,web,edge devices) applied ML pipelines. Mediapipe also facilitates the deployment of machine learning technology into demos and applications on a wide variety of different hardware platforms.

Steps Involved in Face Detection by using Mediapipe Framework

  • Importing all the essential libraries
  • Setting up webcam
  • Initialize the face_detection class from the Mediapipe library
  • Performing face detection
  • Drawing the results with confidence level

Step 1: Import the libraries

import cv2
import mediapipe as mp

Step 2: Setting up a webcam (Windows)

cap = cv2.VideoCapture()
cap.open(0, cv2.CAP_DSHOW)

It is quicker to get web cam live in Windows environment by adding cv2.CAP_DSHOW attribute.

Step 2: Setting up a webcam (Windows/Linux/Mac)

cap = cv2.VideoCapture(0)

Step 3: Initialize the face_detection class from the Mediapipe library

mp_facedetector = mp.solutions.face_detection
#mp_draw = mp.solutions.drawing_utils

If you want to draw facial key points, uncomment the line.

#mp_draw = mp.solutions.drawing_utils

Step 4: Performing face detection

with mp_facedetector.FaceDetection(min_detection_confidence=0.7) as face_detection:
    while cap.isOpened():
        success, image = cap.read()
        results = face_detection.process(image)
        if results.detections:
            for id, detection in enumerate(results.detections):
                #mp_draw.draw_detection(image, detection)
                #print(id, detection)
                bBox = detection.location_data.relative_bounding_box
                h, w, c = image.shape
                boundBox = int(bBox.xmin * w), int(bBox.ymin * h), int(bBox.width * w), int(bBox.height * h)
                cv2.rectangle(image, boundBox, (0, 255, 0), 3)
                cv2.putText(image, f'{int(detection.score[0]*100)}%', (boundBox[0], boundBox[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)

        cv2.imshow('Face Detection', image)
        if cv2.waitKey(1) == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()

If you want to draw facial key points, uncomment the line.

#mp_draw.draw_detection(image, detection)

References

face-detection-using-mediapipe's People

Contributors

kwoksir avatar

Watchers

 avatar

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.