12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- from flask import Flask
- from flask import request,Response,jsonify
- from markupsafe import escape
- from flask_cors import CORS # 导入Flask-CORS模块
- import os
- from zipfile import ZipFile
- from io import BytesIO
- app = Flask(__name__)
- CORS(app) # 在应用上启用CORS
- @app.route("/")
- def hello_world():
- str = ("Rscript D:\\Cocorobo\\test\\RobustVideoMatting\\myPython\\Step03.R")
- p = os.system(str)
- return "<h1>Hello, World!</h1>"
- @app.route("/jidechao", methods=['GET', 'POST'])
- def hello(name):
- if request.method == "POST":
- return f"Hello POST, {escape(name)}!"
- else:
- return f"Hello GET, {escape(name)}!"
- # 设置保存图片的目录
- UPLOAD_FOLDER = 'saveImgFile'
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
- # 允许上传的文件类型
- ALLOWED_EXTENSIONS = {'zip'}
- # 检查文件类型是否允许
- def allowed_file(filename):
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
- @app.route("/uploadImage",methods=["POST"])
- def get_image_save_file():
- print(request.files)
- # 检查是否有文件上传
- if 'file' not in request.files:
- return jsonify({'error': 'No file part'})
- file = request.files['file']
- # 检查文件名和文件类型
- if file.filename == '' or not allowed_file(file.filename):
- return jsonify({'error': 'Invalid file'})
- # 保存文件
- if file and allowed_file(file.filename):
- with ZipFile(BytesIO(file.read()), 'r') as zip_ref:
- # 解压到指定目录
- zip_ref.extractall(app.config['UPLOAD_FOLDER'])
- str = ("Rscript D:\\Cocorobo\\test\\RobustVideoMatting\\myPython\\Step03.R")
- p = os.system(str)
- return jsonify({'message': p, 'filename': file.filename})
|