Flask应用的部署和使用,以照片分割为例。

任务是本地上传一张照片,在服务器端处理后,下载到本地。

服务器端已经封装好了相关的程序通过以下语句调用

from amg_test import main
from test import test
main()
test()

首先要在虚拟环境中安装flask

pip install Flask

 文件组织架构

your_project/
│
├── app.py
├── templates/
│   └── download.html
└── uploads/

templates里面是上传、下载时的网页渲染文件

app.py

from flask import Flask, render_template, request, send_from_directory
from amg_test import main
from test import test
import os

app = Flask(__name__)

# 设置一个允许上传的文件类型
ALLOWED_EXTENSIONS = {'png', 'jpg'}

# 检查文件类型是否在允许的范围内
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# 上传文件的路由
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # 检查是否有文件被上传
        if 'file' not in request.files:
            return 'No file part'
        file = request.files['file']
        # 如果用户没有选择文件,浏览器也会发送一个空的文件名
        if file.filename == '':
            return 'No selected file'
        # 检查文件类型是否合法
        if file and allowed_file(file.filename):
            # 保存上传的文件到服务器的 uploads 文件夹下
            file_path = os.path.join('./data/original', file.filename)
            file.save(file_path)
            return render_template('download.html', filename=file.filename)
        else:
            return 'File type not allowed'
    return render_template('upload.html')

# 提供下载处理后的照片的路由
@app.route('/download/<filename>')
def download_file(filename):
    main()
    test()
    return send_from_directory('./data/result', filename, as_attachment=True)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='5000', debug=True)
# 使用通配符IP地址:0.0.0.0

参考:Flask app的run配置IP\PORT远程访问_flask port-CSDN博客

 upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload File</title>
</head>
<body>
    <h1>Upload a File</h1>
    <form method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="Upload">
    </form>
</body>
</html>

download.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Download File</title>
</head>
<body>
    <h1>File uploaded successfully</h1>
    <p><a href="{{ url_for('download_file', filename=filename) }}">Download processed photo</a></p>
</body>
</html>

结果展示

首先要在服务器端(内网地址:10.28.220.198)运行app.py

2024-05-06 18:03:00.163456: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2024-05-06 18:03:00.184021: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-05-06 18:03:00.184043: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-05-06 18:03:00.184679: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-05-06 18:03:00.188218: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2024-05-06 18:03:00.188374: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-05-06 18:03:00.777609: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /home/huanglu/anaconda3/envs/segment-system/lib/python3.10/site-packages/tensorflow/python/compat/v2_compat.py:108: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://10.28.220.198:5000
Press CTRL+C to quit
 * Restarting with stat
 * Serving Flask app 'app'
 * Debug mode: on
2024-05-06 18:03:02.270099: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2024-05-06 18:03:02.291600: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-05-06 18:03:02.291625: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-05-06 18:03:02.292204: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-05-06 18:03:02.295947: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2024-05-06 18:03:02.296127: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-05-06 18:03:02.905518: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /home/huanglu/anaconda3/envs/segment-system/lib/python3.10/site-packages/tensorflow/python/compat/v2_compat.py:108: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
 * Debugger is active!
 * Debugger PIN: 157-069-259

本地计算机打开浏览器,访问http://10.28.220.198:5000/upload

注意没有s,是http

 上传一张照片

点击下载,由于是在下载函数里面处理照片,所以会比较慢

结果和原图是组里的涉密文件,不能展示。

 

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-10 14:08:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-10 14:08:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-10 14:08:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-10 14:08:07       18 阅读

热门阅读

  1. Linux 系统启动时设置一个全局环境变量

    2024-05-10 14:08:07       11 阅读
  2. STM32中usart使用DMA接受数据

    2024-05-10 14:08:07       10 阅读
  3. ceph osd相关

    2024-05-10 14:08:07       9 阅读
  4. ELK原理详解

    2024-05-10 14:08:07       10 阅读
  5. 【LeetCode】面试经典150题:189.轮转数组

    2024-05-10 14:08:07       10 阅读
  6. Python 迭代器

    2024-05-10 14:08:07       10 阅读