Home Backend Development Python Tutorial Detailed explanation of Python+OpenCV face recognition technology

Detailed explanation of Python+OpenCV face recognition technology

Oct 18, 2016 pm 02:54 PM
human face identify

We always see face recognition in science fiction movies, and now we can also implement it through programming. Haha~~
OpenCV is Intel® open source computer vision library. It consists of a series of C functions and a small number of C++ classes that implement many common algorithms in image processing and computer vision.
OpenCV has a cross-platform mid- and high-level API including more than 300 C functions. It has no dependencies on other external libraries - although some can be used. It also provides interfaces in Python, Ruby, MATLAB and other languages, and implements many common algorithms in image processing and computer vision.

So overall, OpenCV’s face detection function is very good.

The rendering is as follows:

Detailed explanation of Python+OpenCV face recognition technology Next we will use python + OpenCV to implement face recognition.

Development and running environment:
Centos5.5
OpenCV
python2.7
PIL

The code below:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# face_detect.py

# Face Detection using OpenCV. Based on sample code from:
# http://www.pythontab.com

# Usage: python face_detect.py

import sys, os
#Introducing the opencv library Corresponding components
from opencv.cv import *
from opencv.highgui import *
#Introduce the PIL library
from PIL import Image, ImageDraw

from math import sqrt

def detectObjects(image):
#First convert the image to gray degree mode to find the face position
grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
cvCvtColor(image, grayscale, CV_BGR2GRAY)

storage = cvCreateMemStorage(0)
cvClearMemStorage(s storage)
cvEqualizeHist(grayscale, grayscale)

cascade = cvLoadHaarClassifierCascade(
'/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
cvSize(1,1))
faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 2,
        CV_HAAR_DO_CANNY_PRUNING, cvSize(20,20))
 
   result = []
    for f in faces:
       result.append((f.x, f.y, f.x+f.width, f.y+f.height) )

return result

def grayscale(r, g, b):
return int(r * .3 + g * .59 + b * .11)

def process(infile, outfile):

image = cvLoadImage(infile);
if image:
faces = detectObjects(image)

im = Image.open(infile)

if faces:
draw = ImageDraw.Draw(im)
for f in faces:
       draw.rectangle (f, outline=(255, 0, 255)) im.save(outfile, "JPEG", quality=100) else:
print "Error: cannot detect faces on %s" % infile

if __name__ == "__main__":
process('input.jpg', 'output.jpg')

The code ends here. The above example cannot be understood. It doesn't matter, because we use a lot of functions and methods in the library. If If you don’t understand, we can check it online or use the manual. As long as you use these to understand this code, it will be ok. The important thing is to master the facial recognition implementation ideas




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
How to add people's faces to the video during editing How to add people's faces to the video during editing Feb 23, 2024 pm 12:50 PM

There is a very good P-picture function in editing, so how to add faces to videos? Users need to import the material into the timeline, then select keying, click smart keying to use a circular mask, and finally Just cut out the face. This introduction to how to add faces to videos can tell you exactly how to use this function, so take a look. "Tutorial on the use of video clipping" How to add faces to video via video clipping Answer: You need to drag the material to the timeline, and then use smart keying to cut out the face. Specific methods: 1. First, you need to import the material and then add it to the timeline. 2. Click Keying on the upper right. 3. Select Smart Keying, find the mask, and select the circular mask. 4. Finally, just dig out the head of the portrait.

How to implement speech recognition and speech synthesis in C++? How to implement speech recognition and speech synthesis in C++? Aug 26, 2023 pm 02:49 PM

How to implement speech recognition and speech synthesis in C++? Speech recognition and speech synthesis are one of the popular research directions in the field of artificial intelligence today, and they play an important role in many application scenarios. This article will introduce how to use C++ to implement speech recognition and speech synthesis functions based on Baidu AI open platform, and provide relevant code examples. 1. Speech recognition Speech recognition is a technology that converts human speech into text. It is widely used in voice assistants, smart homes, autonomous driving and other fields. The following is the implementation of speech recognition using C++

Face detection and recognition technology implemented using Java Face detection and recognition technology implemented using Java Jun 18, 2023 am 09:08 AM

With the continuous development of artificial intelligence technology, face detection and recognition technology has become more and more widely used in daily life. Face detection and recognition technologies are widely used in various occasions, such as face access control systems, face payment systems, face search engines, etc. As a widely used programming language, Java can also implement face detection and recognition technology. This article will introduce how to use Java to implement face detection and recognition technology. 1. Face detection technology Face detection technology refers to the technology that detects faces in images or videos. in J

An article talking about the traffic sign recognition system in autonomous driving An article talking about the traffic sign recognition system in autonomous driving Apr 12, 2023 pm 12:34 PM

What is a traffic sign recognition system? The traffic sign recognition system of the car safety system, whose English translation is: Traffic Sign Recognition, or TSR for short, uses a front-facing camera combined with a mode to recognize common traffic signs (speed limit, parking, U-turn, etc.). This feature alerts the driver to traffic signs ahead so they can obey them. The TSR function improves safety by reducing the likelihood that drivers will disobey traffic laws such as stop signs and avoid illegal left turns or other unintentional traffic violations. These systems require flexible software platforms to enhance detection algorithms and adjust to traffic signs in different areas. Traffic sign recognition principle Traffic sign recognition is also called TS

Win10 is recognizing the solution for being unable to connect to the internet Win10 is recognizing the solution for being unable to connect to the internet Jul 11, 2023 pm 06:21 PM

When you use a win10 computer, have you ever encountered a situation where you are unable to connect to the internet? If you don't know how to solve this problem, let's take a look at this article. 1. Use the Win+I key combination to bring up the settings window, click to select Network and Internet. 2. Click Ethernet on the left side of the Network and INTERNET window, and then click Change Adapter Options in the right window. 3. In the Network Connection window, right-click Ethernet (desktop, please treat mobile devices as appropriate), and click Disable in the menu. 4. After it is displayed as disabled, right-click the Ethernet mouse and execute the enable command. Once Ethernet is enabled, the problem should be resolved. Here is an introduction to Win10 network failure prompt identification without

Has facial recognition for pets been implemented? Has facial recognition for pets been implemented? Apr 13, 2023 pm 07:52 PM

Translator | Reviewed by Cui Hao | Sun Shujuan Technology that can accurately identify animals will help owners reunite with lost pets, farmers monitor their livestock, and researchers study wild animals. Based on the above application scenarios, microchips are the most popular pet identification method. However, implanting a chip requires invasive surgery, specialized equipment to read the chip, and the risk of a thief extracting the microchip. Another method is DNA analysis, which although accurate, is also very expensive and time-consuming. Today I want to introduce to you how to identify animals through facial recognition. 1. Pet facial recognition using computer vision software Pet facial recognition by computer vision solutions can be used as an alternative to the above solutions. Despite its shortcomings,

How to identify when a table is locked in an Oracle database? How to identify when a table is locked in an Oracle database? Mar 09, 2024 pm 01:09 PM

In Oracle database, table being locked is a common situation. How to identify and solve this problem is one of the challenges that database administrators often face. This article will introduce how to identify table locks in Oracle databases, and give specific code examples to help database administrators quickly locate and solve table lock problems. 1. How to identify when a table is locked? 1. View the V$LOCK view The V$LOCK view is an important view used to view lock information in the Oracle database. we can

How to use PHP and Alibaba Cloud OCR to realize QR code recognition? How to use PHP and Alibaba Cloud OCR to realize QR code recognition? Jul 17, 2023 am 10:46 AM

How to use PHP and Alibaba Cloud OCR to realize QR code recognition? With the widespread application of QR codes, more and more projects require the recognition of QR codes. In traditional methods, we usually use cameras or third-party libraries for QR code recognition. However, these methods are sometimes not very flexible and cannot meet specific needs. In this article, we will introduce how to use PHP and Alibaba Cloud OCR service to realize QR code recognition, and further explore the value of OCR technology in practical applications. First, you need to register a

See all articles