Python: 快速实现人脸识别

首先安装opencv:(在jupyter notebook里)

1 ! pip install opencv-python

接下来,从电脑的摄像头读取视频,识别视频内的人脸。

 1 import cv2
 2 
 3 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 4 
 5 cap = cv2.VideoCapture(0) # 0 means read video form camera of your computer
 6 
 7 while True:
 8     _, img = cap.read()
 9 
10 # img = cv2.imread('people.png')
11 
12     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13 
14     faces = face_cascade.detectMultiScale(gray, 1.1, 4)
15 
16     for (x,y,w,h) in faces:
17         cv2.rectangle(img, (x,y), (x+w, x+h), (255, 0, 0), 2)
18     
19     cv2.imshow('img', img)
20     
21     k = cv2.waitKey(30) & 0xff
22     if k == 27:
23         break
24 
25 cap.erlease()

从YouTube上学到的:

https://www.youtube.com/watch?v=7IFhsbfby9s

posted @ 2020-10-20 08:31  cfdchen  阅读(862)  评论(0编辑  收藏  举报