face recognition demo

We know that the most popular thing today is artificial intelligence. Artificial intelligence refers to the integration of information theory, psychology, physiology, linguistics, logic and mathematics on the basis of computer science to create a computer system that can simulate human intelligent behavior. borderline disciplines. There are two directions in the category of artificial intelligence: computer vision, natural speech processing (NLP, also known as NPL at home and abroad).

  • Introduction: Here is a demo. At the same time, this project is completed based on computer vision, aiming at simple popular science artificial intelligence
  • Required third-party libraries
 import face_recognition
 import cv2
 import datetime
 import glob2 as gb

Introduction to related libraries

  • face_recogniton is the simplest face recognition library in the world. You can use it in the form of Python reference or command line to manage and recognize faces. This package uses the most advanced face recognition deep learning algorithm in dlib, making the recognition accuracy in the "Labled Faces in the world" test benchmark The download reached 99.38%, and it also provides a command line tool called face_recognition, so that you can use the command line to recognize the pictures in a folder.
  • cv2 is an extension library of Opencv (Open Source Computer Vision Library), which contains various functions and processes for image processing. Works on Linux, Windows and Mac OS operating systems.
  • datetime is Python's standard library for dealing with dates and times; you can get the current date and time, or you can get a specified date and time, etc.
  • glob2 is a file operation related module that comes with python. It can be used to find files that meet your own purpose. It is similar to file search under Windows and supports wildcard operations.
  • code section
video_capture = cv2.VideoCapture(0)   # Use cv2 to open the camera to get the current image
img_path = gb.glob(r'D:\pycharmproject\F_recognition\photo\\*.jpg')  # get path
known_face_names = []                                                  # Use an array to get image information under a folder
known_face_encodings = []

for i in img_path:                                                        # Traverse, compare the pictures in the same folder
    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):    # Draw face information
        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()
  • Effect
  • Identify success

  • Recognition failed
  • full code
# -*- coding: utf-8 -*-
# @Time    : 2019/1/4 19:59
# @Author : Beard Spin
# @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()

  • Summarize
  • This is a demo I wrote a long time ago, and I haven't had time to sort it out. I took advantage of the summer vacation to sort out some useful projects and demos and record them for easy recall and sharing. If you have any questions, please correct me.

Posted by menriquez on Wed, 11 May 2022 15:52:44 +0300