怎么安装的话请参考上一篇博客,整的跟连续剧一样~
代码如下:
import cv2
import face_recognition as f
from PIL import Image
import time
import argparse
def check_face_match(baseline_img_path,wantmatch_img_path):
baseline_img = f.load_image_file(baseline_img_path)
wantmatch_img = f.load_image_file(wantmatch_img_path)
# t1 = time.time()
baseline_face_locations = f.face_locations(baseline_img, model="cnn")
wantmatch_face_locations = f.face_locations(wantmatch_img, model="cnn")
baseline_encoding_list = f.face_encodings(baseline_img,known_face_locations=baseline_face_locations)
if(baseline_encoding_list == []):
return [False]
else:
baseline_encoding = baseline_encoding_list[0]
wantmatch_encoding_list = f.face_encodings(wantmatch_img,known_face_locations=wantmatch_face_locations)
if(wantmatch_encoding_list == []):
return [False]
else:
wantmatch_encoding = wantmatch_encoding_list[0]
result = f.compare_faces([wantmatch_encoding],baseline_encoding,tolerance=0.3)
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--img1", help="dr", type=str)
parser.add_argument("--img2", help="dispmber", type=str)
args = parser.parse_args()
# print(args.img1)
# print(args.img2)
if(args.img1 != None):
if(args.img2 != None):
print(check_face_match(args.img1,args.img2))
主要是加了一些判断:比如空传入,传入非人照片,这样不会报错。
然后需要一个pyinstaller ,没有的同学需要安装一下
pip install pyinstaller
最关键的部分来了:
先生成一遍不能用的,主要是为了生成他的spec文件。这样一个py文件的比较简单,多文件参考Python项目打包发布方法_chenzhanhai的专栏-CSDN博客
pyinstaller -F .\match.py
这个是不能用的,但是依然需要这个步骤,如果你运行就会发现缺少dat文件。
你是用anaconda安装的话 Lib\site-packages\face_recognition_models 这个文件夹要复制到项目目录
里面是一些权重文件,这个时候那边生成应该已经完成了,在py文件旁边有一个同名spec文件,找个文本编辑器打开它,注意
5~10行以及14行,改成下面这个样子
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
face_models = [
('.\\face_recognition_models\\models\\dlib_face_recognition_resnet_model_v1.dat', './face_recognition_models/models'),
('.\\face_recognition_models\\models\\mmod_human_face_detector.dat', './face_recognition_models/models'),
('.\\face_recognition_models\\models\\shape_predictor_5_face_landmarks.dat', './face_recognition_models/models'),
('.\\face_recognition_models\\models\\shape_predictor_68_face_landmarks.dat', './face_recognition_models/models'),
]
a = Analysis(['match.py'],
pathex=['C:\\Users\\wins\\FaceMatch'],
binaries=face_models,
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='match',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
改好了再次生成一遍
pyinstaller .\match.spec
去dist文件夹里面找你的exe文件吧~
搞定收工
哦对了 调用的话是这样的:
命令行调用:
.\match.exe --img1 第一张图片路径 --img2 第二张图片路径