人脸识别详细文档
安装face人脸识别
1.创建好conda环境*
conda create -n findface python=3.8
2.激活conda*
conda activate findface
3.pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
安装内核,一般会报错
4.报错补救开始—————
git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
cmake –build .
cd ..
python setup.py install –set USE_AVX_INSTRUCTIONS=1 –set DLIB_USE_CUDA=1 –set DLIB_GIF_SUPPORT=0
pip install face_recognition
5.接下来就可以愉快的使用import ace_recognition来测试了
!@!cuda加速可能会出问题
使用import dlib
dlib.DLIB_USE_CUDA即可测试,如果是false那么跟我走
a.进入dlib文件夹,找到dlib.sln,找到dlib里面的CMakeLists.txt,找到if (CUDA_FOUND AND cudnn AND (NOT USING_OLD_VISUAL_STUDIO_COMPILER))
把后面的几个and去掉即可,强制过滤。上一句已经修改好了。然后cmake .. -DLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
b.然后cmake –build .也会出错,找到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include\crt里面的host_config.h,
找到|| _MSC_VER >=,把后面的改大一点,强制过滤,cmake –build .即可,再执行
cd ..
python setup.py install –set USE_AVX_INSTRUCTIONS=1 –set DLIB_USE_CUDA=1 –set DLIB_GIF_SUPPORT=0
再执行import dlib
dlib.DLIB_USE_CUDA即为True
6.示例代码
# -*- coding: utf-8 -*-
import face_recognition
import cv2
from utils_yolo.datasets import LoadStreams, LoadImages,LoadRedis
video_capture = cv2.VideoCapture(1)
obama_img = face_recognition.load_image_file(“qin.jpg”)
obama_face_encoding = face_recognition.face_encodings(obama_img)[0]
zhan_img = face_recognition.load_image_file(“zhan.jpg”)
zhan_face_encoding = face_recognition.face_encodings(zhan_img)[0]
yu_img = face_recognition.load_image_file(“yu.jpg”)
yu_face_encoding = face_recognition.face_encodings(yu_img)[0]
img_res=[“qin.jpg”,”zhan.jpg”,”yu.jpg”]
encod=[None] * len(img_res)
for i,p in enumerate(img_res):
print(p)
encod[i]=face_recognition.face_encodings(face_recognition.load_image_file(p))[0]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
dataset = LoadStreams(“F:\yolov5-master\streams.txt”)
for path, im0s in dataset:
for i,frame in enumerate(im0s):
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
if process_this_frame:
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
isok=False
for i,enc in enumerate(encod):
#print(1,enc)
match = face_recognition.compare_faces([enc], face_encoding,tolerance=0.39,cpus=-1)
# if(len(match)>0):
# print(match)
if match[0]:
name = img_res[i]
isok=True
break
if not isok:
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, top+6), font, 1.0, (255, 255, 255), 1)
cv2.imshow(‘Video’, frame)
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
#cv2.destroyAllWindows()
发表评论