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 osapp = 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

 上传一张照片

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

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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/670602.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis-单机安装

试图从官网注册不了我也不知道什么情况。 网盘自取吧&#xff0c;链接&#xff1a;https://pan.baidu.com/s/1KERBQaH9gCT10AGt9z0_jg?pwdyjen 安装比较简单&#xff0c;照着敲就完了每一步都试过了&#xff0c;先单机安装&#xff0c;后面搭建集群。 1.将安装包放到/usr/…

Nest 快速上手 —— (三)中间件 / 异常过滤器

一、 中间件&#xff08;Middleware&#xff09; 1.特点 中间件是一个在路由处理程序之前被调用的函数。中间件函数可以访问请求和响应对象&#xff0c;以及应用程序请求-响应周期中的next()中间件函数。下一个中间件函数通常由一个名为next的变量表示。 中间件函数可以执行以…

Xshell 7启动报错 产品运行所需的信息检索失败

错误信息 产品运行所需的信息检索失败 请重新安装Xshell Code:40002 解决办法 1、把压缩包解压出来 2、在“!)绿化处理.bat”上面右键以管理员身份运行

高项-案例分析万能答案(作业分享)

项目管理&#xff1a;每天进步一点点~ 活到老&#xff0c;学到老 ヾ(◍∇◍)&#xff89;&#xff9e; 何时学习都不晚&#xff0c;加油 一、通用问题原因: 1.项目经理管理经验不足&#xff0c;没有及时发现和解决xx方面的问题。 2.项目管理计划没有得到关键干系人的评审确…

PM - ICE 评分模型:快速的优先级排序框架

ICE 评分模型&#xff08;Impact, Confidence, Ease&#xff09;是一种项目管理和决策工具&#xff0c;广泛用于优先排序和决定应该优先实施哪些项目或任务。 它是由 Sean Ellis 提出的&#xff0c;主要用于增长黑客领域&#xff0c;帮助团队识别和优先处理能带来最大增长的机…

MaxKB宝塔Docker安装并配置域名访问

准备 Linux系统 bt面板 默认环境LNMP随便装 服务器环境配置最好是4G&#xff0c; 占用硬盘存储大概1G 对于一些海外AI产品的对接需要使用香港或者海外的服务器 安装 在宝塔面板中打开SSH或者你本地使用SSH工具去链接服务器 运行docker命令 前提是放开服务器的8080端口 doc…

javaweb学习week7

javaweb学习 十四.Springboot 1.配置优先级 Springboot中支持三种格式的配置文件&#xff1a; 注意&#xff1a;虽然Springboot支持多种格式配置文件&#xff0c;但是在项目开发时&#xff0c;推荐使用一种格式的配置&#xff08;yml是主流&#xff09; Springboot除了支持…

与Apollo共创生态:助力自动驾驶迈向新台阶

引言Apollo七周年大会企业协同工具链携手伙伴共创生态未来展望与总结 引言 2024年4月19日&#xff0c;一场智能汽车未来的盛宴正朝我们走来——Apollo开放平台的七周年大会。 此次大会主题为“破晓•拥抱智变时刻”其中“破晓”象征着新时代的曙光&#xff0c;意味着智能汽车技…

数据库原理与应用实验三 嵌套查询

实验目的和要求 加深和掌握对嵌套查询的理解和应用 实验环境 Windows10 SQLServer 实验内容与过程 图书&#xff08;书号&#xff0c;书名&#xff0c;价格&#xff0c;出版社&#xff09; 读者&#xff08;卡号&#xff0c;姓名&#xff0c;年龄&#xff0c;所属单位&a…

docker安装redis命令及运行

docker安装redis&#xff1a; docker run -d -p 6379:6379 --name redis redis:latest -d: 以 守护进程模式 运行容器&#xff0c;容器启动后会进入后台运行&#xff0c;并脱离当前命令行会话。 -p: 显示端口号。 -p 6379:6379: 将容器内部的 6379 端口映射到宿主机 6379 端…

数据结构学习/复习8--树与二叉树的概念与基本性质练习

一、树 1.概念 2.树的表示 二、二叉树 1.二叉树的概念 2.与性质相关的题

数据分析从入门到精通 2.pandas修真之前戏基础

从爱上自己那天起&#xff0c;人生才真正开始 —— 24.5.6 为什么学习pandas numpy已经可以帮助我们进行数据的处理了&#xff0c;那么学习pandas的目的是什么呢? numpy能够帮助我们处理的是数值型的数据&#xff0c;当然在数据分析中除了数值型的数据还有好多其他类型…