当前位置: 首页 > news >正文

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

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.lryc.cn/news/455982.html

相关文章:

  • 【AI绘画】Midjourney进阶:对称构图详解
  • 道路积水检测数据集 1450张 路面积水 带分割 voc yolo
  • 上门安装维修系统小程序开发详解及源码示例
  • 03_23 种设计模式之《原型模式》
  • 【秋招笔试】10.08华为荣耀秋招(已改编)-三语言题解
  • 基于ResNet50模型的船型识别与分类系统研究
  • 一个为分布式环境设计的任务调度与重试平台,高灵活高效率,系统安全便捷,分布式重试杀器!(附源码)
  • 攻防世界(CTF)~Misc-Banmabanma
  • 获取淘宝直播间弹幕数据的技术探索实践方法
  • Python 卸载所有的包
  • JWT(JSON Web Token)、Token、Session和Cookie
  • 国内知名人工智能AI大模型专家培训讲师唐兴通讲授AI办公应用人工智能在营销与销售过程中如何应用数字化赋能
  • Android常用C++特性之std::swap
  • MongoDB数据库详解:特点、架构与应用场景
  • 【C语言刷力扣】1678.设计Goal解析器
  • RK3568平台开发系列讲解(I2C篇)i2c 总线驱动介绍
  • xilinx中bufgce
  • 雷池+frp 批量设置proxy_protocol实现真实IP透传
  • DAY27||回溯算法基础 | 77.组合| 216.组合总和Ⅲ | 17.电话号码的字母组合
  • js基础速成12-正则表达式
  • 使用Selenium自动化测试定位iframe以及修改img标签的display属性值
  • DAY13
  • WPF 自定义用户控件(Content根据加减按钮改变值)
  • CPU、GPU、显卡
  • 深入理解 Django 自定义用户模型
  • 顺序表和链表的区别
  • 系分-数据库总结
  • new Date()解析
  • df 的各种用法 以及与du 的区别
  • 2024年下半年软考准考证什么时候打印?