app.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. from flask import Flask, request, jsonify, send_from_directory, Response, make_response
  3. import uuid
  4. import requests
  5. from PIL import Image
  6. from io import BytesIO
  7. import siwei_config
  8. app = Flask(__name__)
  9. # 设置静态文件夹(如果您想指定路径)
  10. app.config['UPLOAD_FOLDER'] = 'predict/output'
  11. host = siwei_config.CONFIG['host']
  12. uploadPath = 'predict/upload/'
  13. outputPath = 'predict/output/'
  14. # GET 请求接口示例
  15. @app.route('/api/get_data', methods=['GET'])
  16. def get_data():
  17. # 获取请求参数
  18. param = request.args.get('param')
  19. # 返回 JSON 响应
  20. data = jsonify({
  21. "message": "GET 请求成功",
  22. "param": param
  23. })
  24. response = make_response(data)
  25. # response.headers['Content-Type'] = 'text/plain; charset=utf-8'
  26. return response
  27. # GET 请求接口示例
  28. @app.route('/api/postgisupdate', methods=['POST'])
  29. def postgisupdate():
  30. # 获取请求参数
  31. param = request.args.get('param')
  32. # 返回 JSON 响应
  33. return jsonify({
  34. "message": "POST 请求成功",
  35. "param": param
  36. })
  37. # POST 上传图片
  38. @app.route('/api/upload_image', methods=['POST'])
  39. def upload_image():
  40. # 获取上传的文件
  41. img1 = request.files['img1']
  42. img2 = request.files['img2']
  43. # 保存文件
  44. filePath1 = uploadPath + str(uuid.uuid4()) + img1.filename
  45. filePath2 = uploadPath + str(uuid.uuid4()) + img2.filename
  46. img1.save(filePath1)
  47. img2.save(filePath2)
  48. return jsonify({
  49. "message": "上传成功",
  50. "data": {
  51. "img1": filePath1,
  52. "img2": filePath2
  53. }
  54. })
  55. # POST 通过url保存图片到本地
  56. @app.route('/api/save_image', methods=['POST'])
  57. def save_image():
  58. # 获取图片路径
  59. data = request.get_json()
  60. img1 = data['img1']
  61. img2 = data['img2']
  62. response1 = requests.get(host + img1)
  63. if response1.status_code != 200:
  64. return jsonify({
  65. "message": "图片1不存在",
  66. "data": {
  67. "img1": img1
  68. }
  69. })
  70. img1 = Image.open(BytesIO(response1.content))
  71. img1Path = uploadPath + str(uuid.uuid4()) + '.png'
  72. img1.save(img1Path, format='PNG')
  73. response2 = requests.get(host + img2)
  74. if response2.status_code != 200:
  75. return jsonify({
  76. "message": "图片2不存在",
  77. "data": {
  78. "img2": img2
  79. }
  80. })
  81. img2 = Image.open(BytesIO(response2.content))
  82. img2Path = uploadPath + str(uuid.uuid4()) + '.png'
  83. img2.save(img2Path, format='PNG')
  84. return jsonify({
  85. "message": "保存成功",
  86. "data": {
  87. "img1": img1Path,
  88. "img2": img2Path,
  89. }
  90. })
  91. # 访问静态文件
  92. @app.route('/predict/<filename>')
  93. def view_file(filename):
  94. return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
  95. if __name__ == '__main__':
  96. app.run(debug=True, host='0.0.0.0', port=6500)