一.新建project,注意所在位置,以及配备python版本
二.新建hash.py,进行分镜处理
from flask import Flask,render_template import os import cv2 app=Flask(__name__) def genframe(): v_path='static/vm1.mp4' image_save='static/pic' if not(os.path.exists(image_save)): os.mkdir(image_save) cap=cv2.VideoCapture(v_path) fc=cap.get(cv2.CAP_PROP_frame_COUNT) for i in range(int(fc)): _,img=cap.read() cv2.imwrite('static/pic/image{}.jpg'.format(i),img) @app.route('/') def index(): #return "Hi,Flask!" #genframe() pic='static/pic/image' framecount=825 return render_template('index.html',pic1=pic,framecount=framecount) if "__main__"==__name__: app.run(port="5008")
三。新建html文件
Flask分镜 视频分镜
帧数:{{framecount}}
{% for i in range(framecount) %} {pic1}}{{i}}.jpg" /> {% endfor %}
四。进行hash分镜
import cv2 import numpy as np import matplotlib.pyplot as plt import os print(os.getcwd()) # 均值哈希算法 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='static/vm1.mp4' image_save='static/hash' if not(os.path.exists(image_save)): os.mkdir(image_save) cap=cv2.VideoCapture(v_path) fc=cap.get(cv2.CAP_PROP_frame_COUNT) _, img1 = cap.read() cv2.imwrite('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('static/hash/image{}.jpg'.format(i),img2) img1=img2 genframe()
五。修改html文件
Title 视频分镜
{{imgcount}}
{% for f in filename %} {f}}"> {% endfor %}
六。最后修改hash.py
@app.route('/hash') def hash(): path='static/hash' filename=os.listdir(path) print(type(filename)) print(filename) imgcount=len(filename) return render_template('hash.html',imgcount=imgcount,filename=filename)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)