python第三方插件face_recognition

1、安装dlib

sudo pip install dlib
如果不能安装参见前文
sudo pip install face_recognition
python
>>>import face_recognition
不报错识别成功

2、识别照片中的人脸

#coding:utf-8

# 检测人脸
import face_recognition
import cv2

# 读取图片并识别人脸
img = face_recognition.load_image_file("silicon_valley.jpg")
face_locations = face_recognition.face_locations(img)
print face_locations

# 调用opencv函数显示图片
img = cv2.imread("silicon_valley.jpg")
cv2.namedWindow("原图")
cv2.imshow("原图", img)

# 遍历每个人脸,并标注
faceNum = len(face_locations)
for i in range(0, faceNum):
    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 = (55,255,155)
    thickness = 3
    cv2.rectangle(img, start, end, color, thickness)

# 显示识别结果
cv2.namedWindow("识别")
cv2.imshow("识别", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

3、视频实时识别

import face_recognition
import cv2

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
m1_image = face_recognition.load_image_file("M1_B1.jpg")
m1_face_encoding = face_recognition.face_encodings(m1_image)[0]

m2_image = face_recognition.load_image_file("M2.jpg")
m2_face_encoding = face_recognition.face_encodings(m2_image)[0]

k1_image = face_recognition.load_image_file("k1.jpg")
k1_face_encoding = face_recognition.face_encodings(k1_image)[0]

B1_image = face_recognition.load_image_file("B1.jpg")
B1_face_encoding = face_recognition.face_encodings(B1_image)[0]

B2_image = face_recognition.load_image_file("B2.jpg")
B2_face_encoding = face_recognition.face_encodings(B2_image)[0]


# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(small_frame)
        face_encodings = face_recognition.face_encodings(small_frame, face_locations)

        face_names = []
        faces_to_compare = [
            m1_face_encoding,
            m2_face_encoding,
            k1_face_encoding,
            B1_face_encoding,
            B2_face_encoding]    

        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            match = face_recognition.compare_faces(faces_to_compare, face_encoding, tolerance=0.5)
            name = "Unknown"

            print(match)
            if match[0]:
                name = "M1"
            if match[1]:
                name = "M2"
            if match[2]:
                name = "K1"
            if match[3]:
                name = "B1"
            if match[4]:
                name = "B2"

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()

cv2.destroyAllWindows()

参考备注:
https://face-recognition.readthedocs.io/en/latest/readme.html
http://blog.youkuaiyun.com/hongbin_xu/article/details/76284134
https://zhuanlan.zhihu.com/p/27275307
http://blog.youkuaiyun.com/yang_xian521/article/category/910716/5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值