gushoubang vor 5 Monaten
Ursprung
Commit
b0ca47b949
1 geänderte Dateien mit 34 neuen und 17 gelöschten Zeilen
  1. 34 17
      main.py

+ 34 - 17
main.py

@@ -1,9 +1,10 @@
-from flask import Flask, request, jsonify,send_from_directory,Response
+from flask import Flask, request, jsonify, send_from_directory, Response
 import uuid
 # from change_detect import start
 import requests
 from PIL import Image
 from io import BytesIO
+import os
 
 
 app = Flask(__name__)
@@ -16,6 +17,8 @@ uploadPath = 'predict/upload/'
 outputPath = 'predict/output/'
 
 # GET 请求接口示例
+
+
 @app.route('/api/get_data', methods=['GET'])
 def get_data():
     # 获取请求参数
@@ -27,22 +30,30 @@ def get_data():
     })
 
 # POST 上传单张图片
+
+
 @app.route('/api/upload_single_image', methods=['POST'])
-def upload_image():
+def upload_single_image():
     # 获取上传的文件
-    img = request.files['img1']
+    img = request.files['img']
+    # 文件夹不存在则创建
+    if not os.path.exists(uploadPath):
+        os.makedirs(uploadPath)
+
     # 保存文件
-    filePath = 'predict/' + str(uuid.uuid4()) + img.filename
+    filePath = uploadPath + str(uuid.uuid4()) + img.filename
 
     img.save(filePath)
     return jsonify({
         "message": "上传成功",
-        "data":{
+        "data": {
             "img": filePath
         }
     })
 
 # POST 上传图片
+
+
 @app.route('/api/upload_image', methods=['POST'])
 def upload_image():
     # 获取上传的文件
@@ -56,12 +67,14 @@ def upload_image():
     img2.save(filePath2)
     return jsonify({
         "message": "上传成功",
-        "data":{
+        "data": {
             "img1": filePath1,
             "img2": filePath2
         }
     })
 # POST 通过url保存图片到本地
+
+
 @app.route('/api/save_image', methods=['POST'])
 def save_image():
     # 获取图片路径
@@ -73,29 +86,29 @@ def save_image():
     if response1.status_code != 200:
         return jsonify({
             "message": "图片1不存在",
-            "data":{
+            "data": {
                 "img1": img1
             }
         })
     img1 = Image.open(BytesIO(response1.content))
     img1Path = uploadPath + str(uuid.uuid4()) + '.png'
-    img1.save(img1Path,format='PNG')
+    img1.save(img1Path, format='PNG')
 
     response2 = requests.get('http://192.168.60.63'+img2)
     if response2.status_code != 200:
         return jsonify({
             "message": "图片2不存在",
-            "data":{
+            "data": {
                 "img2": img2
             }
         })
     img2 = Image.open(BytesIO(response2.content))
     img2Path = uploadPath + str(uuid.uuid4()) + '.png'
-    img2.save(img2Path,format='PNG')
+    img2.save(img2Path, format='PNG')
 
     return jsonify({
         "message": "保存成功",
-        "data":{
+        "data": {
             # "img1": img1Path,
             # "img2": img2Path,
             "img1": 100
@@ -118,32 +131,36 @@ def save_image():
 #     })
 
 # 访问静态文件
+
+
 @app.route('/predict/output/<filename>')
 def view_file(filename):
     return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
 
+
 # 测试转发接口
 # 目标代理服务 URL
 WMS_PROXY_URL = "http://192.168.60.52:9099/hardwareInfo/getRequestIp"
+
+
 @app.route('/proxy/handle/22d38ad36d354a6fb8adc2fb378a66d6/siweiserver/wms', methods=['GET'])
 def proxy_wms():
     # 从客户端请求中获取参数
     params = request.args.to_dict()
 
-
     # 拼接 WMS 请求 URL
-    wms_url = WMS_PROXY_URL + "?" + "&".join([f"{key}={value}" for key, value in params.items()])
+    wms_url = WMS_PROXY_URL + "?" + \
+        "&".join([f"{key}={value}" for key, value in params.items()])
     # 发送请求到目标 WMS 服务
     # 自定义 HTTP 头
     headers = {
         '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'
-        }
-    response = requests.get(wms_url,headers=headers, stream=True)
+    }
+    response = requests.get(wms_url, headers=headers, stream=True)
 
     # 返回目标服务的响应给客户端
     return Response(response.iter_content(chunk_size=1024), content_type=response.headers['Content-Type'])
 
 
-
 if __name__ == '__main__':
-    app.run(debug=True,host='0.0.0.0',port=4100)
+    app.run(debug=True, host='0.0.0.0', port=4100)