app.py 2.9 KB

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