main.py 6.5 KB

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