2021-11-12

该博客介绍了如何使用哈希算法(均值哈希)来检测视频帧之间的相似度。通过读取视频文件,计算连续帧的哈希值并比较它们的差异,筛选出显著变化的帧进行保存。同时,展示了一个基于Flask的简单应用,用于渲染视频帧和相似度检测结果。
摘要由CSDN通过智能技术生成

html部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
视频分镜
<br>
<video width="640" height="480" controls autoplay>
  <source src="static/ghz.mp4" type="video/mp4">
  <object data="static/ghz.mp4" width="640" height="480">
    <embed width="640" height="480" src="static/ghz.mp4">
  </object>
</video>
<br>
{{framecount}}<br>
{{pic1}}
{% for f in filelist %}
<img height="40" src="{{piclist}}{{f}}"/>
{% endfor %}
</body>
</html>

hash.pybufen

from flask import Flask, render_template
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
print(os.getcwd())

app = Flask(__name__)
# 均值哈希算法
def aHash(img):
    # 缩放为8*8
#     plt.imshow(img)
#     plt.axis('off')  #去掉坐标轴
#     plt.show()
    img = cv2.resize(img, (8, 8))
#     plt.imshow(img)
#     plt.axis('off')  #去掉坐标轴
#     plt.show()

    # 转换为灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # s为像素和初值为0,hash_str为hash值初值为''
    s = 0
    hash_str = ''
    # 遍历累加求像素和
    for i in range(8):
        for j in range(8):
            s = s + gray[i, j]
    # 求平均灰度
    avg = s / 64
    # 灰度大于平均值为1相反为0生成图片的hash值
    for i in range(8):
        for j in range(8):
            if gray[i, j] > avg:
                hash_str = hash_str + '1'
            else:
                hash_str = hash_str + '0'
    return hash_str

# Hash值对比
def cmpHash(hash1, hash2):
    n = 0
    print(hash1)
    print(hash2)
    # hash长度不同则返回-1代表传参出错
    if len(hash1)!=len(hash2):
        return -1
    # 遍历判断
    for i in range(len(hash1)):
        # 不相等则n计数+1,n最终为相似度
        if hash1[i] != hash2[i]:
            n = n + 1
    return n


# img1 = cv2.imread('./pic/image58.jpg')  #  11--- 16 ----13 ---- 0.43
# img2 = cv2.imread('./pic/image59.jpg')
#
#
# hash1 = aHash(img1)
# hash2 = aHash(img2)
# n = cmpHash(hash1, hash2)
# print('均值哈希算法相似度:', n)

def genFrame():
    v_path='/Users/arco/PycharmProjects/homework/static/vm1.mp4'
    image_save='/Users/arco/PycharmProjects/homework/static/hash'

    if not(os.path.exists(image_save)):
        print
        "当前工作目录 : %s" % os.getcwd()

    cap=cv2.VideoCapture(v_path)
    fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)

    _, img1 = cap.read()
    cv2.imwrite('/Users/arco/PycharmProjects/homework/static/hash/image{}.jpg'.format(0), img1)
    for i in range(int(fc)-1):
        _,img2=cap.read()
        hash1 = aHash(img1)
        hash2 = aHash(img2)
        n = cmpHash(hash1, hash2)
        if n>35:
            cv2.imwrite('/Users/arco/PycharmProjects/homework/static/hash/image{}.jpg'.format(i),img2)
            img1=img2
@app.route('/')
def index():
    # return "Hi,Flask!"
    # genFrame()
    pic='static/hash/image'
    framecount=14
    piclist='static/hash/'
    filePath = '/Users/arco/PycharmProjects/homework/static/hash/'
    filelist = os.listdir(filePath)
    print(filelist)
    return render_template('hash.html',pic1=pic,framecount=framecount,filelist=filelist,piclist=piclist)

if "__main__" == __name__:
    app.run(port="5007")

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值