GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — Face Detection using OpenCV in Python - The Python Code Before we get started, have you tried our new Python Code Assistant? It's like having an expert coder at your fingertips. Check it out! Object detection is a computer technology related to computer vision and image processing that deals with detecting instances of semantic objects of a certain class (such as human faces, cars, fruits, etc.) in digital images and videos. In this tutorial, we will be building a simple Python script that deals with detecting human faces in an image, we will be using two methods in OpenCV library. First, we are going to use haar cascade classifiers, which is an easy way (and not that accurate as well) and the most convenient way for beginners. After that, we'll dive into using Single Shot Multibox Detectors (or SSDs in short), which is a method for detecting objects in images using a single deep neural network. Note: It is worth mentioning that you need to distinguish between object detection and object classification, object detection is about detecting an object and where it is located in an image, while object classification is recognizing which class the object belongs to. If you are interested in image classification, head to this tutorial. Face Detection using Haar CascadesHaar feature-based cascade classifiers is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images. The nice thing about Haar feature-based cascade classifiers is that you can make a classifier of any object you want, OpenCV already provided some classifier parameters to you, so you don't have to collect any data to train on it. To get started, install the requirements:
Alright, create a new Python file and follow along, let's first import OpenCV:
You gonna need a sample image to test with, make sure it has clear front faces in it, I will use this stock image that contains two nice lovely kids:
The function Before we detect faces in the image, we will first need to convert the image to grayscale, that is because the function we gonna use to detect faces expects a grayscale image:
The function Since this tutorial is about detecting human faces, go ahead and download the haar cascade for human face detection in this list. More precisely, "haarcascade_frontalface_default.xml". Let's put it in a folder called "cascades" and then load it:
Let's now detect all the faces in the image:
Finally, let's save the new image:
Here is my resulting image:
Pretty cool, right? Feel free to use other object classifiers, other images, and even more interesting, use your webcam! Here is the code for that:
Once you execute that (if you have a webcam of course), it will open up your webcam and start drawing blue rectangles around all front faces in the image. The code isn't that challenging, all I changed is, instead of reading the image from a file, I created a VideoCapture object that reads from it every time in a while loop, once you press the q button, the main loop will end. Related: Mastering YOLO: Build an Automatic Number Plate Recognition System with OpenCV in Python. Face Detection using SSDsAs you can see, the previous method isn't that challenging. Unfortunately, it is obsolete and it is rarely used today in the real world. However, neural networks always come into the rescue, and luckily for us, OpenCV provides us with the amazing To get started predicting faces using SSDs in OpenCV, you need to download the ResNet face detection model architecture along with its pre-trained weights, and then save them into weights folder in the current working directory:
Now to load the actual model, we need to use
We gonna use the same image that's used above:
Now to pass this image into the neural network, we need to prepare it. More specifically, we need to resize the image to the shape of
Let's use this
Now output object has all detected objects (faces in this case), let's iterate over this array and draw all faces in the image that has confidence of more than 50%:
After we extracted the confidence of the model of the detected object, we get the surrounding box and multiply it by the In this case, we didn't only draw the surrounding boxes, but we write some text indicating the confidence as a percentage, let's show and save the new image:
Here is the resulting image:
Awesome, this method is way better and accurate, but it may be lower in terms of FPS if you're predicting faces in real-time, as is it's not as fast as the haar cascade method. By the way, if you want to detect faces using this method in real-time using your camera, you can check the full code page. There are many real-world applications for face detection, for instance, we've used face detection to blur faces in images and videos in real-time using OpenCV as well! Alright, this is it for this tutorial, you can get all tutorial materials (including the testing image, the haar cascade parameters, SSDs model weights, and the full code) here. Mastering YOLO: Build an Automatic Number Plate Recognition SystemBuilding a real-time automatic number plate recognition system using YOLO and OpenCV library in Python Download EBookWant to Learn More?Here are the references for this tutorial:
Learn also: How to Perform YOLO Object Detection using OpenCV and PyTorch in Python. Happy Coding ♥ Ready for more? Dive deeper into coding with our AI-powered Code Explainer. Don't miss it!
Sharing is caring!
Read alsoHow to Detect Contours in Images using OpenCV in PythonLearning how to detect contours in images for image segmentation, shape analysis and object detection and recognition using OpenCV in Python.
How to Perform YOLO Object Detection using OpenCV in PythonUsing the state-of-the-art YOLOv8 object detection for real-time object detection, recognition and localization in Python using OpenCV, Ultralytics and PyTorch. Optical Character Recognition (OCR) in PythonLearn how to Use Tesseract OCR library and pytesseract wrapper for optical character recognition (OCR) to convert text in images into digital text in Python. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |