程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses face_ Face recognition case based on recognition library

編輯:Python

This project uses face_recognition Library to write a simple python Program , Face recognition is realized . This library is based on deep learning technology , I hope to use the most convenient and simple way to realize face recognition , As long as one image is used for training, the corresponding identity of the verifier can be found from a large number of face data , Accuracy up to 96%.

One 、face_recognition Introduce

face_recognition Face recognition is an open source library , It's in the deep learning framework dlib Integration on . adopt Python Language encapsulates it as a very simple method to realize face recognition API library , It shields the algorithm details of face recognition , It greatly reduces the development difficulty of face recognition function .
About face_recognition In the library API See the following table for description .

api explain load_image_file take img File loading to numpy Array face_locations Find the position of all faces and all facial features in the image batch_face_locations Batch face location function (GPU)face_encodings Image coding is transformed into feature vector compare_faces Eigenvector comparison )face_landmarks Face feature extraction function face_distance Calculate the difference of eigenvectors

Two 、 Experiment preparation

1. Libraries installed
(1) Install face recognition library dlib library , The general installation method is as follows :

pip install dlib

If there is an installation error , See the blog I wrote before
Dlib Library installation method
(2) Install the actual face recognition database face_recognition, Installation method: :

pip install face_recognition

(3) install opencv library , Installation method: :

pip install opencv

2. Implementation code
Program description : The program calls the camera to shoot a face image , With “img.jpg” Name and save to desktop . And then use it face_recognition Function load_image_file Command to load a picture file into numpy Array , utilize face_encodings The instruction converts the image encoding into a feature vector . The same method traverses the face database face Every picture in the folder , Then the image coding of the file is transformed into a feature vector .

# Face detection program , Test a training picture , Then get all the pictures from the folder for face comparison , Get a picture of the same person's face 
import cv2
import face_recognition
import os
# Take photos function 
def get_photo():
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW) # Turn on the camera 
ref,frame=capture.read() # Save a frame of data in the camera 
img = "C:/Users/cos(Lin)/Desktop/img.jpg" # Name your pictures , Save to the specified route 
cv2.imwrite(img,frame) # Save the picture 
#cv2.imshow(img,frame)
capture.release() # Turn off camera 
return img # Return image data 
#load_image_file Load face image 
img_modi = face_recognition.load_image_file(get_photo()) # Parameter to call the photographing function , Get an image of the verifier 
img_modi_rgb =cv2.cvtColor(img_modi,cv2.COLOR_BGR2RGB) # Image type conversion function 
# Given an image , Returns each face in the image 128 Dimensional face coding 
train_encode = face_recognition.face_encodings(img_modi)[0] # code 
# return face What's in the folder , To delete a face from a face database face In the folder , Get the identity of the verifier 
pathDir = os.listdir("C:/Users/cos(Lin)/Desktop/face/") # Each picture in the folder is returned 
flag = 0 # Sign a 
# Test another image 
for i in pathDir: # Traverse 6 A picture 
test = face_recognition.load_image_file("C:/Users/cos(Lin)/Desktop/face/" + i) # Open every picture in the folder 
test = cv2.cvtColor(test,cv2.COLOR_BGR2RGB) # Convert every picture in the folder 
test_encode = face_recognition.face_encodings(test)[0] # Encode the image 
#compare_faces Compare the face coding list with the candidate coding , See if they match 
s = face_recognition.compare_faces([train_encode],test_encode,tolerance=0.49) # Parameters tolerance It is the distance between two faces that counts as a match . The smaller the value, the stricter the comparison ,0.6 Is a typical optimal value 
#print(s,type(s)) # Print type 
if s == [True] : # If the match is successful, the result is a Boolean list True
flag = 1 # If successful, assign the flag bit to 1
break # Jump straight out of the loop 
else:
flag = 0 # If no match is successful, the flag bit is 0
if flag ==1:
print(" The first few pictures are :" , i) # Output comparison results 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved