0
点赞
收藏
分享

微信扫一扫

python记录9

汤姆torn 2022-01-10 阅读 120

基于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 条评论