一、设置PyCharm环境
为了使下载在电脑上的包可以继续使用,需要在prefenrence里设置环境,我用的是anaconda的环境,注意此时选择的是system interpreter,在此选项下进行anaconda环境配置
二、阶段一:在main.py里输入代码
from flask import Flask
app = Flask(__name__)
@app.route('/') #路由器
def hello_world():
return 'Hello, World!'
if "__main__"==__name__:
app.run(port="5008")
三、阶段二:与html配合
1:修改main.py
from flask import Flask,render_template
app=Flask(__name__)
@app.route('/')
def index():
#return "Hi,Flask!"
return render_template('index.html')
if "__main__"==__name__:
app.run(port="5008")
2:在项目中创建文件夹templates
3:在Templates里新建一个HTML文件,命名为index.html index.html里内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask分镜</title>
</head>
<body>
视频分镜
</body>
</html>
四、将用cv2抓去到的图片显示在网页上
1.在main.py中加入一段定义genFrame的代码,并在pic文件夹里生成图片
from flask import Flask,render_template
import cv2
import os
app=Flask(__name__)
def genFrame():
v_path='static/video.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()
2.将为筛选的图片和用均值哈希筛选后的图片呈现在网页上
2.1.mian.py
from flask import Flask,render_template
import cv2
import os
app=Flask(__name__)
def genFrame():
v_path='static/video.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)
def aHash(img):
# 缩放为8*8
img = cv2.resize(img, (8, 8))
# 转化为灰度图
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
def compare():
for i in range(254):
img1 = cv2.imread('static/pic/image{}.jpg'.format(i))
img2 = cv2.imread('static/pic/image{}.jpg'.format(i + 1))
hash1 = aHash(img1)
hash2 = aHash(img2)
n = cmpHash(hash1, hash2)
if (n > 18):
cv2.imwrite('static/pic2/image{}.jpg'.format(i + 1), img2)
@app.route('/') #路由器
def index():
#return 'Hi,Flask!'
#genFrame()
compare()
pic='static/pic/image'
framecount=254
pic2='static/pic2/image'
return render_template('index.html',pic1=pic,pic2=pic2,framecount=framecount)
if '__main__'==__name__:
app.run(port='5008')
2.2.index.html
<!DOCTYPE html>
<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/video.mp4">
</object>
</video>
<br>
帧数:{{framecount}}<br>
{% for i in range(framecount) %}
<img height="40" src="{{pic1}}{{i}}.jpg" />
{% endfor %}
<br>
<br>
用均值哈希筛选后的图片:
<img height="40" src="{{pic2}}{{25}}.jpg" />
<img height="40" src="{{pic2}}{{27}}.jpg" />
<img height="40" src="{{pic2}}{{38}}.jpg" />
<img height="40" src="{{pic2}}{{42}}.jpg" />
<img height="40" src="{{pic2}}{{59}}.jpg" />
<img height="40" src="{{pic2}}{{62}}.jpg" />
</body>
</html>