Face Detection on webcame using Python OpenCV | Code2Night
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Realtime face detection aon web cam in Python using OpenCV

Date- Jan 22,2023

4255

Free Download Pay & Download
Python OpenCV

Face detection is a crucial application of computer vision, and it has numerous use cases, such as security, surveillance, and facial recognition. With the increasing demand for intelligent systems, such as facial recognition, this tutorial will provide you with a practical understanding of how to apply this technology to your projects.

Welcome to Code2Night, where we explore the fascinating world of coding and technology! With the rapid advancements in AI, Python has become one of the most popular programming languages for developing cutting-edge applications. In this blog post, we will delve into the world of computer vision and explore how we can implement real-time face recognition on a webcam using the OpenCV library in Python.

So, buckle up and get ready to explore the exciting world of computer vision with Python!

Open CV is a powerful library that helps you in implementing many features like Automatic face detection, changing images to grayscale, image rotation, and many more features related to images and videos. So here we will be using this for face detection on webcams in real-time.

So we will add a new Python file and name it facedetection.py

Now copy the following piece of code for face detection

import cv2
import sys
from time import sleep

cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)


video_capture = cv2.VideoCapture(0)
anterior = 0

while True:
    if not video_capture.isOpened():
        print('Unable to load camera.')
        sleep(5)
        pass

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30)
    )

    # Draw a rectangle around the faces
    for i,(x, y, w, h) in enumerate(faces):
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        face = frame[y:y+h, x:x+w]
        cv2.imwrite(f'face{i}.jpg', face)
        

    if anterior != len(faces):
        anterior = len(faces)
        


    # Display the resulting frame
    cv2.imshow('Video', frame)


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # Display the resulting frame
    cv2.imshow('Video', frame)
    
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()


Now save the file and open the command prompt and go to the location of the file. Here first we have to install two packages. Run the following command and press enter

pip install opencv-python==4.6.0.66

It will look like this in the image belowface detection

Now install this second package 

pip install opencv-contrib-python

You can check the command in the below image

We this you will also need to add one XML file in the same location as your program. You can see the file in the image below You can download the files from the download button above. Now run the program and it will open the webcam on your system

You will be able to see the rectangle drawn on your webcam detecting your face. it will also save the detected face in the same location with name face0.jpg

So this is how we can implement real-time face detection on webcam in Python using OpenCV.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1190
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts