Face Detection in OpenCV

Face Detection in OpenCV

Example of Face Detection using OpenCV in Python. We will discuss how we can apply Face Detection using OpenCV. We go straightforward with a practical reproducible example. The logic it the following: We get the image from the URL (or from the hard disk). We convert it to an numpy array and then to a grayscale.

We will discuss how we can apply Face Detection using OpenCV. We go straightforward with a practical reproducible example.

The logic it the following: We get the image from the URL (or from the hard disk). We convert it to an numpy array and then to a grayscale. Then by applying the proper CascadeClassifier we get the bounding boxes of the faces. Finally, using PIllow (or even OpenCV) we can draw the boxes on the initial image.

import cv2 as cv
import numpy as np
import PIL
from PIL import Image
import requests
from io import BytesIO
from PIL import ImageDraw
## I have commented out the cat and eye cascade. Notice that the xml files are in the opencv folder that you have downloaded and installed
## so it is good a idea to write the whole path
face_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
#cat_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalcatface.xml')
#eye_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_eye.xml')
URL = "https://images.unsplash.com/photo-1525267219888-bb077b8792cc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
response = requests.get(URL)
img = Image.open(BytesIO(response.content))
img_initial = img.copy()
## convert it to np array
img = np.asarray(img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
## And lets just print those faces out to the screen
#print(faces)
drawing=ImageDraw.Draw(img_initial)
## For each item in faces, lets surround it with a red box
for x,y,w,h in faces:
    ## That might be new syntax for you! Recall that faces is a list of rectangles in (x,y,w,h)
    ## format, that is, a list of lists. Instead of having to do an iteration and then manually
    ## pull out each item, we can use tuple unpacking to pull out individual items in the sublist
    ## directly to variables. A really nice python feature
    #
    ## Now we just need to draw our box
    drawing.rectangle((x,y,x+w,y+h), outline="red")
display(img_initial)

python facedetection ai opencv opencv-python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

Face Recognition with Python and OpenCV

What is face recognition? Or what is recognition? When you look at an apple fruit, your mind immediately tells you that this is an apple fruit. This process, your mind telling you that this is an apple fruit is recognition in simple words. So what is face recognition then? I am sure you have guessed it right. When you look at your friend walking down the street or a picture of him, you recognize that he is your friend Paulo. Interestingly when you look at your friend or a picture of him you look at his face first before looking at anything else. Ever wondered why you do that? This is so that you can recognize him by looking at his face. Well, this is you doing face recognition. Face Recognition With OpenCV and Python

OpenCV Python | Python OpenCV Tutorial | Python for Beginners

OpenCV Python in Python will explain all the basics of OpenCV. It also explains how to create a face recognition system and a motion detector.

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.