首先安装“OPENCV”,通过清华的链接,加快下载速度。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn opencv-python
import cv2
import os
import matplotlib.pyplot as plt
os.chdir('D:\PYTHON AD')
def detect(filename):
face_cascade=cv2.CascadeClassifier(r'C:\Users\ENTHONY_WING\anaconda3\Lib\site-packages\cv2\data\haarcascade_eye.xml')
img=cv2.imread(filename)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=face_cascade.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)
plt.imshow(img)
plt.axis('off')
plt.show()
detect('CAMUS.jpeg')
运行代码后
结果:
然后安装DLIB、2-Face-recognition ,出现错误
pip install C:\Users\ENTHONY_WING\Desktop\JPT\dlib-19.21.99-cp38-cp38-win_amd64.whl
解决 :直接下载CMAKE等等
接下识别两人:
import face_recognition
import cv2
import matplotlib.pyplot as plt
image = face_recognition.load_image_file("D:\PYTHON AD\R-C.jpg")
face_locations=face_recognition.face_locations(image)
face_num2=len(face_locations)
print(face_num2) # The number of faces
org = cv2.imread("D:\PYTHON AD\R-C.jpg")
for i in range(0,face_num2):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
img=cv2.rectangle(org, start, end, color, thickness)
plt.imshow(img)
plt.axis('off') #去掉坐标轴
plt.show()
运行结果:
?人脸对齐:
import cv2
import dlib
import matplotlib.pyplot as plt
path = "D:\PYTHON AD\CAMUS.jpeg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"C:\Users\ENTHONY_WING\anaconda3\Lib\site-packages\face_recognition_models\models\shape_predictor_68_face_landmarks.dat")
#predictor = dlib.shape_predictor(r"C:\Users\ENTHONY_WING\anaconda3\Lib\site-packages\face_recognition_models\models\shape_predictor_5_face_landmarks.dat")
dets = detector(gray, 1)
for face in dets:
shape = predictor(img, face) # 寻找人脸的68个标定点
# 遍历所有点,打印出其坐标,并圈出来
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
img=cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
plt.imshow(img)
plt.axis('off') #去掉坐标轴
plt.show()
结果:
?接着实现人脸匹配:
import cv2
import face_recognition
import matplotlib.pyplot as plt
known_image=cv2.imread("D:\PYTHON AD\CAMUS.jpeg")
known_image = face_recognition.load_image_file("D:\PYTHON AD\CAMUS.jpeg")
unknown_image=cv2.imread("D:\PYTHON AD\CAMUS2.jpg")
unknown_image = face_recognition.load_image_file("D:\PYTHON AD\CAMUS2.jpg")
known_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([known_encoding],
unknown_encoding,
tolerance=0.6)
if results[0] == True:
print("匹配成功,该未知图片与已有图片人脸可匹配!")
else:
print("匹配失败!")
print(known_encoding)
print(unknown_encoding)
plt.imshow(known_image)
plt.axis('off') #去掉坐标轴
plt.show()
plt.imshow(unknown_image)
plt.axis('off') #去掉坐标轴
plt.show()
?尝试不一样的:
import cv2
import face_recognition
import matplotlib.pyplot as plt
known_image=cv2.imread("D:\PYTHON AD\CAMUS.jpeg")
known_image = face_recognition.load_image_file("D:\PYTHON AD\CAMUS.jpeg")
unknown_image=cv2.imread("D:\PYTHON AD\OIP-C.jpg")
unknown_image = face_recognition.load_image_file("D:\PYTHON AD\OIP-C.jpg")
known_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([known_encoding],
unknown_encoding,
tolerance=0.6)
if results[0] == True:
print("匹配成功,该未知图片与已有图片人脸可匹配!")
else:
print("匹配失败!")
print(known_encoding)
print(unknown_encoding)
plt.imshow(known_image)
plt.axis('off') #去掉坐标轴
plt.show()
plt.imshow(unknown_image)
plt.axis('off') #去掉坐标轴
plt.show()
?匹配失败。
视频人脸识别
import cv2
import dlib
#ESC退出
cap = cv2.VideoCapture(0)
predictor_path = "D:\PYTHON AD\shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)
detector = dlib.get_frontal_face_detector()
while True:
_, frame = cap.read()
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(frame, 1)
#if len(dets) != 0:
for i in range(len(dets)):
# Get the landmarks/parts for the face in box d.
shape = predictor(frame, dets[0])
for p in shape.parts():
cv2.circle(frame, (p.x, p.y), 3, (0, 0, 0), -1)
cv2.imshow('video', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
?运行结果:
|