1.哈希算法
总体程序和之前在jupyternotebook上的差不多,但是需要做部分改动。
from flask import Flask, render_template
import cv2
import os
app = Flask(__name__)
def aHash(img):
img = cv2.resize(img, (8, 8))
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
def cmpHash(hash1, hash2):
n = 0
print(hash1)
print(hash2)
if len(hash1) != len(hash2):
return -1
for i in range(len(hash1)):
if hash1[i] != hash2[i]:
n = n + 1
sim = 1-n/64
return sim
#img1 = cv2.imread('./pic/image0.jpg')
#img2 = cv2.imread('./pic/image1.jpg')
#hash1 = aHash(img1)
#hash2 = aHash(img2)
#n = cmpHash(hash1, hash2)
#print('均值哈希算法相似度:', n)
def genFrame():
v_path = 'static/video.mp4'
image_save = 'static/Hash2021'
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/Hash2021/image{}.jpg'.format(0), img1)
for i in range(int(fc)):
_, img2 = cap.read()
hash1 = aHash(img1)
hash2 = aHash(img2)
n = cmpHash(hash1, hash2)
if n < 0.6:
cv2.imwrite('static/Hash2021/image{}.jpg'.format(i), img2)
img1 = img2
genFrame()
以上这步是将通过哈希算法,分析出视频中不同的帧并保存
运行这一步时需要将右上角的运行程序进行更改,否则将会一直默认运行“main”文件
html文件写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<br>
均值哈希帧数:{{framecount}}<br>
{% for i in range(framecount)%}
<img height="40" src="static/Hash2021/{{filename[i]}}"/>
{{filename[i]}}
{% endfor%}
</body>
</html>
然后再在“main”文件中加入一个新的路由器程序
@app.route('/shot')
def shot():
path='static/Hash2021'
filename=os.listdir(path)
framecount=len(filename)
filename.sort(key=lambda x:int(x[5:-4]))
print(filename)
print(type(filename))
print(filename)
return render_template('hash.html',filename=filename,framecount=framecount)
将右上角改回“main”并运行
得到的页面需要在网址后面加上‘/shot’
???????
2. 聚类色彩提取
先在jupyternotebook上进行分段写,最后再拼在pycharm里
import numpy as np
import os
from PIL import Image
import matplotlib.pyplot as plt
im=np.array(Image.open('IMG_7433.JPG'))
def colorz(filename,n=3):
img=Image.open(filename)
img=img.rotate(-90)
img.thumbnail((200,200))
w,h=img.size
print(w,h)
print('w*h=',w*h)
plt.axis('off')
plt.imshow(img)
plt.show()
points=[]
for count,color in img.getcolors(w*h):
points.append(color)
return points
colorz('IMG_7433.JPG',3)
通过以上代码将图片颜色进行提取:
import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
import matplotlib.pyplot as plt
points=colorz('IMG_7433.JPG',3)
print(points[0:10])
fe=np.array(points,dtype=float)
print(fe[0:10])
book=np.array((fe[100],fe[1],fe[8],fe[8]))
print(type(book))
print("book:\n",book)
codebook,distortion=kmeans(fe,7)
print("codebook:",codebook)
centers=np.array(codebook,dtype=int)
print(centers)
print("distortion:",distortion)
fe=np.array(points)
plt.scatter(fe[:,0],fe[:,2],c='b')
plt.scatter(codebook[:,0],codebook[:,2],c='r')
plt.show()
生成色彩聚类图
?
然后将以上片段粘贴到pycharm的imageColor文件里
import numpy as np
from scipy.cluster.vq import vq, kmeans, whiten
from PIL import Image
def colorz(filename,n=3):
img=Image.open(filename)
img=img.rotate(-90)
img.thumbnail((200,200))
w,h=img.size
print(w,h)
print('w*h=',w*h)
points=[]
for count,color in img.getcolors(w*h):
points.append(color)
return points
def kmeansColor(img,n):
points=colorz(img,3)
fe=np.array(points,dtype=float)
codebook,distortion=kmeans(fe,n)
centers=np.array(codebook,dtype=int)
return centers
main文件中加入
from flask import Flask, render_template
import os
import cv2
import imageColor
@app.route('/')#这是路由器
def index():
#return "Hi,Flask!"
#genFrame()
pic = 'static/pic/image'
framecount = 254
imgcolors=imageColor.kmeansColor('static/pic/image0.jpg',5)
return render_template('index.html', pic1=pic, framecount=framecount,imgcolors=imgcolors)
一定要注意拼写!拼写!拼写!
index中其他内容都不用删掉,加入:?
<br>
帧数:{{framecount}}<br>
{{imgcolors}}<br>
{% for c in imgcolors %}
<font style="color:rgb({{c[0]}},{{c[1]}},{{c[2]}}">video</font>
{% endfor %}
<br>
运行结果:
???????
|