main.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from flask import Flask
  2. from flask import request,Response,jsonify
  3. from markupsafe import escape
  4. from flask_cors import CORS # 导入Flask-CORS模块
  5. import os
  6. from zipfile import ZipFile
  7. from io import BytesIO
  8. app = Flask(__name__)
  9. CORS(app) # 在应用上启用CORS
  10. @app.route("/")
  11. def hello_world():
  12. str = ("Rscript D:\\Cocorobo\\test\\RobustVideoMatting\\myPython\\Step03.R")
  13. p = os.system(str)
  14. return "<h1>Hello, World!</h1>"
  15. @app.route("/jidechao", methods=['GET', 'POST'])
  16. def hello(name):
  17. if request.method == "POST":
  18. return f"Hello POST, {escape(name)}!"
  19. else:
  20. return f"Hello GET, {escape(name)}!"
  21. # 设置保存图片的目录
  22. UPLOAD_FOLDER = 'saveImgFile'
  23. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  24. # 允许上传的文件类型
  25. ALLOWED_EXTENSIONS = {'zip'}
  26. # 检查文件类型是否允许
  27. def allowed_file(filename):
  28. return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  29. @app.route("/uploadImage",methods=["POST"])
  30. def get_image_save_file():
  31. print(request.files)
  32. # 检查是否有文件上传
  33. if 'file' not in request.files:
  34. return jsonify({'error': 'No file part'})
  35. file = request.files['file']
  36. # 检查文件名和文件类型
  37. if file.filename == '' or not allowed_file(file.filename):
  38. return jsonify({'error': 'Invalid file'})
  39. # 保存文件
  40. if file and allowed_file(file.filename):
  41. with ZipFile(BytesIO(file.read()), 'r') as zip_ref:
  42. # 解压到指定目录
  43. zip_ref.extractall(app.config['UPLOAD_FOLDER'])
  44. str = ("Rscript D:\\Cocorobo\\test\\RobustVideoMatting\\myPython\\Step03.R")
  45. p = os.system(str)
  46. return jsonify({'message': p, 'filename': file.filename})