图像人脸与视频人脸匹配度检测

news/2024/10/9 11:27:15/
import cv2
import dlib
import numpy as np
import os
from pathlib import Path# 加载预训练模型
face_recognition_model = "dlib_face_recognition_resnet_model_v1.dat"
face_recognition_net = dlib.face_recognition_model_v1(face_recognition_model)detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")def load_image(file_path):"""加载图像"""image = cv2.imread(file_path)return imagedef get_face_encoding(image):"""获取图像中第一个脸部的编码"""face_rects, scores, idx = detector.run(image, 1)if len(face_rects) > 0:shape = predictor(image, face_rects[0])return np.array(face_recognition_net.compute_face_descriptor(image, shape, 100))return Nonedef compare_faces(known_face_encoding, unknown_image_path):"""比较两张图像是否属于同一人"""unknown_image = load_image(unknown_image_path)unknown_face_encoding = get_face_encoding(unknown_image)if known_face_encoding is not None and unknown_face_encoding is not None:distance = np.linalg.norm(known_face_encoding - unknown_face_encoding)threshold = 0.3  # 根据实际情况调整阈值return distance <= thresholdreturn Falsedef extract_first_frame(video_path):"""从视频中提取第一帧"""cap = cv2.VideoCapture(str(video_path))ret, frame = cap.read()if not ret:raise ValueError(f"Failed to read the video {video_path}")return framedef main():# 定义目标目录TARGET_DIR = "special"os.makedirs(TARGET_DIR, exist_ok=True)# 加载参考图像known_image_path = "example.png"  # 请替换为你的样例图片路径known_image = load_image(known_image_path)known_face_encoding = get_face_encoding(known_image)# 遍历当前目录下的所有直接子文件中的 MP4 文件for mp4_file in Path('.').iterdir():if mp4_file.is_file() and mp4_file.suffix.lower() == '.mp4':try:# 从视频中提取第一帧frame = extract_first_frame(mp4_file)# 将第一帧保存为临时文件以便后续处理temp_image_path = "temp_frame.jpg"cv2.imwrite(temp_image_path, frame)# 比较第一帧中的人脸是否与参考图像中的人脸匹配if compare_faces(known_face_encoding, temp_image_path):print(f"Face in {mp4_file.name} matches the reference image.")# 移动匹配的视频到 special 文件夹mp4_file.rename(Path(TARGET_DIR) / mp4_file.name)else:print(f"Face in {mp4_file.name} does not match the reference image.")# 清理临时文件os.remove(temp_image_path)except Exception as e:print(f"Error processing {mp4_file.name}: {str(e)}")if __name__ == "__main__":main()

wget依赖包:
shape_predictor_68_face_landmarks.dat
dlib_face_recognition_resnet_model_v1.dat


http://www.ppmy.cn/news/1536606.html

相关文章

操作系统 | 学习笔记 | 王道 | 4.1 文件系统基础

4.文件管理 4.1 文件系统基础 4.1.1 文件的基本概念 定义 文件是以计算机硬盘为载体的存储在计算机上的信息集合&#xff0c;在用户进行的输入、输出中&#xff0c;以文件位基本单位。 文件管理系统是实现的文件的访问、修改和保存&#xff0c;对文件维护管理的系统。 文件的…

RTSP协议讲解

1.RTSP协议 rtsp&#xff0c;英文全称 Real Time Streaming Protocol&#xff0c;RFC2326&#xff0c;实时流传输协议&#xff0c;是 TCP/IP 协议体系中的一个应用层协议。 RTSP 交互流程 1&#xff09;OPTIONS C--->S 客户端向服务器端发现 OPTIONS&#xff0c;请求可用…

阿里云CDN设置阀值的指南

在如今这个信息爆炸的时代&#xff0c;网站的访问速度和用户体验变得尤为重要。阿里云CDN&#xff08;内容分发网络&#xff09;作为一种高效的解决方案&#xff0c;帮助用户加速网站内容的分发。然而&#xff0c;如何合理设置CDN的阀值&#xff0c;确保资源的高效利用&#xf…

使用 Helsinki-NLP 中英文翻译本地部署 - python 实现

通过 Helsinki-NLP 本地部署中英文翻译功能。该开源模型性价比相对高&#xff0c;资源占用少&#xff0c;对于翻译要求不高的应用场景可以使用&#xff0c;比如单词&#xff0c;简单句式的中英文翻译。 该示例使用的模型下载地址&#xff1a;【免费】Helsinki-NLP中英文翻译本…

QD1-P1 HTML、CSS与JS三者之间的关系

今天开始学习前端基础&#xff0c;新建专题《前端学习笔记1》保存前端基础学习笔记。 专题文章命名以qd1开头。 源课程 视频教程&#xff1a;【Web前端-最通俗易懂HTML、CSS与JS合集 1天速成】 up&#xff1a;遥遥温柔乡 在B站随便搜索了一个前端课程&#xff0c;共91节&am…

卸载各种服务的详尽方法

HTTP 服务器软件&#xff08;如 Apache 或 Nginx&#xff09; 以下是一些常见的卸载方法&#xff1a; **Apache&#xff08;httpd&#xff09;**&#xff1a; 1. 如果是通过 yum 安装的&#xff1a; sudo yum remove httpd 2. 如果是通过源码编译安装的&#xff0c;需要进入…

【探索艺术新纪元:Midjourney中文版,让创意无界!】

&#x1f3a8; 艺术&#xff0c;从此触手可及 在这个数字时代&#xff0c;Midjourney中文版正引领一场艺术创作的革命。作为一款前沿的AI绘画工具&#xff0c;它利用深度学习技术&#xff0c;将你的想象转化为触手可及的艺术作品。无需深厚的绘画功底&#xff0c;只需简单的文…

C#WPF label的content中放置控件实例

本文讲解C#WPF label的content中放置控件实例。 在Label控件中添加一个button按钮控件,并使居中显示。实现代码如下: 实例 <Windowx:Class="ContentDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x=&q…