CHWHC 4 лет назад
Родитель
Сommit
ce499b2123
4 измененных файлов с 200 добавлено и 212 удалено
  1. 1 1
      backend/Dockerfile
  2. 0 88
      backend/db.py
  3. 162 0
      backend/db_project.py
  4. 37 123
      backend/flask_api.py

+ 1 - 1
backend/Dockerfile

@@ -10,6 +10,6 @@ RUN apt update
 RUN apt install -y  libgl1-mesa-glx
 COPY . .
 
-#RUN python db.py
+#RUN python db_project.py
 
 CMD ["gunicorn", "flask_api:app", "-c", "./gunicorn.conf.py"]

+ 0 - 88
backend/db.py

@@ -1,88 +0,0 @@
-#-*- coding: UTF-8 -*-
-import pymysql
-
-db = pymysql.connect(host="localhost",
-                     user="root",
-                     password="root",
-                     port=3306,  # 端口
-                     charset='utf8')
-
-cursor = db.cursor()
-
-def create_db():
-    db = pymysql.connect(host="localhost",
-                         user="root",
-                         password="root",
-                         #port=3306,  # 端口
-                         #charset='utf8')
-                         )
-
-    cursor = db.cursor()
-    # 使用 execute() 方法执行 SQL,如果表存在则删除
-    #cursor.execute("DROP TABLE IF EXISTS pose")
-
-    # 使用预处理语句创建表
-    cursor.execute("create database if not exists pose")
-    db.commit()
-    db.close()
-    cursor.close()
-def create_table():
-    db = pymysql.connect(host="localhost",
-                         user="root",
-                         password="root",
-                         port=3306,  # 端口
-                         charset='utf8',
-                         database="pose")
-
-    cursor = db.cursor()
-
-
-    sql = """CREATE TABLE pose(
-             id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-             status INT)
-             """
-    insert_table_sql = """INSERT INTO `pose` (`status`) VALUES ('0')"""
-
-    cursor.execute(sql)
-    db.commit()
-    cursor.execute(insert_table_sql)
-    version = cursor.fetchone()
-    print(version)
-    db.commit()
-    db.close()
-def add_new_data():
-    db = pymysql.connect(host="localhost",
-                         user="root",
-                         password="root",
-                         port=3306,  # 端口
-                         database="pose",
-                         charset='utf8')
-
-    cursor = db.cursor()
-
-    insert_table_sql="""INSERT INTO `pose` (`status`) VALUES ( '0')"""
-    cursor.execute(insert_table_sql)
-    db.commit()
-    db.close()
-    cursor.close()
-    return_id=cursor.lastrowid
-    return return_id
-
-def process(id):
-    db = pymysql.connect(host="localhost",
-                         user="root",
-                         password="123456",
-                         port=3306,  # 端口
-                         database="pose",
-                         charset='utf8')
-    sql = "update pose set status = 1 where id = %d " %id
-    cursor = db.cursor()
-    cursor.execute(sql)
-    db.commit()
-    cursor.close()
-    db.close()
-
-if __name__ == "__main__":
-    create_db()
-    create_table()
-    print(add_new_date())

+ 162 - 0
backend/db_project.py

@@ -0,0 +1,162 @@
+import pymysql
+
+ip_ad="106.15.1.178"
+
+db = pymysql.connect(host=ip_ad,
+                     user="root",
+                     password="root",
+                     port=3306,  # 端口
+                     charset='utf8')
+
+cursor = db.cursor()
+
+def create_db():
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         #port=3306,  # 端口
+                         #charset='utf8')
+                         )
+
+    cursor = db.cursor()
+    # 使用 execute() 方法执行 SQL,如果表存在则删除
+    #cursor.execute("DROP TABLE IF EXISTS pose")
+
+    # 使用预处理语句创建表
+    cursor.execute("create database if not exists pose")
+    db.commit()
+    db.close()
+    cursor.close()
+def create_table():
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         charset='utf8',
+                         database="pose")
+
+    cursor = db.cursor()
+
+
+    sql = """CREATE TABLE pose(
+             id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
+             status INT)
+             """
+    insert_table_sql = """INSERT INTO `pose` (`status`) VALUES ('0')"""
+
+    cursor.execute(sql)
+    db.commit()
+    cursor.execute(insert_table_sql)
+    version = cursor.fetchone()
+    print(version)
+    db.commit()
+    db.close()
+def create_table_dynamic():
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         charset='utf8',
+                         database="pose")
+
+    cursor = db.cursor()
+
+    sql = """CREATE TABLE video(
+             id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
+             status INT,
+             mode VARCHAR(20),
+             photo1 INT,
+             photo2 INT,
+             photo3 INT)
+             """
+    insert_table_sql = """INSERT INTO `video` (`stat us`,`mode`,`photo1`,`photo2`,`photo3`) VALUES ('0','jump','-1','-1','-1')"""
+
+    cursor.execute(sql)
+    db.commit()
+    cursor.execute(insert_table_sql)
+    version = cursor.fetchone()
+    print(version)
+    db.commit()
+    db.close()
+def add_new_data():
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         database="pose",
+                         charset='utf8')
+
+    cursor = db.cursor()
+
+    insert_table_sql="""INSERT INTO `pose` (`status`) VALUES ( '0')"""
+    cursor.execute(insert_table_sql)
+    db.commit()
+    db.close()
+    cursor.close()
+    return_id=cursor.lastrowid
+    return return_id
+def add_new_data_dyna(modes):
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         database="pose",
+                         charset='utf8')
+
+    cursor = db.cursor()
+
+    insert_table_sql="""INSERT INTO `video` (`status`,`mode`) VALUES ( '0',modes)"""
+    cursor.execute(insert_table_sql)
+    db.commit()
+    db.close()
+    cursor.close()
+    return_id=cursor.lastrowid
+    return return_id
+
+def lookup_data(id):
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         database="pose",
+                         charset='utf8')
+    cursor = db.cursor()
+    sql="select mode where id=%d"  %id
+    cursor.execute(sql)
+    result=cursor.fetchone()
+    db.commit()
+    cursor.close()
+    db.close()
+    return result
+def process_db(id):
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         database="pose",
+                         charset='utf8')
+    sql = "update pose set status = 1 where id = %d " %id
+
+    cursor = db.cursor()
+    cursor.execute(sql)
+
+    db.commit()
+    cursor.close()
+    db.close()
+
+def get_status(id):
+    db = pymysql.connect(host=ip_ad,
+                         user="root",
+                         password="root",
+                         port=3306,  # 端口
+                         database="pose",
+                         charset='utf8')
+    sql="select status from pose where id =%d"%id
+    cursor=db.cursor()
+    cursor.execute(sql)
+    result=cursor.fetchone()
+    cursor.close()
+    db.close()
+
+    return result
+

+ 37 - 123
backend/flask_api.py

@@ -7,6 +7,8 @@ from PIL import Image
 import base64
 
 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
 
 # str_res=""
 # a dict contains the txt
@@ -17,107 +19,6 @@ bJson = ""
 global_index = [6]
 li_return = []
 img_return = []
-ip_ad="106.15.1.178"
-
-
-import pymysql
-
-db = pymysql.connect(host=ip_ad,
-                     user="root",
-                     password="root",
-                     port=3306,  # 端口
-                     charset='utf8')
-
-cursor = db.cursor()
-
-def create_db():
-    db = pymysql.connect(host=ip_ad,
-                         user="root",
-                         password="root",
-                         #port=3306,  # 端口
-                         #charset='utf8')
-                         )
-
-    cursor = db.cursor()
-    # 使用 execute() 方法执行 SQL,如果表存在则删除
-    #cursor.execute("DROP TABLE IF EXISTS pose")
-
-    # 使用预处理语句创建表
-    cursor.execute("create database if not exists pose")
-    db.commit()
-    db.close()
-    cursor.close()
-def create_table():
-    db = pymysql.connect(host=ip_ad,
-                         user="root",
-                         password="root",
-                         port=3306,  # 端口
-                         charset='utf8',
-                         database="pose")
-
-    cursor = db.cursor()
-
-
-    sql = """CREATE TABLE pose(
-             id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-             status INT)
-             """
-    insert_table_sql = """INSERT INTO `pose` (`status`) VALUES ('0')"""
-
-    cursor.execute(sql)
-    db.commit()
-    cursor.execute(insert_table_sql)
-    version = cursor.fetchone()
-    print(version)
-    db.commit()
-    db.close()
-def add_new_data():
-    db = pymysql.connect(host=ip_ad,
-                         user="root",
-                         password="root",
-                         port=3306,  # 端口
-                         database="pose",
-                         charset='utf8')
-
-    cursor = db.cursor()
-
-    insert_table_sql="""INSERT INTO `pose` (`status`) VALUES ( '0')"""
-    cursor.execute(insert_table_sql)
-    db.commit()
-    db.close()
-    cursor.close()
-    return_id=cursor.lastrowid
-    return return_id
-
-def process_db(id):
-    db = pymysql.connect(host=ip_ad,
-                         user="root",
-                         password="root",
-                         port=3306,  # 端口
-                         database="pose",
-                         charset='utf8')
-    sql = "update pose set status = 1 where id = %d " %id
-    cursor = db.cursor()
-    cursor.execute(sql)
-    db.commit()
-    cursor.close()
-    db.close()
-
-def get_status(id):
-    db = pymysql.connect(host=ip_ad,
-                         user="root",
-                         password="root",
-                         port=3306,  # 端口
-                         database="pose",
-                         charset='utf8')
-    sql="select status from pose where id =%d"%id
-    cursor=db.cursor()
-    status=cursor.execute(sql)
-    cursor.close()
-    db.close()
-    if str(status)=="1":
-        return True
-    return False                      
 
 
 # read txt file and change it to the list format
@@ -149,30 +50,30 @@ 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 += 2 #第六行 i=6
                 dic_2["name"] = "肩部分析"
-                num_risk_1 = int(lines[i + 1].strip())
-                dic_2["value"] = lines[i][:-1] + " " + lines[(i + 2)][:-1]
-                num_risk_2 = int(lines[i + 3].strip())
-                dic_2["level"] = int(max(num_risk_1, num_risk_2))
+                #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 += 5
+                i += 4 #i=10
                 dic_3 = {}
                 dic_3["name"] = "髋部分析"
-                dic_3["value"] = lines[i][:-1] + " " + lines[(i + 2)][:-1]
-                num_risk_1 = int(lines[i + 1].strip())
-                num_risk_2 = int(lines[i + 3].strip())
-                dic_3["level"] = int(max(num_risk_1, num_risk_2))
-                i += 5
-                dic_4 = {"name": "腿型分析", "value": lines[i][:-1] + " " + lines[(i + 1)][:-1] + " " + lines[(i + 2)][:-1]}
+                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())
+                dic_3["level"] = int(max(num_risk_1, 0))
+                i += 4 #i=14
+                dic_4 = {"name": "腿型分析", "value": lines[i][:-1]}
                 li.append(dic_3)
                 li.append(dic_4)
                 dic_data["detail"] = li
-                if ("非" in lines[(i + 2)][:-1]):
+                if ("非" in lines[i][:-1]):
                     dic_4["level"] = 0
                 else:
                     dic_4["level"] = 1
@@ -219,8 +120,6 @@ def run_the_process(index: int = 0, height: float = 110):
     # import analyse_func
     #print(global_index[0])
     do_analysis(global_index[0], height)
-
-
 # 把list转化为可以直接返回的json格式
 
 def list_to_json(li):
@@ -228,7 +127,6 @@ def list_to_json(li):
 
     aJson = json.dumps(li, ensure_ascii=False)
     print(aJson)
-
     # dict1={}
     # dict1["front"]="/home/seecoder/Documents/pose-correction/pc-demo/pose_processed_images/"+str(global_index[0])+"-0-result.jpg"
     # dict1["right"] = "/home/seecoder/Documents/pose-correction/pc-demo/pose_processed_images/" + \
@@ -236,11 +134,7 @@ def list_to_json(li):
     # print(dict1)
     return aJson
     # print(dic_data)
-
-
 app = Flask(__name__)
-
-
 def save_file(file):
     base = "pose_source_images/"
     filename = file.filename
@@ -267,6 +161,24 @@ def save_file(file):
     file.save(save_name)
     print(filename)
     print(save_name)
+#运行动态分析的入口
+def run_video(mode,ana=1):
+    num=add_new_data_dyna(mode)
+    q="./video/"+str(num)+".mp4"
+    #qqq=os.path.abspath(q)
+    #r="capture_image"+str(re["2"]["num"])+".png"
+    #a=os.path.join(qqq,r)
+    video_path=q
+    re= api_dyna(q,mode,ana)
+    return re
+
+def lookup_video(num,ana=0):
+
+    q="./video/"+str(num)+".mp4"
+    video_path=q
+    mode=lookup_data(num)
+    re= api_dyna(q,mode,ana)
+    return re
 
 
 @app.route('/send_form', methods=['POST'])
@@ -282,7 +194,7 @@ def send_form():
     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
     }
 
@@ -317,3 +229,5 @@ if __name__ == '__main__':
     #app.run()
     app.run(host="0.0.0.0", port=8090,debug=True)
     CORS(app, resouces=r'/*')
+
+