人脸真伪检测:使用训练模型识别图像中的人脸真伪
import cv2 from face_recognition import FaceDetector
class TestFaceDetector: '初始化类,传入训练模型路径和待检测图片路径' def init(self, model_path, image_path): self.model_path = model_path self.image_path = image_path
'运行测试,加载图片和模型,检测人脸,绘制方框并标注概率'
def run_test(self):
# 加载图片
image = cv2.imread(self.image_path)
# 加载人脸检测模型
face_detector = FaceDetector(self.model_path)
# 检测图片中的人脸
faces = face_detector.detect_faces(image)
# 为每个检测到的人脸绘制方框并添加标签
for face in faces:
# 获取方框坐标
x, y, w, h = face['box']
# 绘制方框
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 获取人脸真实性的概率
prob_real = face['prob_real']
# 使用真实性概率作为标签
label = 'Real: {:.2f}%'.format(prob_real * 100)
cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 显示带有方框和标签的图片
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if name == 'main': model_path = 'path/to/model' image_path = 'path/to/image' test = TestFaceDetector(model_path, image_path) test.run_test()
原文地址: https://www.cveoy.top/t/topic/oidY 著作权归作者所有。请勿转载和采集!