- 上节我们介绍了如何安装InsightFace的WHL包
- 上节地址 https://blog.csdn.net/Andrwin/article/details/119209774
- 本节主要介绍从python代码里面调用InsightFace包
1.导入InsightFace
import insightface_paddle as face
import logging
logging.basicConfig(level=logging.INFO)
2.建立人脸索引
parser = face.parser()
args = parser.parse_args()
args.build_index = "./demo/friends/index.bin"
args.img_dir = "./demo/friends/gallery"
args.label = "./demo/friends/gallery/label.txt"
predictor = face.InsightFace(args)
predictor.build_index()
3.单独检测人脸
import cv2
parser = face.parser()
args = parser.parse_args()
args.det = True
args.output = "./output"
path = "./demo/friends/query/friends1.jpg"
img = cv2.imread(path)[:, :, ::-1]
predictor = face.InsightFace(args)
# 两种传入形式均可
res = predictor.predict(path)
res = predictor.predict(img)
print(next(res))
3.1 视频检测
parser = face.parser()
args = parser.parse_args()
args.det = True
args.output = "./output"
input_path = "./demo/friends/query/friends.mp4"
predictor = face.InsightFace(args)
res = predictor.predict(input_path)
for _ in res:
print(_)
4.单独识别人脸[ Face Verification 1:1 ]
import cv2
parser = face.parser()
args = parser.parse_args()
args.rec = True
args.index = "./demo/friends/index.bin"
path = "./demo/friends/query/Rachel.png"
img = cv2.imread(path)[:, :, ::-1]
predictor = face.InsightFace(args)
#两种传入形式均可
res = predictor.predict(path, print_info=True)
res = predictor.predict(img, print_info=True)
next(res)
5.检测+识别人脸 [ Face Recognition 1:N ]
import cv2
parser = face.parser()
args = parser.parse_args()
args.det = True
args.rec = True
args.index = "./demo/friends/index.bin"
args.output = "./output"
path = "./demo/friends/query/friends2.jpg"
img = cv2.imread(path)[:, :, ::-1]
predictor = face.InsightFace(args)
# 两种输入方式均可
res = predictor.predict(path, print_info=True)
res = predictor.predict(img, print_info=True)
next(res)
5.1 视频检测
parser = face.parser()
args = parser.parse_args()
args.det = True
args.rec = True
args.index = "./demo/friends/index.bin"
args.output = "./output"
input_path = "./demo/friends/query/friends.mp4"
predictor = face.InsightFace(args)
res = predictor.predict(input_path, print_info=True)
for _ in res:
pass