• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pycharm实现Web开发

武飞扬头像
ad_yangang
帮助1

一、PyCharm开发环境与简单应用

  • PyCharm是一种Python的集成开发环境,可以高效的进行语言开发,并且支持支持专业Web开发。
  • Flask是一个Web应用框架,使用PyCharm编写。

安装并启动PyCharm后,需要先安装要用的模块,包括Flask、cv2等等。

安装方法:点击setting进入如下页面,点击Package上方的加号,搜索要用到的模块进行安装。

学新通

 注意:

  • 在安装cv2的时候,系统提示我的模块版本过低,我使用了在命令行输入如下命令的方法安装,安装成功。

C:\Users\95378\PycharmProjects\pythonProject\venv\Scripts\python.exe -m pip install opencv-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

  • 在安装PIL的时候也出现了错误。在网上查了一下发现,PIL较多用于2.7版本的Python中,到python3版本已经用Pillow代替PIL了。所以应该安装Pillow库,引入的命令方式也从:import image变为:from PIL import Image 

安装成功后可以利用PyCharm进行一些简单的编写。

        1.在PyCharm中创建新项目(new project)

        2.在main.py中录入如下代码(如果main.py中有内容可删除)

  1.  
    from flask import Flask,render_template
  2.  
     
  3.  
    app=Flask(__name__)
  4.  
     
  5.  
    @app.route('/')
  6.  
    def index():
  7.  
    #return "Hi,Flask!"
  8.  
    return render_template('index.html')
  9.  
     
  10.  
    if "__main__"==__name__:
  11.  
    app.run(port="5008"

        3.创建templates文件夹,用于存放html模板(PyCharm要求文件夹一定是这个名字,才会出现模板)

        4.在templates里新建一个HTML文件,命名为index.html,输入如下代码:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>分镜</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    视频分镜
  9.  
    </body>
  10.  
    </html>

打开网页,就可以看到网页上出现一行字

学新通

还可以通过python将视频切帧。在项目中新建static文件夹,用于存放视频、图片等信息。

学新通

 main.py代码中定义genFrame函数,用于视频切帧,将图像保存到static/pic文件中。

  1.  
    from flask import Flask,render_template
  2.  
    import os
  3.  
    import cv2
  4.  
     
  5.  
    app = Flask(__name__)
  6.  
     
  7.  
    def genFrame(): #定义函数切帧
  8.  
    v_path = "static/ghz.mp4"
  9.  
    image_save = "static/pic"
  10.  
     
  11.  
    if not(os.path.exists(image_save)):
  12.  
    os.mkdir(image_save)
  13.  
     
  14.  
    cap=cv2.VideoCapture(v_path)
  15.  
    fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)
  16.  
    print(fc)
  17.  
     
  18.  
    for i in range(int(fc)):
  19.  
    _,img=cap.read()
  20.  
    cv2.imwrite("static/pic/image{}.jpg".format(i),img)
  21.  
     
  22.  
    @app.route('/')
  23.  
    def index(): #定义函数将framecount和pic传到html中
  24.  
    #return 'Hi, Flask!'
  25.  
    genFrame() #运行上面的函数
  26.  
     
  27.  
    framecount=249 #根据文件中的帧数输入数字。这一行是指将py文件的帧数传到html中
  28.  
    pic = "static/pic/image"
  29.  
    return render_template("index.html", pic1=pic, framecount=framecount)
  30.  
     
  31.  
    if "__main__" == __name__:
  32.  
    app.run(port='5001')
学新通

html文件:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Flask分镜</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    视频分镜
  9.  
    <br>
  10.  
    <video width="640" height="480" controls autoplay>
  11.  
    <source src="static/ghz.mp4" type="video/mp4">
  12.  
    </video>
  13.  
    <br>
  14.  
    帧数:{{framecount}} <!--括号里的是看有没有传成功-->
  15.  
    <br>
  16.  
    {% for i in range(framecount)%}<!--在html写python代码的模板,这是开头,一个循环-->
  17.  
    <img height="40" src="{{pic1}}{{i}}.jpg">
  18.  
    image{{i}}
  19.  
    {%endfor%}<!--这是结尾-->
  20.  
    </body>
  21.  
    </html>
学新通

运行结果(图片共有249帧): 

学新通 

二、在网页中加载视频分镜功能

在flask架构的基础上,将视频分镜的代码片段合并到main.py文件中,再在html中加载出视频,就可以实现在网页加载视频分镜。

哈希均值算法,Python代码:

  1.  
    #哈希均值算法
  2.  
    from flask import Flask,render_template
  3.  
    import os
  4.  
    import cv2
  5.  
     
  6.  
    app = Flask(__name__)
  7.  
     
  8.  
    def aHash(img): #定义哈希值函数
  9.  
    img = cv2.resize(img, (8, 8))
  10.  
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  11.  
    s = 0
  12.  
    hash_str = ''
  13.  
     
  14.  
    for i in range(8):
  15.  
    for j in range(8):
  16.  
    s = s gray[i, j]
  17.  
     
  18.  
    avg = s / 64
  19.  
    for i in range(8):
  20.  
    for j in range(8):
  21.  
    if gray[i, j] > avg:
  22.  
    hash_str = hash_str '1'
  23.  
    else:
  24.  
    hash_str = hash_str '0'
  25.  
    return hash_str
  26.  
     
  27.  
    def cmpHash(hash1, hash2): #定义比较哈希值的函数
  28.  
    n = 0
  29.  
    print(hash1)
  30.  
    print(hash2)
  31.  
     
  32.  
    if len(hash1) != len(hash2):
  33.  
    return -1
  34.  
    # 遍历判断
  35.  
    for i in range(len(hash1)):
  36.  
    # 不相等则n计数 1,n最终为相似度
  37.  
    if hash1[i] != hash2[i]:
  38.  
    n = n 1
  39.  
    return n
  40.  
     
  41.  
    def genFrame(): #定义函数放入视频分镜的代码
  42.  
    v_path = "static/ghz.mp4"
  43.  
    image_save = "static/hash"
  44.  
     
  45.  
    if not(os.path.exists(image_save)):
  46.  
    os.mkdir(image_save)
  47.  
    cap=cv2.VideoCapture(v_path)
  48.  
    fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)
  49.  
    print(fc)
  50.  
    _,img1=cap.read() #读取第一张图像
  51.  
    cv2.imwrite("static/hash/image{}.jpg".format(0),img1)
  52.  
    for i in range(int(fc)-1):
  53.  
    _,img2 = cap.read()
  54.  
    hash1 = aHash(img1)
  55.  
    hash2 = aHash(img2)
  56.  
    n = cmpHash(hash1, hash2)
  57.  
    if (n>35): #数值越大,分的帧数越小
  58.  
    cv2.imwrite("static/hash/image{}.jpg".format(i), img2)
  59.  
    img1=img2
  60.  
     
  61.  
    @app.route('/hash')
  62.  
    def index(): #定义函数将framecount和hash传到html中
  63.  
    genFrame()
  64.  
     
  65.  
    path='static/hash'
  66.  
    filename = os.listdir(path)
  67.  
    framecount=len(filename)
  68.  
    filename.sort(key= lambda x:int(x[5:-4]))
  69.  
    print(filename)
  70.  
    return render_template("hash.html", filename=filename, framecount=framecount)
  71.  
     
  72.  
     
  73.  
    if "__main__" == __name__:
  74.  
    app.run(port="5008")
学新通

HTML代码: 

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>hash分镜</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    视频分镜
  9.  
    <br>
  10.  
    <video width="640" height="480" controls autoplay>
  11.  
    <source src="static/ghz.mp4" type="video/mp4">
  12.  
    </video>
  13.  
    <br>
  14.  
    帧数:{{framecount}}
  15.  
    <br>
  16.  
    {% for i in filename %}
  17.  
    <img height="40" src="static/hash/{{i}}">
  18.  
    {{i}}
  19.  
    {% endfor %}
  20.  
    </body>
  21.  
    </html>
学新通

运行结果:

学新通

直方图算法,Python代码:

  1.  
    from flask import Flask,render_template
  2.  
    import os
  3.  
    import cv2
  4.  
     
  5.  
    app = Flask(__name__)
  6.  
     
  7.  
    # 定义函数比较RGB每个通道的直方图计算相似度
  8.  
    def classify_hist_with_split(image1, image2, size=(256, 256)):
  9.  
    image1 = cv2.resize(image1, size)
  10.  
    image2 = cv2.resize(image2, size)
  11.  
     
  12.  
    sub_image1 = cv2.split(image1)
  13.  
    sub_image2 = cv2.split(image2)
  14.  
    sub_data = 0
  15.  
     
  16.  
    for im1, im2 in zip(sub_image1, sub_image2):
  17.  
    sub_data = calculate(im1, im2)
  18.  
    sub_data = sub_data / 3
  19.  
    return sub_data
  20.  
     
  21.  
    # 计算单通道的直方图的相似值
  22.  
    def calculate(image1, image2):
  23.  
    hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
  24.  
    hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
  25.  
     
  26.  
    degree = 0
  27.  
    for i in range(len(hist1)):
  28.  
    if hist1[i] != hist2[i]:
  29.  
    degree = degree (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
  30.  
    else:
  31.  
    degree = degree 1 # 统计相似
  32.  
    degree = degree / len(hist1)
  33.  
    return degree
  34.  
     
  35.  
     
  36.  
    #定义根目录,分镜
  37.  
    def genFrame():
  38.  
    v_path = "static/ghz.mp4"
  39.  
    image_save = "static/hist"
  40.  
     
  41.  
    if not(os.path.exists(image_save)):
  42.  
    os.mkdir(image_save)
  43.  
     
  44.  
    cap=cv2.VideoCapture(v_path)
  45.  
    fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)
  46.  
    print(fc)
  47.  
    _, img1=cap.read() #读取第一张图像
  48.  
    cv2.imwrite("static/hist/image{}.jpg".format(0), img1)
  49.  
    print(int(fc))
  50.  
    for i in range(int(fc)-1):
  51.  
    _, img2 = cap.read()
  52.  
    n = classify_hist_with_split(img1, img2)
  53.  
    if (n<0.4):
  54.  
    cv2.imwrite("static/hist/image{}.jpg".format(i), img2)
  55.  
    img1=img2
  56.  
     
  57.  
    genFrame()
  58.  
     
  59.  
    @app.route("/hist")
  60.  
    def index():
  61.  
    pic = "static/hist"
  62.  
    filename=os.listdir(pic)
  63.  
    count=int(len(filename))
  64.  
    filename.sort(key=lambda x: int(x[5:-4]))
  65.  
    print(filename)
  66.  
    return render_template("hist.html", pic=pic, filename=filename, count=count)
  67.  
     
  68.  
    if "__main__" == __name__ :
  69.  
    app.run(port="5009")
学新通

HTML代码同上

运行结果:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgahbci
系列文章
更多 icon
同类精品
更多 icon
继续加载