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

devtools/2024/10/15 22:27:52/
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/devtools/126382.html

相关文章

Leetcode 完全平方数

这段代码是用 动态规划&#xff08;Dynamic Programming, DP&#xff09;来解决 LeetCode 第279题「完全平方数」的问题&#xff0c;题目要求给定一个整数 n&#xff0c;找出若干个完全平方数&#xff08;如1, 4, 9, 16等&#xff09;的和&#xff0c;恰好等于 n&#xff0c;并…

2024.10.10计算机外部设备及调试培训

授课老师&#xff1a;杨戬 1.计算机组成 cpu&#xff0c;主板&#xff0c;内存&#xff0c;硬盘&#xff0c;电源&#xff0c;显示器&#xff0c;键盘和鼠标&#xff0c;光驱和显卡&#xff0c;其他外部设备。 2.虚拟机专业版转换 由于我们在2024.10.8的培训中已经安装了wi…

如何接入实时期货行情数据 - 2024最新教程

期货市场通过标准化合约的交易&#xff0c;为投资者提供了在大宗商品、金融工具等方面进行风险对冲和投机的机会。量化交易以计算机模型为核心&#xff0c;通过历史数据和实时数据进行分析和策略执行&#xff0c;减少人为情绪对交易的干扰。由于期货市场的波动性强且价格变化迅…

【VUE】Vue中的组件间通信

Vue2 中组件间传值的方法有以下几种&#xff1a; props&#xff1a;父组件通过 props 属性向子组件传递数据。子组件接收该数据后&#xff0c;即可在其模板中直接使用。$emit() 和事件&#xff1a;子组件通过 $emit() 方法触发一个自定义事件&#xff0c;并把需要传递的数据作…

【C++】基于红黑树封装set和map

&#x1f680;个人主页&#xff1a;小羊 &#x1f680;所属专栏&#xff1a;C 很荣幸您能阅读我的文章&#xff0c;诚请评论指点&#xff0c;欢迎欢迎 ~ 目录 前言一、更高维度的泛型二、模版参数三、比较逻辑的重写四、迭代器4.1 const迭代器4.2 重载4.3 - -重载 五、完整代…

YOLOv8模型改进 第八讲 添加上下文引导模块 ContextGuided

随着目标检测技术的不断进步&#xff0c;YOLOv8 作为一个领先的实时目标检测模型&#xff0c;已在多个任务中表现出色。然而&#xff0c;随着应用场景的复杂性增加&#xff0c;如何进一步提高模型的精度和鲁棒性仍然是一个挑战。本文将探讨如何将 CGNet 的上下文引导模块集成到…

揭秘酱香型白酒中的6大劣质酒的特点,守好你的健康与钱包

你知道吗&#xff1f;居然有 90%的人都喝过这 6 种劣质酱香型白酒&#xff0c;今天酱酒亮哥就带大家一起揭开它们的真面目&#xff0c;看看你中招了没有&#xff01; 先说那种有很浓的生粮味的酱酒&#xff0c;就像刚磨出来还没烧开的豆浆味&#xff0c;喝起来那叫一个难受。想…

hiricacp 连接池校验机制

一、背景 项目发生告警&#xff0c;但是并没有影响业务&#xff0c;看了下日志&#xff0c;红框里面有循环调用了3次 &#xff0c;一直以为是外部的重试在重试&#xff0c;但是外部确没有重试记录&#xff0c;就深扒了代码 二、想法 我知道hikaricp获取连接之后会校验连接的有…