在这周内,老师利用Pycharm将前端和后端结合,并使得剪切出来的分镜头能够在网页上显示,以下为步骤:
一、开始步骤
1.运行环境
注意:如果在pycharm里第三方库找不到
(1)首先检查是否可以用得上
(2)如果确实需要该库,可以在file-settings-project-Python Interpreter里手动查询之前在python里下载的第三方库并进行添加操作
(3)如果以上两种方式都无效,可以尝试用添加下方代码解决
app=Flask(__name__)
2. 在pycharm里创建新文件,命名为main.py
from flask import Flask
app=Flask(__name__)
#__name__是类,区别于app这个实例,app继承类的所有属性和方法
@app.route('/')
def index():
return("Hi,Flask!")
if "__main__"==__name__:
app.run()
#run里面可改为其他的字符串数字,默认5000
网页显示:“Hi,Flask!”
二、flask视频分镜头处理
1.与项目同级新建文件夹,命名为templates【必须命名为templates】,用于存放网页html文件;static文件,防置视频及图片结果等。(可参考上图)
2.我们需要对视频的每一帧进行提取。
注意:(1)需要在项目文件夹下手动新建存放图片的文件夹;(2)尽量不要使用os.chdir()去改变路径,容易“迷惑”Pycharm寻找文件,使用默认地址即可;(3)一定注意路径的使用,比如:视频放在项目的所属文件中
from flask import Flask,render_template
import os
import cv2
app=Flask(__name__)
def genFrame():
v_path='static/ghz.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():
genFrame()
return render_template('index.html')
if "__main__"==__name__:
app.run()
html文件内容则为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
视频分镜
<br>
<video width="180" height="320" controls autoplay>
<source src="static/ghz.mp4" type="video/mp4">
<object data="static/ghz.mp4" width="180" height="320">
<embed width="180" height="320" src="static/ghz.mp4">
</object>
</video>
</body>
</html>
结果:
pic文件里也有所有的图片,例如:
3.利用哈希算法进行视频分镜
1.分镜头选取
新建Hash.py 文件,利用哈希算法从第一张图片开始和后面一张图片进行相似度对比,其中,临界值n的数值需要根据不同的图片提前计算好,ghz.mp4图片差值我们选取的是40(n越大,图片之间的差距越大)
此种方式叫做:离线计算,在线展示
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
def aHash(img):
# 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 = ''
# 遍历累加求像素和
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/image0.jpg') # 11--- 16 ----13 ---- 0.43
# img2 = cv2.imread('pic/image1.jpg')
#
# hash1 = aHash(img1)
# hash2 = aHash(img2)
# n = cmpHash(hash1, hash2)
# print('均值哈希算法相似度:', n)
#
# n = classify_hist_with_split(img1, img2)
# print('三直方图算法相似度:', n)
def genFrame():
v_path='static/ghz.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>40):
cv2.imwrite('static/hash/image{}.jpg'.format(i),img2)
img1=img2
genFrame()
2.在前端显示分镜头数量及图片
在templates下新建hash.html文件,在原基础上添加分镜头数量及引用图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<video width="180" height="320" controls autoplay>
<source src="static/ghz.mp4" type="video/mp4">
<object data="static/ghz.mp4" width="180" height="320">
<embed width="180" height="320" src="static/ghz.mp4">
</object>
</video>
{{imgcount}}<br>
{% for f in filename %}
<img height="40" src="static/hash/{{f}}" />
{% endfor %}
</body>
</html>
3.修改main.py
from flask import Flask,render_template,request
import os
import cv2
import imageColor
app = Flask(__name__)
def genFrame():
v_path = 'ghz.mp4'
image_save = '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():
pic='static/pic/image'
framecount=500
return render_template('index.html',pic1=pic,framecount=framecount)
@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)
if "__main__"==__name__:
app.run()
结果:
注:因为结果是在根目录下得到的,所以网页需要应为http://127.0.0.1:5008/hash才能看得见结果。