栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

11.10 pycharm练习

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

11.10 pycharm练习

一、hash分镜

导入包——哈希算法——定义根目录、分镜——显示在网页里

from flask import Flask,render_template
import os
import cv2

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)):
        # 不相等则n计数+1,n最终为相似度
        if hash1[i] != hash2[i]:
            n = n + 1
    return 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)
    print(fc)
    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

@app.route('/hash')
def index():
    genframe()

    path='static/hash'
    filename = os.listdir(path)
    framecount=len(filename)
    filename.sort(key= lambda x:int(x[5:-4]))
    print(filename)
    return render_template("hash.html", filename=filename, framecount=framecount)

if "__main__" == __name__:
    app.run(port="5008")

注意在打开网页时末尾要加上“/hash"

运行结果

二、直方图比较

from flask import Flask,render_template
import cv2
import os

app = Flask(__name__)

os.chdir(r"C:UserslenovoAppDataLocalProgramsPythonPython3711.10")
# 通过得到RGB每个通道的直方图来计算相似度
def classify_hist_with_split(image1, image2, size=(256, 256)):
    # 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
    image1 = cv2.resize(image1, size)
    image2 = cv2.resize(image2, size)
    sub_image1 = cv2.split(image1)
    sub_image2 = cv2.split(image2)
    sub_data = 0

    for im1, im2 in zip(sub_image1, sub_image2):
        sub_data += calculate(im1, im2)
    sub_data = sub_data / 3
    return sub_data


# 计算单通道的直方图的相似值
def calculate(image1, image2):
    hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
    hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
    # 计算直方图的重合度
    degree = 0
    for i in range(len(hist1)):
        if hist1[i] != hist2[i]:
            degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
        else:
            degree = degree + 1
    degree = degree / len(hist1)
    return degree


def genframe():  # 定义根目录,分镜
    v_path = "static/ghz.mp4"
    image_save = "static/hist"

    if not (os.path.exists(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/hist/image{}.jpg".format(0), img1)
    print(int(fc))
    for i in range(248):
        _, img2 = cap.read()
        n = classify_hist_with_split(img1,img2)
        if (n < 0.6):  # 数值越大,分的帧数越小
            cv2.imwrite("static/hist/image{}.jpg".format(i), img2)
            img1 = img2

genframe()

@app.route('/hist')
def index():
    path='static/hist'
    histfile = os.listdir(path)
    histcount=int(len(histfile))
    histfile.sort(key= lambda x:int(x[5:-4]))   #对List的元素排序
    print(histfile)
    return render_template('hist.html',path=path,histfile=histfile,histcount=histcount)

if "__main__" == __name__:
    app.run(port="5009")

注意:直方图与哈希算法分开来写更清晰,运行时注意选择的是哪个py文件

顺序是:先直方图,再定义定义根目录和分镜,最后呈现在网页里

运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487836.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号