栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

NO.2-人脸识别练习

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NO.2-人脸识别练习

首先安装“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:UsersENTHONY_WINGanaconda3Libsite-packagescv2datahaarcascade_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:UsersENTHONY_WINGDesktopJPTdlib-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 ADR-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 ADR-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 ADCAMUS.jpeg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"C:UsersENTHONY_WINGanaconda3Libsite-packagesface_recognition_modelsmodelsshape_predictor_68_face_landmarks.dat")
#predictor = dlib.shape_predictor(r"C:UsersENTHONY_WINGanaconda3Libsite-packagesface_recognition_modelsmodelsshape_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 ADCAMUS.jpeg")
known_image = face_recognition.load_image_file("D:PYTHON ADCAMUS.jpeg")

unknown_image=cv2.imread("D:PYTHON ADCAMUS2.jpg")
unknown_image = face_recognition.load_image_file("D:PYTHON ADCAMUS2.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 ADCAMUS.jpeg")
known_image = face_recognition.load_image_file("D:PYTHON ADCAMUS.jpeg")

unknown_image=cv2.imread("D:PYTHON ADOIP-C.jpg")
unknown_image = face_recognition.load_image_file("D:PYTHON ADOIP-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 ADshape_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()

 运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号