其他分享
首页 > 其他分享> > 人脸识别demo

人脸识别demo

作者:互联网

我们知道当今最火的莫过于人工智能了,人工智能指在计算机科学的基础上,综合信息论、心理学、生理学、语言学、逻辑学和数学等知识,制造能模拟人类智能行为的计算机系统的边缘学科。在人工智能的范畴内有两个方向:计算机视觉、自然语音处理(NLP,国内外也有人称NPL)。

 import face_recognition
 import cv2
 import datetime
 import glob2 as gb

相关库介绍

video_capture = cv2.VideoCapture(0)   # 使用cv2打开摄像头获取当前图像
img_path = gb.glob(r'D:\pycharmproject\F_recognition\photo\\*.jpg')  # 获取路径
known_face_names = []                                                  # 使用数组获取文件夹下的图片信息
known_face_encodings = []

for i in img_path:                                                        # 遍历,通过同文件夹下的图片比对
    picture_name = i.replace('D:\pycharmproject\F_recognition\photo\\*.jpg', '')
    picture_newname = picture_name.replace('.jpg', '')
    someone_img = face_recognition.load_image_file(i)
    someone_face_encoding = face_recognition.face_encodings(someone_img)[0]
    known_face_names.append(picture_newname)
    known_face_encodings.append(someone_face_encoding)
    someone_img = []
    someone_face_encoding = []

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    ret, frame = video_capture.read()
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame = small_frame[:, :, ::-1]

    if process_this_frame:
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        face_names = []
        for i in face_encodings:
            match = face_recognition.compare_faces(known_face_encodings, i, tolerance=0.39)
            if True in match:
                match_index = match.index(True)
                name = "match"
                # To print name and time
                cute_clock = datetime.datetime.now()
                print(known_face_names[match_index] + ':' + str(cute_clock))
            else:
                name = "unknown"
            face_names.append(name)

    process_this_frame = not process_this_frame

    for (top, right, bottom, left), name in zip(face_locations, face_names):    # 将人脸面部信息画出来
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
# @Time    : 2019/1/4 19:59
# @Author  : 胡子旋
# @FileName: Recognition.py
# @Software: PyCharm
# @Email   :1017190168@qq.com
import face_recognition
import cv2
import datetime
import glob2 as gb
video_capture = cv2.VideoCapture(0)
img_path = gb.glob(r'D:\pycharmproject\F_recognition\photo\\*.jpg')
known_face_names = []
known_face_encodings = []

for i in img_path:
    picture_name = i.replace('D:\pycharmproject\F_recognition\photo\\*.jpg', '')
    picture_newname = picture_name.replace('.jpg', '')
    someone_img = face_recognition.load_image_file(i)
    someone_face_encoding = face_recognition.face_encodings(someone_img)[0]
    known_face_names.append(picture_newname)
    known_face_encodings.append(someone_face_encoding)
    someone_img = []
    someone_face_encoding = []

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    ret, frame = video_capture.read()
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame = small_frame[:, :, ::-1]

    if process_this_frame:
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        face_names = []
        for i in face_encodings:
            match = face_recognition.compare_faces(known_face_encodings, i, tolerance=0.39)
            if True in match:
                match_index = match.index(True)
                name = "match"
                # To print name and time
                cute_clock = datetime.datetime.now()
                print(known_face_names[match_index] + ':' + str(cute_clock))
            else:
                name = "unknown"
            face_names.append(name)

    process_this_frame = not process_this_frame

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

标签:人脸识别,name,demo,frame,cv2,face,encodings,recognition
来源: https://blog.csdn.net/public669/article/details/97171124