Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Realtime face detection aon web cam in Python using OpenCV

Date- Jan 22,2023

7290

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.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

ChatGPT-An Introduction to OpenAI's Powerful Language Model
Apr 19, 2023

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
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 | 1760
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
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers