face_recognition號稱是世界上最簡單的基于python的人臉識別庫,是在大名鼎鼎的深度學習框架dlib上做的整合,dlib模型在LFW(Labeled Faces in the Wild)能有99.38的準確率,另外face_recognition提供了相應的命令列工具,可以通過命令列來對圖片檔案夾進行人臉識別,非常的酷,跟隨著文章開啟步伐前行吧!
一、pip安裝dlib庫(建議第二種方法)
pip install Cmake
pip install boost
注意:一般還需要下載VS2019(建議不要用更老的版本!)社區版即可,安裝好配置完進入下一步,
輸入pip install dlib
二、下載whl檔案安裝
本人用的是python3.8 ,需要該檔案請留言或者自行下載,請一定要根據版本下載對應的whl檔案,
打開cmd,進入存放whl檔案目錄,如圖:


二、安裝face_recognition
pip install face_recognition
人臉識別并不需要使用dlib,但是安裝face_recognition一定要先存在dlib庫
三、使用pycharm進行人臉識別
如圖,計算器已經存在face_recognition,

views.py代碼:
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):#拍照儲存生成特征值并存盤
# images = os.listdir('D:/opencv.img')
# 加載影像
a = input() #輸入的是攝像頭拍下圖片的命名
print('ok')
cap = cv2.VideoCapture(0) # 打開攝像頭,如果外加攝像頭便不是0哦
while (1):
# get a frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1) # 攝像頭是和人對立的,將影像左右調換回來正常顯示
# show a frame
cv2.imshow("capture", frame) # 生成攝像頭視窗
b = input()
print('ok')
b=int(b)
if cv2.waitKey(1) & b==1: # 按下1 類似于攝像頭拍照
cv2.imwrite("D:/opencv.img/" + a +".jpg", frame) # 并把圖片保存到路徑的檔案夾
break
cap.release()
cv2.destroyAllWindows()
image_to_be_matched = face_recognition.load_image_file("D:/opencv.img/" + a +".jpg")
# 將加載影像編碼為特征向量,這句是參考別人的代碼哦
image_to_be_matched_encoded = face_recognition.face_encodings(image_to_be_matched)[0]
alist = ndarray.tolist(image_to_be_matched_encoded)#將矩陣轉化為list,便于儲存進mysql
print(alist)
for i in alist:
print(i)
people=ccun()#ccun是自定義的一個models
people.tezheng=i
people.name=a
people.save()
return HttpResponse("tt")
def opencvcs(request):#人臉識別
list = []
students = ccun.objects.filter(name='thth')#篩選出某人的特征值
for student in students:
studentlist=[student.tezheng]
list.extend(studentlist)
print(list)
c = np.array(list)#從list變成矩陣
# 遍歷每張影像
images = os.listdir('D:/opencv.img')
for image in images:
# 加載影像
current_image = face_recognition.load_image_file("D:/opencv.img/" + image)
# 將加載影像編碼為特征向量
current_image_encoded = face_recognition.face_encodings(current_image)[0]
# 將你的影像和影像對比,看是否為同一人
result = face_recognition.compare_faces([c], current_image_encoded, tolerance=0.48) # 容忍度范圍,越大要求越低
# 檢查是否一致
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()#將正確的影像輸出
else:
print("不同人: " + image)
#
return HttpResponse("tt")
models.py代碼:
from django.db import models
class ccun(models.Model):
name = models.CharField(max_length=16)
tezheng = models.FloatField(default=1)
資料庫展示

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/438060.html
標籤:AI
下一篇:python簡易語音助手
