main.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from flask import Flask, request, jsonify,send_from_directory,Response
  2. import uuid
  3. from change_detect import start
  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', default='default_value')
  18. # 返回 JSON 响应
  19. return jsonify({
  20. "message": "GET 请求成功",
  21. "param": param
  22. })
  23. # POST 上传图片
  24. @app.route('/api/upload_image', methods=['POST'])
  25. def upload_image():
  26. # 获取上传的文件
  27. img1 = request.files['img1']
  28. img2 = request.files['img2']
  29. # 保存文件
  30. filePath1 = uploadPath + str(uuid.uuid4()) + img1.filename
  31. filePath2 = uploadPath + str(uuid.uuid4()) + img2.filename
  32. img1.save(filePath1)
  33. img2.save(filePath2)
  34. return jsonify({
  35. "message": "上传成功",
  36. "data":{
  37. "img1": filePath1,
  38. "img2": filePath2
  39. }
  40. })
  41. # POST 通过url保存图片到本地
  42. @app.route('/api/save_image', methods=['POST'])
  43. def save_image():
  44. # 获取图片路径
  45. data = request.get_json()
  46. img1 = data['img1']
  47. img2 = data['img2']
  48. response1 = requests.get('http://192.168.60.63'+img1)
  49. if response1.status_code != 200:
  50. return jsonify({
  51. "message": "图片1不存在",
  52. "data":{
  53. "img1": img1
  54. }
  55. })
  56. img1 = Image.open(BytesIO(response1.content))
  57. img1Path = uploadPath + str(uuid.uuid4()) + '.png'
  58. img1.save(img1Path,format='PNG')
  59. response2 = requests.get('http://192.168.60.63'+img2)
  60. if response2.status_code != 200:
  61. return jsonify({
  62. "message": "图片2不存在",
  63. "data":{
  64. "img2": img2
  65. }
  66. })
  67. img2 = Image.open(BytesIO(response2.content))
  68. img2Path = uploadPath + str(uuid.uuid4()) + '.png'
  69. img2.save(img2Path,format='PNG')
  70. return jsonify({
  71. "message": "保存成功",
  72. "data":{
  73. "img1": img1Path,
  74. "img2": img2Path,
  75. }
  76. })
  77. # POST 分析图片
  78. @app.route('/api/detect_image', methods=['POST'])
  79. def detect_image():
  80. # 获取图片路径
  81. data = request.get_json()
  82. img1 = data['img1']
  83. img2 = data['img2']
  84. getImgPath=start(img1,img2,outputPath)
  85. return jsonify({
  86. "message": "分析成功",
  87. "data":{
  88. "img": host+getImgPath
  89. }
  90. })
  91. # 访问静态文件
  92. @app.route('/predict/<filename>')
  93. def view_file(filename):
  94. return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
  95. # 测试转发接口
  96. # 目标代理服务 URL
  97. WMS_PROXY_URL = "http://192.168.60.2:9206/proxy/handle/22d38ad36d354a6fb8adc2fb378a66d6/siweiserver/wms"
  98. @app.route('/proxy/handle/22d38ad36d354a6fb8adc2fb378a66d6/siweiserver/wms', methods=['GET'])
  99. def proxy_wms():
  100. # 从客户端请求中获取参数
  101. params = request.args.to_dict()
  102. # 拼接 WMS 请求 URL
  103. wms_url = WMS_PROXY_URL + "?" + "&".join([f"{key}={value}" for key, value in params.items()])
  104. # 发送请求到目标 WMS 服务
  105. response = requests.get(wms_url, stream=True)
  106. # 返回目标服务的响应给客户端
  107. return Response(response.iter_content(chunk_size=1024), content_type=response.headers['Content-Type'])
  108. if __name__ == '__main__':
  109. app.run(debug=True,host='0.0.0.0',port=4100)