Face recognition in Python in AI
With the shocking performance of alphago last year, AI has once again become the darling of technology companies. AI involves many fields, and face recognition in image recognition is one of the interesting branches. Baidu's BFR, Face++'s open platform, Hanwang, iFlytek, etc. all provide face recognition APIs. For experienced programmers, they can write a small piece of code to see how many people are in a picture. There are no tall and tall people. on, just for fun, and only requires 7 lines of code.
import cv2 face_patterns = cv2.CascadeClassifier('/usr/local/opt/opencv3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml') sample_image = cv2.imread('/Users/abel/201612.jpg') faces = face_patterns.detectMultiScale(sample_image,scaleFactor=1.1,minNeighbors=5,minSize=(100, 100)) for (x, y, w, h) in faces: cv2.rectangle(sample_image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.imwrite('/Users/abel/201612_detected.png', sample_image);
Line 1 Introducing OpenCV
Open source is great, it allows us to broaden our horizons and does not have to reinvent the wheel. Instead of using PIL and combining it with a specific algorithm, OpenCV (http://opencv.org) is used directly. OpenCV is a cross-platform computer vision library released under the BSD license. It can run on Linux, Windows and Mac OS operating systems. It is lightweight and efficient. It is written in C/C++. It also provides Python, Ruby, MATLAB and other interfaces to implement Many general algorithms in image processing and computer vision.
Line 2 loads the classifier cv2.CascadeClassifier
CascadeClassifier is a cascade classifier used for face detection in Opencv. This class encapsulates the target detection mechanism, that is, the sliding window mechanism + Cascading classifiers. The data structure includes two main parts: Data and FeatureEvaluator. Data stores the classifier data loaded from the xml file obtained from training; while FeatureEvaluator is about the loading, storage and calculation of features. The training file used here is haarcascade frontalface default.xml provided by default in OpenCV. As for the specific principles of Haar and LBP, you can refer to the relevant documents of opencv. Simply, it can be understood as the feature data of the face.
Line 3 Load the target image imread
Face recognition systems are generally divided into: face image collection, face image preprocessing, face image feature extraction, and matching and recognition. For the sake of simplicity, read in the image,
Line 4 multi-scale detection detectMultiScale
Call the detectMultiScale function in CascadeClassifier for multi-scale detection, and the single-scale method will be called in multi-scale detection detectSingleScale. Parameter description:
scaleFactor is the scaling factor of the image
minNeighbors is the number of neighbors that should be retained for each cascade rectangle, which can be understood as how many faces are around a person
MinSize is the size of the detection window
These parameters can be adjusted for the picture, and the processing result returns a rectangular object list of faces.
Lines 5 and 6 draw a frame for each face
Loop through the list of rectangular objects of the face, obtain the coordinates, width and height of the face rectangle, and then draw it in the original picture To exit the rectangular frame, OpenCV's rectangle method is called, in which the color of the rectangular frame is adjustable.
Line 7 Save the test results
The mystery is not these 7 lines of code, but the related implementation in OpenCV. The Chinese website of OpenCV is also a good place to learn and experience.
Therefore, the 7 lines of code are just a gimmick, the real core is OpenCV. Then, there are some pitfalls when installing the OpenCV environment, so record them in particular.
Mac-based OpenCV environment
It is recommended to use Brew for installation. If brew is not installed, execute the following command first:
$/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
Then, specify the target warehouse $brew tap homebrew/science
Install OpenCV3 $brew install opencv3
The installation speed depends on the network. After installation, you need to bind the Python development environment. There are many ways:
1) Add environment variables and add the site-packages of opencv to the PYTHONPATH
2) Use ln soft connection to link cv2.so to the site-packages of the python environment
3 )Directly cp cv2.so to the site-packages directory of the python environment
More simply, execute the following command:
echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth mkdir -p /Users/hecom/.local/lib/python2.7/site-packages echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/hecom/.local/lib/python2.7/site-packages/homebrew.pth
The above is the detailed content of Face recognition in Python in AI. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Laravel is suitable for building web applications quickly, and Python is suitable for projects that require flexibility and versatility. 1) Laravel provides rich features such as ORM and routing, suitable for the PHP ecosystem. 2) Python is known for its concise syntax and a powerful library ecosystem, and is suitable for fields such as web development and data science.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

I encountered a tricky problem when developing a new Laravel project: how to quickly build a fully functional and easy-to-manage content management system (CMS). I tried multiple solutions, but all gave up because of complex configuration and inconvenient maintenance. Until I discovered the LaravelCMS package mki-labs/espresso, which not only simple to install, but also provides powerful functions and intuitive management interface, which completely solved my problem.
