Pages

February 21, 2017

Face detection example

In this post I will show you a very simple example of how to detect faces inside an image using Python 3 and OpenCV library.

First of all you have to install the prerequisite software as described in this post.

Download the PyFaceRec repository in a directory of your choice. In the examples we assume you have downloaded it into C:\PyFaceRec.

The example described here is located in the SimpleFaceDetection folder and automatically identifies faces in a picture of the famous pop group Abba.
Open a command Prompt, move into the SimpleFaceDetection folder an run the sample script as follows.

cd C:\PyFaceRec\SimpleFaceDetection
python simple_face_detection.py

You should get an output like this.



The source is quite self-explanatory.

# Import the OpenCV library
import cv2

# Load the sample image
image = cv2.imread('data/abba.jpg')

# Load the OpenCV classifier to detect faces
faceCascade = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    image,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(50, 50)
)

# The faces variable now contains an array of Nx4 elements
# where N is the number faces detected

print("Found", len(faces), "faces")

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

# Display the image
cv2.imshow("Faces found", image)

# Wait for any key before exiting
cv2.waitKey(0)


2 comments:

  1. Easily, the is actually the very best topic with this registry related issue. I participate in your conclusions and will eagerly look forward to the next updates. Just saying thanks won't you need to be sufficient, for the fantasti c lucidity in your writing. I'll instantly grab your rss feed to remain informed of any updates.open afp file

    ReplyDelete
  2. If you're trying hard to burn fat then you absolutely need to jump on this totally brand new custom keto diet.

    To design this keto diet, licensed nutritionists, fitness couches, and cooks have united to develop keto meal plans that are powerful, painless, economically-efficient, and delightful.

    Since their first launch in 2019, 100's of people have already transformed their figure and well-being with the benefits a good keto diet can offer.

    Speaking of benefits: in this link, you'll discover 8 scientifically-confirmed ones offered by the keto diet.

    ReplyDelete

Note: Only a member of this blog may post a comment.