在PC上搭建Insightface环境,下载insightface预训练模型,并测试给出的样例代码
InsightFace工程中提供的预训练模型链接可以看前一篇内容(预训练模型和简单介绍)
1.考虑到后面转rknn可能要兼容部分环境,还是沿用了之前搭建的conda环境RKNN_Toolkit2,conda active RKNN_Toolkit2
2.下载insightface库和onnxruntime库:
pip install insightface
pip install onnxruntime
3.将预下载的onnx文件解压到models文件夹
mkdir -p ~/.insightface/models/buffalo_l
mv buffalo_l.zip ~/.insightface/models/buffalo_l
cd ~/.insightface/models/buffalo_l
unzip buffalo_l.zip
4.将想测试的图片文件放到指定路径
{$your_conda_path}/envs/RKNN-Toolkit2/lib/python3.8/site-packages/insightface/data/images
5.修改insightface源码路径下examples的demo_analysis.py并进行测试,主要是屏蔽了对人脸数量的校验,修改输入图片名称和输出图片名称
import argparse
import cv2
import sys
import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
assert insightface.__version__>='0.3'
parser = argparse.ArgumentParser(description='insightface app test')
# general
parser.add_argument('--ctx', default=0, type=int, help='ctx id, <0 means using cpu')
parser.add_argument('--det-size', default=640, type=int, help='detection size')
args = parser.parse_args()
app = FaceAnalysis()
app.prepare(ctx_id=args.ctx, det_size=(args.det_size,args.det_size))
img = ins_get_image('t2')
faces = app.get(img)
#assert len(faces)==6
rimg = app.draw_on(img, faces)
cv2.imwrite("./t2_output.jpg", rimg)
# then print all-to-all face similarity
feats = []
for face in faces:
feats.append(face.normed_embedding)
feats = np.array(feats, dtype=np.float32)
sims = np.dot(feats, feats.T)
print(sims)
6.执行demo测试
python demo_analysis.py