Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 12, 2022 07:37 pm GMT

How to build a basic face detector using Python

To build a basic face detector using python, you can use a pre-trained Haar cascade classifier. A Haar cascade classifier is a machine learning object detection algorithm used to identify objects in images or video. It is trained to detect specific features, such as faces, in an image.

Here is an example of how to use a pre-trained Haar cascade classifier to detect faces in an image using the OpenCV library in python:

import cv2# Load the Haar cascade classifierclassifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")# Load the input imageimage = cv2.imread("input.jpg")# Convert the image to grayscalegray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# Detect faces in the grayscale imagefaces = classifier.detectMultiScale(gray_image)# Loop through the detected facesfor (x, y, w, h) in faces:    # Draw a rectangle around the face    cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)# Save the output imagecv2.imwrite("output.jpg", image)

This code will load the input image, convert it to grayscale, detect faces in the grayscale image using the Haar cascade classifier, and draw rectangles around the detected faces in the original image. The resulting image will be saved with the detected faces highlighted.


Original Link: https://dev.to/elvirhajdari/how-to-build-a-basic-face-detector-using-python-2n6n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To