---
title: Face and Object Detection using Python and OpenCV - Inventive HQ
description: "\"Learn face and object detection with Python and OpenCV using pre-trained models and a simple webcam script.\""
image: https://hs.inventivehq.com/hubfs/Imported_Blog_Media/1514621066_featured.jpg
---

[Skip to content](https://hs.inventivehq.com/blog/face-and-object-detection-using-python-and-opencv#main-content)

![](https://hs.inventivehq.com/hs-fs/hubfs/InventiveHQ%20Logo%20-%20RectangleWide.png?width=2200&height=1238&name=InventiveHQ%20Logo%20-%20RectangleWide.png)Homepage

Get started

Get started

![](https://hs.inventivehq.com/hs-fs/hubfs/Imported_Blog_Media/1514621066_featured.jpg?width=2048&height=1365&name=1514621066_featured.jpg)

# Face and Object Detection using Python and OpenCV

![Sean Conroy](https://7528315.fs1.hubspotusercontent-na1.net/hub/7528315/hubfs/raw_assets/public/mV0_d-cms-elevate-theme_hubspot/elevate/images/avatar-placeholder.jpg?width=48&height=48&name=avatar-placeholder.jpg)

 Sean Conroy

January 13, 2025

For those who want to do object or face detection, the standard way to do it is with OpenCV. OpenCV is a programming library for Object recognition and is very flexible. You can train it to recognize any number of different objects. For the purpose of this tutorial, I will be using Python3. However, you can use other programming languages with OpenCV as well.

# Prerequisites

**1. Pip**  
Pip is not necessarily a prerequisite. But it makes it a lot easier. Below are instructions on how to install Pip on OSX, Linux, and Windows.

Linux and OSX:  
Open a terminal window and run: curl [https://bootstrap.pypa.io/get-pip.py](https://bootstrap.pypa.io/get-pip.py) -o get-pip.py

Windows:  
Installing on Windows is a very similar process to OSX and Linux, you just don’t have access to curl. So start by downloading the get-pip.py script here: [https://bootstrap.pypa.io/get-pip.py](https://bootstrap.pypa.io/get-pip.py)Then run:python3 [https://bootstrap.pypa.io/get-pip.py](https://bootstrap.pypa.io/get-pip.py)

**2. Opencv**  
If you are using python 3:  
Pip3 install opencv-python

If you are using Python 2:  
Pip install opencv-python

The above command(s) will install the latest version of OpenCV. As of today, that is version 3.4.

**3. Pre-trained Model**  
OpenCV comes with pre-trained models for various scenarios. Using a pre-trained model is much easier than training one yourself. For the purposes of this demonstration, we will be using [this](http://https//raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml) pre-trained model.

There are additional pre-trained models [here.](https://github.com/opencv/opencv/tree/master/data/haarcascades)

# Running the script

After your prerequisites are installed, you can write your code. At the bottom of this article is an example script that will capture from your webcam and draw a rectangle around any detected faces. Make sure that you save the XML file you downloaded in the previous step to the same folder as the python script below.

The XML file I mentioned will detect all full faces. However, there are a variety of other pre-trained models you can use. There is one that will detect partial faces, another that will detect smiles, etc…. You can use the same script below with the different models to detect different things. In a future article, we will go over training your own model.

Below is the script I mentioned. You can copy and paste it into a file called face.py. Then you can run it my typing python3 face.py

```
######## Example ##############
import cv2
import sys

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

cam = cv2.VideoCapture(0)
anterior = 0

while True:
    # Process each frame
    ret, frame = cam.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(40, 40))

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)

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

    cv2.imshow('Video', frame)

    #Press Q to quit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # Display video
    cv2.imshow('Video', frame)

cam.release()
cv2.destroyAllWindows()
```

When you are done running the above script, you can exit by pressing the ‘Q’ button on your keyboard.

Elevate Your IT Efficiency with Expert Solutions

## Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At Inventive HQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.

[Discover Our Services](https://inventivehq.com/services/)

## Share this post

<https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fhs.inventivehq.com%2Fblog%2Fface-and-object-detection-using-python-and-opencv><https://twitter.com/intent/tweet?url=https%3A%2F%2Fhs.inventivehq.com%2Fblog%2Fface-and-object-detection-using-python-and-opencv><https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fhs.inventivehq.com%2Fblog%2Fface-and-object-detection-using-python-and-opencv><https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fhs.inventivehq.com%2Fblog%2Fface-and-object-detection-using-python-and-opencv>[mailto:https%3A%2F%2Fhs.inventivehq.com%2Fblog%2Fface-and-object-detection-using-python-and-opencv](mailto:https%3A%2F%2Fhs.inventivehq.com%2Fblog%2Fface-and-object-detection-using-python-and-opencv)

## Keep reading

### [![](https://hs.inventivehq.com/hs-fs/hubfs/Imported_Blog_Media/Beautiful_ai-The-Definitive-Guide-to-Content-Delivery-Networks-CDNs-Slide-1.jpg?width=1920&height=1080&name=Beautiful_ai-The-Definitive-Guide-to-Content-Delivery-Networks-CDNs-Slide-1.jpg) What is a CDN? The Ultimate Guide to Content Delivery Networks - Inventive HQ](https://hs.inventivehq.com/blog/what-is-a-cdn-the-ultimate-guide-to-content-delivery-networks)

### [![](https://hs.inventivehq.com/hs-fs/hubfs/Imported_Blog_Media/pexels-binyaminmellish-108941.jpg?width=5616&height=3744&name=pexels-binyaminmellish-108941.jpg) Transitioning from WSUS to Azure Update Management and Azure Arc - Inventive HQ](https://hs.inventivehq.com/blog/transitioning-from-wsus-to-azure-update-management-and-azure-arc)

[![](https://hs.inventivehq.com/hs-fs/hubfs/InventiveHQ%20Logo%20-%20RectangleWide.png?width=2200&height=1238&name=InventiveHQ%20Logo%20-%20RectangleWide.png)](https://hs.inventivehq.com/)

<https://www.linkedin.com><https://www.facebook.com><https://www.twitter.com><https://www.instagram.com><https://www.tiktok.com>

---

Privacy Policy · Legal · © 2026. All rights reserved.

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Sean Conroy",
    "url" : "https://hs.inventivehq.com/blog/author/sean-conroy"
  },
  "dateModified" : "2025-01-31T00:10:36.239Z",
  "datePublished" : "2025-01-13T17:11:48.000Z",
  "headline" : "Face and Object Detection using Python and OpenCV - Inventive HQ",
  "image" : [ "https://hs.inventivehq.com/hubfs/Imported_Blog_Media/1514621066_featured.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://hs.inventivehq.com/blog/face-and-object-detection-using-python-and-opencv",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://hs.inventivehq.com/hubfs/InventiveHQ%20Logo%20-%20RectangleWide.png"
    }
  }
}
```