利用cv2来实现“人脸识别”,配代码及训练好的成品

爱必应

前提概要(基础知识):

  1. Viola-Jones Algorithm : 基本的算法程序,参考论文
  2. Haar-like Features: 建立在[font=-apple-system, BlinkMacSystemFont, &quot]Viola-Jones Algorithm之上的如何让计算机实现“人脸识别”,参考论文
  3. Integral Image: 如何高效地计算像素点(pixel), 从而来辨别[font=-apple-system, BlinkMacSystemFont, &quot]Haar-like下的‘black&white’区域[/font]
  4. Training Classifiers
  5. Adaptive Boosting (Adaboost): 规定了先detect face 区域,进而detect eyes, nose, mouth, etc.. 参考论文
  6. Cascading: 最终的数据集成.

python代码

# Face Recognitionimport cv2face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # We load the cascade for the face.eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') # We load the cascade for the eyes.'''first detect face then detect eye, then using a loop function roi_gray is used to spare more computorial time but we also can replace it with gray'''def detect(gray, frame): # We create a function that takes as input the image in black and white (gray) and the original image (frame), and that will return the same image with the detector rectangles.     faces = face_cascade.detectMultiScale(gray, 1.3, 5) # We apply the detectMultiScale method from the face cascade to locate one or several faces in the image.    for (x, y, w, h) in faces: # For each detected face:        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # We paint a rectangle around the face. (255,0,0) : RGB code for specific color        roi_gray = gray[y:y+h, x:x+w] # We get the region of interest in the black and white image.        roi_color = frame[y:y+h, x:x+w] # We get the region of interest in the colored image.        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3) # We apply the detectMultiScale method to locate one or several eyes in the image.        for (ex, ey, ew, eh) in eyes: # For each detected eye:            cv2.rectangle(roi_color,(ex, ey),(ex+ew, ey+eh), (0, 255, 0), 2) # We paint a rectangle around the eyes, but inside the referential of the face.    return frame # We return the image with the detector rectangles.video_capture = cv2.VideoCapture(0) # We turn the webcam on.while True: # We repeat infinitely (until break):    _, frame = video_capture.read() # We get the last frame.    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # We do some colour transformations.    canvas = detect(gray, frame) # We get the output of our detect function.    cv2.imshow('Video', canvas) # We display the outputs.    if cv2.waitKey(1) & 0xFF == ord('q'): # If we type on the keyboard:        break # We stop the loop.video_capture.release() # We turn the webcam off.cv2.destroyAllWindows() # We destroy all the windows inside which the images were displayed.

注意,需要调用本地的摄像功能,请配备好摄像设备。若没有摄像头,就用笔记本电脑尝试
配图没有上本人~,大体就是这张图片那样。

利用cv2来实现“人脸识别”,配代码及训练好的成品-图1

文件下载地址暂时未公布,需要的朋友请在下方留言,看到后会第一时间更新下载地址。

声明:本站所有资源均由网友分享,如有侵权内容,请在文章下方留言,本站会立即处理。

原文链接:,转发请注明来源!

发表评论