python记录9

基于PyCharm实现Flask分镜

1.进行视频切帧

定义切帧函数genFrame

def genFrame():
    v_path='static/itzy-output.mp4'
    image_save='static/hash'
 
    if not(os.path.exists(image_save)):
        print(image_save)
        os.mkdir(image_save)
 
    cap=cv2.VideoCapture(v_path)
    fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)
    print(fc)
    _, img1 = cap.read()
    cv2.imwrite('static/hash/image{}.jpg'.format(0), img1)
    print(int(fc))
    for i in range(248):
        _, img2 = cap.read()
        hash1 = aHash(img1)
        hash2 = aHash(img2)
        n = cmpHash(hash1, hash2)
        #print('均值哈希算法相似度:', n)
        if (n<0.6):
            cv2.imwrite('static/hash/image{}.jpg'.format(i),img2)
            img1=img2

查看切帧结果

 
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask分镜</title>
</head>
<body>
视频分镜
<br>
<video width="640" height="480" controls autoplay>
  <source src="static/video.mp4" type="video/mp4">
  <object data="static/video.mp4" width="640" height="480">
    <embed width="640" height="480" src="static/itzy-output.mp4">
  </object>
</video>
<br>
帧数:{{framecount}}<br>
{% for i in range(framecount) %}
<img height="40" src="{{pic1}}{{i}}.jpg" />
{% endfor %}
 
</body>
</html>

利用hash均值进行分镜提取

# 均值哈希算法
def aHash(img):
    # 转换为灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    s = 0
    hash_str = ''
    for i in range(8):
        for j in range(8):
            s = s + gray[i, j]
    # 求平均灰度
    avg = s / 64
    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
    # 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
    sim=1-n/64
    return sim
@app.route('/')
def shot():
    genFrame()
    path='static/hash'
    filename = os.listdir(path)
    framecount=len(filename)
    filename.sort(key=lambda x: int(x[5:-4]))
    print(filename)
    print(type(filename))
 
    return render_template('shot.html', filename=filename, framecount=framecount)
if '__main__'==__name__:
    app.run(port='5008')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值