DHASH感知算法计算视频相邻帧的相似度

news/2024/10/20 2:23:30/

一个朋友想用python来读取视频帧,根据帧和帧之间相似度判断剪辑痕迹;但是最后发现并没什么用……

原理就是遍历地读取图像相邻帧,将图像相邻帧前处理后,缩小什么的,计算d_hash,然后计算其汉明距离,然后把汉明距离变成相似度,比较相似度,如果相似度低于阈值,就标记时间什么的

不过我一想,欸!好像可以用来解GIF隐写

我就把代码放这里了

1.处理视频版本的

import cv2
import numpy as np
from PIL import Image# Constants
VIDEO_FILE = "faker.mp4"
FRAME_PREFIX = "frame"
FAIL_THRESHOLD = 0.77def d_hash(image):"""Calculate the difference hash for the given image."""hash_bits = []for i in range(8):for j in range(8):hash_bits.append(1 if image[i, j] > image[i, j + 1] else 0)return hash_bitsdef hamming_distance(hash1, hash2):"""Calculate the Hamming distance between two hashes."""return sum(1 for x, y in zip(hash1, hash2) if x != y)def process_frame(frame):"""Process frame to convert it to a format suitable for hash computation."""return np.array(Image.fromarray(frame).resize((9, 8), Image.LANCZOS).convert('L'), 'f')def main():video = cv2.VideoCapture(VIDEO_FILE)edit_detected = Falsewhile True:# Read two consecutive frames from the videosuccess, frame0 = video.read()success1, frame1 = video.read()if not success or not success1:break# Process framesframe0_processed = process_frame(frame0)frame1_processed = process_frame(frame1)# Calculate hashes and distancehash0 = d_hash(frame0_processed)hash1 = d_hash(frame1_processed)distance = hamming_distance(hash0, hash1)similarity = 1.0 - distance / 64.0# Check similarity against the thresholdif similarity < FAIL_THRESHOLD:msec = video.get(cv2.CAP_PROP_POS_MSEC)minute, second = divmod(msec // 1000, 60)print(f"{int(minute)} minute {int(second)} second detected with similarity {similarity}")edit_detected = Trueif not edit_detected:print("No edit detected.")if __name__ == "__main__":main()

2.处理GIF版本的

处理GIF的时候如果遇到相似度过低的帧,就标记出来,而不是时间

from PIL import Image, ImageSequence
import numpy as np# Constants
GIF_FILE = "aaa.gif"
FRAME_PREFIX = "frame"
FAIL_THRESHOLD = 0.77def d_hash(image):"""Calculate the difference hash for the given image."""hash_bits = []for i in range(8):for j in range(8):hash_bits.append(1 if image[i, j] > image[i, j + 1] else 0)return hash_bitsdef hamming_distance(hash1, hash2):"""Calculate the Hamming distance between two hashes."""return sum(1 for x, y in zip(hash1, hash2) if x != y)def process_frame(frame):"""Process frame to convert it to a format suitable for hash computation."""return np.array(frame.resize((9, 8), Image.LANCZOS).convert('L'), 'f')def main():gif = Image.open(GIF_FILE)frames = [frame.copy() for frame in ImageSequence.Iterator(gif)]edit_detected = Falseprevious_hash = Nonefor index, frame in enumerate(frames):# Process frameframe_processed = process_frame(frame)# Calculate hashcurrent_hash = d_hash(frame_processed)if previous_hash is not None:# Calculate distance and similaritydistance = hamming_distance(previous_hash, current_hash)similarity = 1.0 - distance / 64.0# Check similarity against the thresholdif similarity < FAIL_THRESHOLD:print(f"Frame {index} detected with similarity {similarity}")edit_detected = Trueprevious_hash = current_hashif not edit_detected:print("No edit detected.")if __name__ == "__main__":main()

FAIL_THRESHOLD那里可以调节灵敏度,越高越容易筛选帧对

不过话说回来,好像还是很鸡肋……算了……写都写了

兴许以后有用呢。


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

相关文章

动手学深度学习60 机器翻译与数据集

1. 机器翻译与数据集 import os import torch from d2l import torch as d2l#save d2l.DATA_HUB[fra-eng] (d2l.DATA_URL fra-eng.zip,94646ad1522d915e7b0f9296181140edcf86a4f5)#save def read_data_nmt():"""载入“英语&#xff0d;法语”数据集"&qu…

38 Spring

38 Spring 参考资料 Spring-全面详解&#xff08;学习总结&#xff09; 基本概念 Spring理念 : 使现有技术更加实用 . 本身就是一个大杂烩 , 整合现有的框架技术。 Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器&#xff08;框架&#xff09;。 IOC本质 IOC全…

压力测试指南-云环境中的压力测试实践

云环境中的压力测试实践 1. 云环境对压力测试的影响​ 在数字化转型的浪潮中&#xff0c;云环境已成为软件部署与测试的首选。它不仅提供了无限的可扩展性&#xff0c;还极大地改变了我们进行压力测试的方式。传统本地环境中&#xff0c;硬件资源的限制常导致无法模拟真实世界…

php 生成随机数

记录&#xff1a;随机数抽奖 要求&#xff1a;每次生成3个 1 - 10 之间可重复&#xff08;或不可重复&#xff09;的随机数&#xff0c;10次为一轮&#xff0c;每轮要求数字5出现6次、数字4出现3次、…。 提炼需求&#xff1a; 1&#xff0c;可设置最小数、最大数、每次抽奖生…

大厂面试真题-微服务有什么缺点

微服务架构在提供诸多优势的同时&#xff0c;也伴随着一些显著的缺点和挑战。以下是对微服务缺点的详细归纳&#xff1a; 一、操作复杂性增加 微服务架构将一个大型应用程序分解为多个小型服务&#xff0c;这导致系统整体的复杂度显著提高。需要管理更多的服务实例、网络通信…

【Qt Quick】状态:State 使用

State 是 Qt Quick 中管理界面组件状态的关键工具。它允许我们定义组件的不同状态&#xff0c;并且在用户交互或事件发生时进行状态切换&#xff0c;从而实现属性、外观和行为的动态变化。通过使用 State&#xff0c;可以避免复杂的条件逻辑&#xff0c;使代码更加简洁和可维护…

sql server 查看实际的执行计划

SET STATISTICS PROFILE ON select ** SET STATISTICS PROFILE off 范围标准分析轻型分析全局包含 query_post_execution_showplan XE 的 xEvent 会话&#xff1b;从 SQL Server 2012 (11.x) 开始跟踪标志 7412&#xff1b;从 SQL Server 2016 (13.x) SP1 开始全局包含 Showpl…

【WEB应用安全测试指南–蓝队安全测试2】--超详细-可直接进行实战!!!亲测-可进行安全及渗透测试

安全基础理论入门知识参考上一篇《WEB应用安全测试指南蓝队安全测试1》 WEB应用安全测试指南2 一、文件 I/O 类1.1、任意文件上传1.2、任意文件下载1.3、文件包含 二、接口安全类2.1、短信炸弹2.2、邮件炸弹2.3、短信内容可控2.4、邮件内容可控 三、逻辑流程类3.1、越权3.2、未…