OpenCV, the most popular computer vision library, originally developed in C and C++, now supports Python as well.


It uses machine learning algorithms to search for people’s faces in images. For something as complex as a face, there is no simple test that can make a conclusion about the presence of a face, and thousands of feature matches are required. The algorithm breaks the face-recognition task into thousands of smaller tasks, each of which is not difficult to handle. These tasks are also known as classifiers.


For something like a face, you might need no fewer than 6,000 classifiers, each of which needs to be a successful match (with error tolerance, of course) to detect a face. But there’s a problem: For face recognition, the algorithm starts at the top left corner and calculates blocks of data, constantly asking “Is this a face?” With more than 6,000 checks per block, adding up to millions of levels of computation, computers are likely to keep you waiting.


OpenCV uses Cascades to avoid that. What is Cascade? The best answer is already in the dictionary: a waterfall or a series of waterfalls.


Like a cascade, OpenCV Cascade breaks down the face detection problem into several steps. For each chunk of data, it does a rough, quick check. If it passes, a more careful test is carried out, and so on. The algorithm has 30 to 50 of these stages, or cascades.


Only through all stages, the algorithm will judge the detection of the face. The advantage of this is that most graphs generate negative feedback in the first few steps, so the algorithm doesn’t have to test all 6,000 features on it, saving a lot of time. This allows face detection in real time, as opposed to a “normal process” that can take hours.



Software environment:

python3+openCV



Code:

#encoding:utf-8 import cv2 filename = "/users/Downloads/20181102142518.png" def detect(filename): # haarCascade_frontalface_default. XML is stored at the location where the package is installed face_CASCADE = Cv2. CascadeClassifier ("/usr/local/lib/python3.6 / site - packages/cv2 / data/haarcascade_frontalface_default XML ") img = cv2.imread(filename) gray = cv2.cvtColor(img, Cv2.color_bgr2gray) # Transfer parameters are scaleFactor and minNeighbors, which respectively represent the compression rate of the image during each iteration of the face detection process and the minimum number of neighbors retained by each face rectangle DetectMultiScale (Gray, 1.3, 5) for (x, y, W, H) in Faces: img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.namedWindow("Human Face Result!" ) cv2.imshow("Human Face Result!" , img) cv2.imwrite("images/Face.jpg", img) cv2.waitKey(0) cv2.destroyAllWindows() detect(filename)Copy the code





After the recognition:




Wechat official account: Data Analysis Union


Automatic identification of http://weixin.qq.com/r/V3XUzBbEm0vkrVKv9yBF (qr code)


Data analysis Treasure book


“Spiderman” everyone data analysis data analysis 200000 words ebook 】 https://m.tb.cn/h.3i7mJyz click the link, then select the browser tak ð « Or to copy this description ¥8iohbTKiaBs¥8iohbTKiaBs¥later to 👉


【 July algorithm of machine learning deep learning python data analysis Zou Bo 】 https://m.tb.cn/h.3iWDgFB click on the link, then select the browser tak ð « S8iIbTKiQoX¥later to 👉 Chao ♂ ♀👈[share from super member]


The machine learning field Python full code includes training data collection of image data presentation case 】 https://m.tb.cn/h.3ik7Hmf click the link, then select the browser tak ð « [查 看 全 文] [查 看 全 文]