main.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. import os
  8. app = Flask(__name__)
  9. # 设置静态文件夹(如果您想指定路径)
  10. app.config['UPLOAD_FOLDER'] = 'predict/output'
  11. host = 'http://192.168.60.2'
  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', default='default_value')
  19. # 返回 JSON 响应
  20. return jsonify({
  21. "message": "GET 请求成功",
  22. "param": param
  23. })
  24. # POST 上传单张图片
  25. @app.route('/api/upload_single_image', methods=['POST'])
  26. def upload_single_image():
  27. # 获取上传的文件
  28. img = request.files['img']
  29. # 文件夹不存在则创建
  30. if not os.path.exists(uploadPath):
  31. os.makedirs(uploadPath)
  32. # 保存文件
  33. filePath = uploadPath + str(uuid.uuid4()) + img.filename
  34. img.save(filePath)
  35. return jsonify({
  36. "message": "上传成功",
  37. "data": {
  38. "img": filePath
  39. }
  40. })
  41. @app.route('/api/upload_single_image', methods=['POST'])
  42. def upload_single_image():
  43. # 获取上传的文件
  44. img = request.files['img']
  45. # 文件夹不存在则创建
  46. if not os.path.exists(uploadPath):
  47. os.makedirs(uploadPath)
  48. # 保存文件
  49. filePath = uploadPath + str(uuid.uuid4()) + img.filename
  50. img.save(filePath)
  51. return jsonify({
  52. "message": "上传成功",
  53. "data": {
  54. "img": filePath
  55. }
  56. })
  57. @app.route('/api/upload_single_image', methods=['POST'])
  58. def upload_single_image():
  59. # 获取上传的文件
  60. img = request.files['img']
  61. # 文件夹不存在则创建
  62. if not os.path.exists(uploadPath):
  63. os.makedirs(uploadPath)
  64. # 保存文件
  65. filePath = uploadPath + str(uuid.uuid4()) + img.filename
  66. img.save(filePath)
  67. return jsonify({
  68. "message": "上传成功",
  69. "data": {
  70. "img": filePath
  71. }
  72. })
  73. @app.route('/api/upload_single_image', methods=['POST'])
  74. def upload_single_image():
  75. # 获取上传的文件
  76. img = request.files['img']
  77. # 文件夹不存在则创建
  78. if not os.path.exists(uploadPath):
  79. os.makedirs(uploadPath)
  80. # 保存文件
  81. filePath = uploadPath + str(uuid.uuid4()) + img.filename
  82. img.save(filePath)
  83. return jsonify({
  84. "message": "上传成功",
  85. "data": {
  86. "img": filePath
  87. }
  88. })
  89. @app.route('/api/upload_single_image', methods=['POST'])
  90. def upload_single_image():
  91. # 获取上传的文件
  92. img = request.files['img']
  93. # 文件夹不存在则创建
  94. if not os.path.exists(uploadPath):
  95. os.makedirs(uploadPath)
  96. # 保存文件
  97. filePath = uploadPath + str(uuid.uuid4()) + img.filename
  98. img.save(filePath)
  99. return jsonify({
  100. "message": "上传成功",
  101. "data": {
  102. "img": filePath
  103. }
  104. })
  105. @app.route('/api/upload_single_image', methods=['POST'])
  106. def upload_single_image():
  107. # 获取上传的文件
  108. img = request.files['img']
  109. # 文件夹不存在则创建
  110. if not os.path.exists(uploadPath):
  111. os.makedirs(uploadPath)
  112. # 保存文件
  113. filePath = uploadPath + str(uuid.uuid4()) + img.filename
  114. img.save(filePath)
  115. return jsonify({
  116. "message": "上传成功",
  117. "data": {
  118. "img": filePath
  119. }
  120. })
  121. # POST 上传图片
  122. @app.route('/api/upload_image', methods=['POST'])
  123. def upload_image():
  124. # 获取上传的文件
  125. img1 = request.files['img1']
  126. img2 = request.files['img2']
  127. # 保存文件
  128. filePath1 = uploadPath + str(uuid.uuid4()) + img1.filename
  129. filePath2 = uploadPath + str(uuid.uuid4()) + img2.filename
  130. img1.save(filePath1)
  131. img2.save(filePath2)
  132. return jsonify({
  133. "message": "上传成功",
  134. "data": {
  135. "img1": filePath1,
  136. "img2": filePath2
  137. }
  138. })
  139. # POST 通过url保存图片到本地
  140. @app.route('/api/save_image', methods=['POST'])
  141. def save_image():
  142. # 获取图片路径
  143. data = request.get_json()
  144. img1 = data['img1']
  145. img2 = data['img2']
  146. response1 = requests.get('http://192.168.60.63'+img1)
  147. if response1.status_code != 200:
  148. return jsonify({
  149. "message": "图片1不存在",
  150. "data": {
  151. "img1": img1
  152. }
  153. })
  154. img1 = Image.open(BytesIO(response1.content))
  155. img1Path = uploadPath + str(uuid.uuid4()) + '.png'
  156. img1.save(img1Path, format='PNG')
  157. response2 = requests.get('http://192.168.60.63'+img2)
  158. if response2.status_code != 200:
  159. return jsonify({
  160. "message": "图片2不存在",
  161. "data": {
  162. "img2": img2
  163. }
  164. })
  165. img2 = Image.open(BytesIO(response2.content))
  166. img2Path = uploadPath + str(uuid.uuid4()) + '.png'
  167. img2.save(img2Path, format='PNG')
  168. return jsonify({
  169. "message": "保存成功",
  170. "data": {
  171. # "img1": img1Path,
  172. # "img2": img2Path,
  173. "img": 100,
  174. }
  175. })
  176. # POST 分析图片
  177. @app.route('/api/detect_image', methods=['POST'])
  178. def detect_image():
  179. # 获取图片路径
  180. data = request.get_json()
  181. img1 = data['img1']
  182. img2 = data['img2']
  183. getImgPath = start(img1, img2)
  184. return jsonify({
  185. "message": "分析成功",
  186. "data": {
  187. "img": host+'/predict/'+getImgPath
  188. }
  189. })
  190. # 访问静态文件
  191. @app.route('/predict/output/<filename>')
  192. def view_file(filename):
  193. return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
  194. # # 测试转发接口
  195. # # 目标代理服务 URL
  196. # WMS_PROXY_URL = "http://192.168.60.52:9099/hardwareInfo/getRequestIp"
  197. # @app.route('/proxy/handle/22d38ad36d354a6fb8adc2fb378a66d6/siweiserver/wms', methods=['GET'])
  198. # def proxy_wms():
  199. # # 从客户端请求中获取参数
  200. # params = request.args.to_dict()
  201. # # 拼接 WMS 请求 URL
  202. # wms_url = WMS_PROXY_URL + "?" + \
  203. # "&".join([f"{key}={value}" for key, value in params.items()])
  204. # # 发送请求到目标 WMS 服务
  205. # # 自定义 HTTP 头
  206. # headers = {
  207. # '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'
  208. # }
  209. # response = requests.get(wms_url, headers=headers, stream=True)
  210. # # 返回目标服务的响应给客户端
  211. # return Response(response.iter_content(chunk_size=1024), content_type=response.headers['Content-Type'])
  212. if __name__ == '__main__':
  213. app.run(debug=True, host='0.0.0.0', port=4100)