main.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from flask import Flask, request, jsonify,send_from_directory,Response
  2. import uuid
  3. # from change_detect import change_detect
  4. # from building_extrat import building_extrat
  5. # from road_extrat import road_extrat
  6. import requests
  7. from PIL import Image
  8. from io import BytesIO
  9. app = Flask(__name__)
  10. # 设置静态文件夹(如果您想指定路径)
  11. app.config['UPLOAD_FOLDER'] = 'predict/output'
  12. host = 'http://192.168.60.2'
  13. uploadPath = 'predict/upload/'
  14. outputPath = 'predict/output/'
  15. # GET 请求接口示例
  16. @app.route('/api/get_data', methods=['GET'])
  17. def get_data():
  18. # 获取请求参数
  19. param = request.args.get('param', default='default_value')
  20. # 返回 JSON 响应
  21. return jsonify({
  22. "message": "GET 请求成功",
  23. "param": param
  24. })
  25. # POST 上传单张图片
  26. @app.route('/api/upload_single_image', methods=['POST'])
  27. def upload_image():
  28. # 获取上传的文件
  29. img = request.files['img1']
  30. # 保存文件
  31. filePath = 'predict/' + str(uuid.uuid4()) + img.filename
  32. img.save(filePath)
  33. return jsonify({
  34. "message": "上传成功",
  35. "data":{
  36. "img": filePath
  37. }
  38. })
  39. # POST 上传图片
  40. @app.route('/api/upload_image', methods=['POST'])
  41. def upload_image():
  42. # 获取上传的文件
  43. img1 = request.files['img1']
  44. img2 = request.files['img2']
  45. # 保存文件
  46. filePath1 = uploadPath + str(uuid.uuid4()) + img1.filename
  47. filePath2 = uploadPath + str(uuid.uuid4()) + img2.filename
  48. img1.save(filePath1)
  49. img2.save(filePath2)
  50. return jsonify({
  51. "message": "上传成功",
  52. "data":{
  53. "img1": filePath1,
  54. "img2": filePath2
  55. }
  56. })
  57. # POST 通过url保存图片到本地
  58. @app.route('/api/save_image', methods=['POST'])
  59. def save_image():
  60. # 获取图片路径
  61. data = request.get_json()
  62. img1 = data['img1']
  63. img2 = data['img2']
  64. response1 = requests.get('http://192.168.60.63'+img1)
  65. if response1.status_code != 200:
  66. return jsonify({
  67. "message": "图片1不存在",
  68. "data":{
  69. "img1": img1
  70. }
  71. })
  72. img1 = Image.open(BytesIO(response1.content))
  73. img1Path = uploadPath + str(uuid.uuid4()) + '.png'
  74. img1.save(img1Path,format='PNG')
  75. response2 = requests.get('http://192.168.60.63'+img2)
  76. if response2.status_code != 200:
  77. return jsonify({
  78. "message": "图片2不存在",
  79. "data":{
  80. "img2": img2
  81. }
  82. })
  83. img2 = Image.open(BytesIO(response2.content))
  84. img2Path = uploadPath + str(uuid.uuid4()) + '.png'
  85. img2.save(img2Path,format='PNG')
  86. return jsonify({
  87. "message": "保存成功",
  88. "data":{
  89. "img1": img1Path,
  90. "img2": img2Path,
  91. }
  92. })
  93. # POST 分析图片
  94. @app.route('/api/detect_image', methods=['POST'])
  95. def detect_image():
  96. # 获取图片路径
  97. data = request.get_json()
  98. img1 = data['img1']
  99. img2 = data['img2']
  100. getImgPath=start(img1,img2)
  101. return jsonify({
  102. "message": "分析成功",
  103. "data":{
  104. "img": host+'/predict/'+getImgPath
  105. }
  106. })
  107. # 访问静态文件
  108. @app.route('/predict/output/<filename>')
  109. def view_file(filename):
  110. return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
  111. # # 测试转发接口
  112. # # 目标代理服务 URL
  113. # WMS_PROXY_URL = "http://192.168.60.52:9099/hardwareInfo/getRequestIp"
  114. # @app.route('/proxy/handle/22d38ad36d354a6fb8adc2fb378a66d6/siweiserver/wms', methods=['GET'])
  115. # def proxy_wms():
  116. # # 从客户端请求中获取参数
  117. # params = request.args.to_dict()
  118. # # 拼接 WMS 请求 URL
  119. # wms_url = WMS_PROXY_URL + "?" + "&".join([f"{key}={value}" for key, value in params.items()])
  120. # # 发送请求到目标 WMS 服务
  121. # # 自定义 HTTP 头
  122. # headers = {
  123. # 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
  124. # }
  125. # response = requests.get(wms_url,headers=headers, stream=True)
  126. # # 返回目标服务的响应给客户端
  127. # return Response(response.iter_content(chunk_size=1024), content_type=response.headers['Content-Type'])
  128. if __name__ == '__main__':
  129. app.run(debug=True,host='0.0.0.0',port=4100)