Browse Source

Merge branch 'dev' into dev

张许妍 4 years ago
parent
commit
197b6fcb5f

+ 4 - 2
backend_refactor/db/task.py

@@ -12,11 +12,13 @@ class Task(db.Model):
                              'VIDEO_SINGLELEGSTAND', 'VIDEO_PLANK', 'VIDEO_SQUAT'))
     create_time = db.Column(db.BigInteger)
     status = db.Column(db.Enum('QUEUEING', 'RUNNING', 'FINISHED', 'ERROR'))
-    result = db.Column(db.String(8192))
+    progress = db.Column(db.Integer)
+    result = db.Column(db.Text)
 
-    def __init__(self, uuid, type, create_time=time.time() * 1000, status='QUEUEING', result='{}'):
+    def __init__(self, uuid, type, create_time=time.time() * 1000, status='QUEUEING', progress=0, result='{}'):
         self.uuid = uuid
         self.type = type
         self.create_time = create_time
         self.status = status
+        self.progress = progress
         self.result = result

+ 3 - 1
backend_refactor/rebuild_and_run.sh

@@ -1,4 +1,6 @@
+git pull
 sudo docker build -t backend .
 sudo docker container rm -f backend
-sudo docker run -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_ADDRESS=106.15.1.178 -e MYSQL_DATABASE=openpose -p 8090:80 -v /tmp:/data --gpus all -d --name backend backend
+sudo docker run -e MYSQL_USER=root -e MYSQL_PASSWORD=openpose -e MYSQL_ADDRESS=106.15.1.178 -e MYSQL_DATABASE=openpose -p 8090:80 -v /tmp:/data --gpus all -d --name backend backend
+sudo docker system prune -af
 sudo docker logs -f backend

+ 1 - 1
backend_refactor/requirements.txt

@@ -7,4 +7,4 @@ matplotlib==3.5.3
 scipy==1.7.3
 flask_sqlalchemy==2.5.1
 mysqlclient==1.4.6
-flask-cors==3.0.10
+flask-cors==3.0.10

+ 3 - 0
backend_refactor/route/analyze.py

@@ -28,6 +28,8 @@ def callback_generator(task_uuid):
             with app.app_context():
                 task = Task.query.get(task_uuid)
                 task.status = status
+                if status == 'RUNNING':
+                    task.progress = kwargs['progress']
                 if status == 'FINISHED':
                     task.result = json.dumps(kwargs['result'])
                 if status == 'ERROR':
@@ -37,6 +39,7 @@ def callback_generator(task_uuid):
                 logging.debug("calling to " + status)
         except Exception as e:
             logging.debug("nmsl", exc_info=e)
+            raise e
 
     return callback
 

+ 4 - 4
backend_refactor/route/result.py

@@ -20,12 +20,12 @@ def get_result(task_uuid):
 
     if status == 'FINISHED':
         return make_response(200, "success",
-                             {"task_uuid": task.uuid, "type": task.type, "result": json.loads(task.result)})
+                             {"task_uuid": task.uuid, "type": task.type, "status": "FINISHED", "result": json.loads(task.result)})
     elif status == 'QUEUEING':
-        return make_response(200, "queueing", {"task_uuid": task.uuid, "type": task.type})
+        return make_response(200, "success", {"task_uuid": task.uuid, "type": task.type, "status": "QUEUEING"})
     elif status == 'RUNNING':
-        return make_response(200, "running", {"task_uuid": task.uuid, "type": task.type})
+        return make_response(200, "success", {"task_uuid": task.uuid, "type": task.type, "status": "RUNNING", "progress": task.progress})
     elif status == 'ERROR':
-        return make_response(200, "error", {"task_uuid": task.uuid, "type": task.type})
+        return make_response(200, "success", {"task_uuid": task.uuid, "type": task.type, "status": "ERROR"})
     else:
         assert 0.1 + 0.2 == 0.3, "what do u mean?"

+ 1 - 1
backend_refactor/service/photo_analyzer.py

@@ -24,8 +24,8 @@ class StandingPhotoAnalyzer(BasePhotoAnalyzer):
         self.height = data['height']
 
     def analyze(self, callback: Callable) -> None:
-        callback("RUNNING")
         try:
+            callback("RUNNING",progress=0)
             photos = []
             npys = []
 

+ 6 - 4
backend_refactor/service/video_analyzer.py

@@ -34,11 +34,12 @@ from service.ai.video.video_score.tennisthrow import cal_tennisthrow
 from service.ai.video.video_score.turnaround import cal_turnaround
 
 
-def run_openpose_for_npy_and_video(video_uuid):
+def run_openpose_for_npy_and_video(video_uuid, callback: Callable):
     video_path = UPLOAD_DIR + video_uuid + ".mp4"
 
     cap = cv2.VideoCapture(video_path)
-    FPS = cap.get(5)  # 5 means fps
+    FPS = cap.get(cv2.CAP_PROP_FPS)
+    total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
     out = None
     logging.debug("Splitting video %s, FPS: %s" % (video_path, FPS))
 
@@ -67,6 +68,7 @@ def run_openpose_for_npy_and_video(video_uuid):
                 out.write(canvas)
 
             c += 1
+            callback("RUNNING", progress=min(99,int(100*c/(total_frames/sampling_fps))))
         else:
             break
 
@@ -86,12 +88,12 @@ class BaseVideoAnalyzer:
 
     def analyze(self, callback: Callable):
         try:
-            callback("RUNNING")
+            callback("RUNNING", progress=0)
 
             PICKLE_DUMP_PATH = UPLOAD_DIR + self.video_uuid + ".dump"
 
             if not os.path.exists(PICKLE_DUMP_PATH):  # analyze
-                self.results = run_openpose_for_npy_and_video(self.video_uuid)
+                self.results = run_openpose_for_npy_and_video(self.video_uuid, callback)
                 pickle.dump(self.results, open(PICKLE_DUMP_PATH, "wb"))
             else:  # read cache
                 self.results = pickle.load(open(PICKLE_DUMP_PATH, "rb"))

+ 5 - 3
frontend_refactor/src/containers/TaskList.js

@@ -1,5 +1,5 @@
 import React from "react";
-import {Collapse, Divider} from "antd";
+import {Collapse, Divider, Progress} from "antd";
 import axios from "axios";
 import {BACKEND_URL} from "../config";
 
@@ -13,10 +13,9 @@ class TaskList extends React.Component {
         this.timer = setInterval(() => {
             this.props.tasks.forEach((x) => {
 
-                if (!(x in this.state.details) || this.state.details[x]['status'] == 'running') {
+                if (!(x in this.state.details) || this.state.details[x]['status'] == 'RUNNING' || this.state.details[x]['status'] == 'QUEUEING') {
                     axios.get(BACKEND_URL + "/result/" + x).then(r => {
                         this.state.details[x] = r.data.data
-                        this.state.details[x]['status'] = r.data.msg
                         this.setState(this.state.details)
                     })
                 }
@@ -44,6 +43,8 @@ class TaskList extends React.Component {
                             let data = ""
                             let advice = ""
                             let raw = ""
+                            let progress = details.status === "FINISHED" ? 100 : details.progress
+
 
                             try {
                                 raw = JSON.stringify(details)
@@ -56,6 +57,7 @@ class TaskList extends React.Component {
                             return (
                                 <Collapse.Panel header={"任务 ID: " + x} key={x}>
                                     <p>状态: {details.status}</p>
+                                    <p><Progress style={{width: "50%"}} percent={progress} status={details.status !== "ERROR" ? "active" : "exception"} /></p>
                                     <p>建议: {advice}</p>
                                     <p>评分: {data}</p>
                                     <p>原始数据: {raw}</p>