dongyuanjushi 4 rokov pred
rodič
commit
168cfdf5e9
2 zmenil súbory, kde vykonal 101 pridanie a 80 odobranie
  1. 1 1
      backend/demo_func.py
  2. 100 79
      backend/flask_api.py

+ 1 - 1
backend/demo_func.py

@@ -24,7 +24,7 @@ def run_3d_for_video(original_video_path):
     run_video_reconstruction(original_video_path, compressed_analyzed_path)
     reconstructed_video_path = original_video_path.replace(".mp4", "_reconstructed.mp4")
     print(reconstructed_video_path)
-    return reconstructed_video_path
+    # return reconstructed_video_path
 
 
 def run_openpose_for_image_front(index):

+ 100 - 79
backend/flask_api.py

@@ -6,9 +6,12 @@ from flask_cors import *
 from PIL import Image
 import base64
 
-from analyse_func import  do_analysis
+from analyse_func import do_analysis
 from classify import api_dyna
-from db_project import get_status,process_db,lookup_data ,add_new_data_dyna,add_new_data,create_table_dynamic,create_table,create_db
+from db_project import get_status, process_db, lookup_data, add_new_data_dyna, add_new_data, create_table_dynamic, \
+    create_table, create_db
+import shutil
+from demo_func import run_3d_for_video
 
 # str_res=""
 # a dict contains the txt
@@ -17,14 +20,11 @@ li_last = []
 # store dic_data's json format
 bJson = ""
 global_index = [6]
-global_video_index=0
+global_video_index = 0
 li_return = []
 img_return = []
-flag_run=False
-
-
-
-
+flag_run = False
+front_end_dir = "/root/www/dist"
 
 ##jump
 ##result["jump_power"]["level"]/["path"]/["val"]
@@ -32,7 +32,7 @@ flag_run=False
 ##result["landing_position"]["level"]/["path"]["val"]
 ##juanfu
 ## result["head_power"]["level"]/["path"]/["val"]
-#result["core_strength"]["level"]/["path"]
+# result["core_strength"]["level"]/["path"]
 ##result["body_stability"]["level"]/["val"]
 
 ##pingban
@@ -51,7 +51,6 @@ result["standard"]["level"]/["path"]
 """
 
 
-
 # read txt file and change it to the list format
 def read_pose_processes_txt_result():
     file_dir = "pose_processed_txt"  # the file's path
@@ -81,25 +80,25 @@ def read_pose_processes_txt_result():
                 i = i + 1  # i是头部分析那行
                 dic_1["name"] = "头部分析"
                 dic_1["value"] = lines[i][5:len(lines[i]) - 1]
-                i = i + 1 # 头部数字级别
+                i = i + 1  # 头部数字级别
                 dic_1["level"] = int(lines[i].strip())
                 li.append(dic_1)
                 dic_2 = {}
-                i += 2 #第六行 i=6
+                i += 2  # 第六行 i=6
                 dic_2["name"] = "肩部分析"
-                #num_risk_1 = int(lines[i + 1].strip())
+                # num_risk_1 = int(lines[i + 1].strip())
                 dic_2["value"] = lines[i][:-1] + " " + lines[(i + 1)][:-1]
                 num_risk_2 = int(lines[i + 2].strip())
                 dic_2["level"] = int(num_risk_2)
                 li.append(dic_2)
-                i += 4 #i=10
+                i += 4  # i=10
                 dic_3 = {}
                 dic_3["name"] = "髋部分析"
                 dic_3["value"] = lines[i][:-1] + " " + lines[(i + 1)][:-1]
                 num_risk_1 = int(lines[i + 2].strip())
-                #num_risk_2 = int(lines[i + 3].strip())
+                # num_risk_2 = int(lines[i + 3].strip())
                 dic_3["level"] = int(max(num_risk_1, 0))
-                i += 4 #i=14
+                i += 4  # i=14
                 dic_4 = {"name": "腿型分析", "value": lines[i][:-1]}
                 li.append(dic_3)
                 li.append(dic_4)
@@ -149,12 +148,14 @@ def read_pose_processes_txt_result():
 def run_the_process(index: int = 0, height: float = 110):
     # sys.path.append("../analyse_func.py")
     # import analyse_func
-    #print(global_index[0])
+    # print(global_index[0])
     do_analysis(global_index[0], height)
+
+
 # 把list转化为可以直接返回的json格式
 
 def list_to_json(li):
-    #print(li)
+    # print(li)
 
     aJson = json.dumps(li, ensure_ascii=False)
     print(aJson)
@@ -165,115 +166,136 @@ def list_to_json(li):
     # print(dict1)
     return aJson
     # print(dic_data)
+
+
 app = Flask(__name__)
+
+
 def save_file(filed):
     base = "pose_source_images/"
     filename = filed.filename
     index = global_index[0]
-    #global_index[0] = index
+    # global_index[0] = index
     suffix = ".png"
     if filename.find("jpg") > 0:
         suffix = ".jpg"
     elif filename.find("png") > 0:
         suffix = ".png"
     save_name = ""
-    print(str(index)+"index")
-    print(filename+"filename")
+    print(str(index) + "index")
+    print(filename + "filename")
     if "front" in filename:
         save_name = base + str(index) + str("-0") + suffix
-        print(filename+"filename_front")
-        print(save_name+"savename_front")
+        print(filename + "filename_front")
+        print(save_name + "savename_front")
     elif "right" in filename:
         save_name = base + str(index) + str("-1") + suffix
-        print(filename+"filename_right")
-        print(save_name+"savename_right")
-    else :
+        print(filename + "filename_right")
+        print(save_name + "savename_right")
+    else:
         print("nope")
     filed.save(save_name)
     print(filename)
     print(save_name)
-#运行动态分析的入口
-def run_video(mode,num,ana=1):
-    q="./video/"+str(num)+".mp4"
-    re= api_dyna(q,mode,ana)
+
+
+# 运行动态分析的入口
+def run_video(mode, num, ana=1):
+    q = "./video/" + str(num) + ".mp4"
+    re = api_dyna(q, mode, ana)
     process_db(num)
     return re
 
-def lookup_video(num,ana=0):
-    q="./video/"+str(num)+".mp4"
-    mode=lookup_data(num)
-    re= api_dyna(q,mode,ana)
+
+def lookup_video(num, ana=0):
+    q = "./video/" + str(num) + ".mp4"
+    mode = lookup_data(num)
+    re = api_dyna(q, mode, ana)
     process_db(num)
     return re
 
+
 ###send a video and return a id marked the video (identical)
-@app.route('/get_videoid',methods=['POST'])
+@app.route('/get_videoid', methods=['POST'])
 @cross_origin()
 def get_videoid():
-    dict_video_=request.form.to_dict()
-    dict_video_mp4=request.files.to_dict()
+    dict_video_ = request.form.to_dict()
+    dict_video_mp4 = request.files.to_dict()
 
-    ff=dict_video_mp4["video"]
-    mode=dict_video_["mode"]
+    ff = dict_video_mp4["video"]
+    mode = dict_video_["mode"]
 
     global global_video_index
-    global_video_index=int(add_new_data_dyna(mode))
+    global_video_index = int(add_new_data_dyna(mode))
 
-    base="./video/"
-    filename=base+str(global_video_index)+".mp4"
+    base = "./video/"
+    filename = base + str(global_video_index) + ".mp4"
 
     ff.save(filename)
 
-    return {"id":str(global_video_index)}
+    return {"id": str(global_video_index)}
 
 
-@app.route('/get_video_data',methods=['POST'])
+@app.route('/get_video_data', methods=['POST'])
 @cross_origin()
 def get_video_data():
-    dict_video_=request.form.to_dict()
+    dict_video_ = request.form.to_dict()
     global global_video_index
-    global_video_index=int(dict_video_["id"])
-    mode=str(lookup_data(global_video_index))#get database mode
-    re=run_video(mode,global_video_index,1)
+    global_video_index = int(dict_video_["id"])
+    mode = str(lookup_data(global_video_index))  # get database mode
+    re = run_video(mode, global_video_index, 1)
     return re
 
-@app.route('/get_video_status',methods=['POST'])
+
+@app.route('/get_video_status', methods=['POST'])
 @cross_origin()
 def get_video_status():
-    dict_video_=request.form.to_dict()
+    dict_video_ = request.form.to_dict()
     global global_video_index
-    global_video_index=int(dict_video_["id"])
-    bool=get_status(global_video_index)==1
-    return {"state":get_status(global_video_index)==1}
+    global_video_index = int(dict_video_["id"])
+    # bool = get_status(global_video_index) == 1
+    return {"state": get_status(global_video_index) == 1}
 
 
-
-@app.route('/query_video',methods=['POST'])
+@app.route('/query_video', methods=['POST'])
 @cross_origin()
 def query_video():
-    dict_video_=request.form.to_dict()
+    dict_video_ = request.form.to_dict()
     print(dict_video_)
-    global  global_video_index
-    global_video_index=int(dict_video_["id"])
+    global global_video_index
+    global_video_index = int(dict_video_["id"])
     print(global_video_index)
-    return lookup_video(global_video_index,0)
+    return lookup_video(global_video_index, 0)
+
+
+def get_3D(id):
+    video_path = "video/" + id + ".mp4"
+    run_3d_for_video(video_path)
 
-@app.route('/get_3D',methods=['POST'])
-@cross_origin()
-def get_3D():
-    return "404"
 
-@app.route('/query_3D',methods=['POST'])
+@app.route('/query_3D', methods=['POST'])
 @cross_origin()
 def query_3D():
-    return 404
+    id = request.form.to_dict()["id"]
+    video_path = "video/" + id + "_reconstructed.mp4"
+    saved_video_path = os.path.join(front_end_dir, os.path.basename(video_path))
+    if not os.path.exists(saved_video_path):
+        get_3D(id)
+    shutil.copyfile(video_path, saved_video_path)
+    print(saved_video_path)
+    return {
+        'url': saved_video_path.replace(front_end_dir, "http://106.15.1.178:8001"),
+        # 'status':'finished'
+    }
 
-#result={xx:xx,xx:xx}
-#result={power:{val:xx,path:xx},strength:{val:xx,path:xx}}
 
-#1.锻炼建议
-#2.发送视频
-#3.
+
+# result={xx:xx,xx:xx}
+# result={power:{val:xx,path:xx},strength:{val:xx,path:xx}}
+
+# 1.锻炼建议
+# 2.发送视频
+# 3.
 
 @app.route('/send_form', methods=['POST'])
 @cross_origin()
@@ -282,14 +304,14 @@ def send_form():
 
     fileList = request.files.to_dict()
     global global_index
-    global_index[0]=int(add_new_data())
+    global_index[0] = int(add_new_data())
     for filed in fileList.values():
         save_file(filed)
     height = float(text_data["height"]) if text_data["height"] != "undefined" else None
     run_the_process(global_index[0], height)
     process_db(int(global_index[0]))
     return {
-        "id":global_index[0],
+        "id": global_index[0],
         "status": True if height is not None else False
     }
 
@@ -306,7 +328,7 @@ def run_project():
     id = request.form.to_dict()["id"]
     print(id)
     base = "pose_processed_images"
-    global_index[0]=int(id)
+    global_index[0] = int(id)
     read_pose_processes_txt_result()
     print(base + "/" + str(global_index[0]) + "-0-result.png")
     return {
@@ -316,14 +338,13 @@ def run_project():
     }
 
 
-
 if __name__ == '__main__':
     # read_pose_processes_txt_result()
     # print(list_to_json(li_return))
 
-    #app.run()
-    app.run(host="0.0.0.0", port=8090,debug=True)
-    #app.run(host="0.0.0.0", port=8090)
+    # app.run()
+    app.run(host="0.0.0.0", port=8090, debug=True)
+    # app.run(host="0.0.0.0", port=8090)
     CORS(app, resouces=r'/*')
-    #run_video("gaotaitui",27,0)
-    lookup_video(41,0)
+    # run_video("gaotaitui",27,0)
+    # lookup_video(41, 0)