Introduction: This article is compiled by the editor of Programming Notes# for you. It mainly introduces the knowledge related to the installation and application (with code) of face recognition—-face_recognition. I hope it has certain reference value for you.
face_recognition
claims to be the world’s simplest face recognition library based on python
, is a well-known deep learning framework The integrated ,dlib
model on dlib
can have an accuracy rate of 99.38 in LFW (Labeled Faces in the Wild). In addition, face_recognition
provides a corresponding command line tool , you can use the command line to perform face recognition on the picture folder , very cool , follow the steps before the article opens Okay!
One, pip install dlib library ( suggest the second method)
pip install Cmake
pip install boost
Attention :Generally you need to download VS2019(It is recommended not to use an older version&# xff01;)community edition is fine. After installation and configuration, go to the next step.
Input pip install dlib
Second, download whl file installation
p>
I use python3.8, if you need this file, please leave a message or download it by yourself,Please be sure to download the corresponding whl file according to the version.
Open cmd and enter the whl file directory , as shown in the picture :
2. Install face_recognition
pip install face_recognition
Face recognition is not required Use dlib, but install face_recognition must first exist dlib library
Third, use pycharm for face recognition
Calculation as shown in , face_recognition already exists.
views.py Code :
import os
import face_recognition
from django.http import HttpResponse
from numpy import ndarray
import numpy as np
from app.models import csone,ccun
import cv2
def cs(request):#photograph storage generate feature values and store
# images = os. listdir('D:/opencv.img')
# Load image
a = input() #The input is the name of the picture taken by the camera
print(= 39;ok')
cap = cv2.VideoCapture(0) # Turn on the camera , if you add a camera, it is not 0
while (1):
# get a frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1) # The camera is opposed to people , swap the image left and right to return to normal display
# show a frame
cv2.imshow("capture", frame) # generate camera window
b = input()
print('ok')
b=int(b)
if cv2.waitKey(1) & b==1: # Press 1, similar to camera to take pictures
cv2.imwrite("D:/opencv .img/" + a +".jpg", frame) # and save the picture to the path folder
break
cap.release()
cv2.destroyAllWindows()
image_to_be_matched = face_recognition.load_image_file("D:/opencv.img/" + a +".jpg")
#Encode the loaded image into a feature vector& #xff0c; This sentence is a reference to other people's code
image_to_be_matched_encoded = face_recognition.face_encodings(image_to_be_matched)[0]
alist = ndarray.tolist(image_to_be_matched_encoded)# convert the matrix into a list, which is convenient Store in mysql
print(alist)
for i in alist:
print(i)
people=ccun()#ccun is A custom models
people.tezheng=i
people.name=a
people.save()
return HttpResponse("tt")
def opencvcs(request ):#Face recognition
list = []
students = Value
for student in students:
studentlist=[student.tezheng]
list.extend(studentlist)
print(list)
c = np.array(list )#From list to matrix
# Traverse each image
images = os.listdir('D:/opencv.img')
for image in images:
# load image
current_image = face_recognition.load_image_file("D:/opencv.img/" + image)
# encode loaded image as a feature vector
current_image_encoded = ; face_recognition.face_encodings(current_image)[0]
# Compare your image with the image , to see if it is the same person
result = face_recognition.compare_faces([c], current_image_encoded, tolerance= 61;0.48) # Tolerance range , The bigger the requirement, the lower the requirement
# Check whether it is consistent
if result[0] == True:
piutuce=cv2.imread("D:/opencv.img/" + image)
res = cv2. resize(piutuce, (126, 126))
cv2.imshow('yes', res)
cv2.waitKey(0)
cv2.destroyAllWindows()# will correct the image Output
else:
print("Different people: " + image)
#
return HttpResponse("tt")
models.pycode:
from django.db import models
class ccun(models. Model):
name = models.CharField(max_length=16)
tezheng = models.FloatField(default=1)
Database display