LeoDu 4 lat temu
rodzic
commit
47c8a95b10
2 zmienionych plików z 35 dodań i 27 usunięć
  1. 30 26
      backend_refactor/route/analyze.py
  2. 5 1
      backend_refactor/route/result.py

+ 30 - 26
backend_refactor/route/analyze.py

@@ -1,5 +1,6 @@
 import uuid, json
-from service.video_analyzer import BaseVideoAnalyzer
+from backend_refactor.db import task
+from service.video_analyzer import BaseVideoAnalyzer, CrunchVideoAnalyzer, HighKneesVideoAnalyzer, PlankVideoAnalyzer, SquatVideoAnalyzer
 # from service.video.Video3DAnalyzer import Video3DAnalyzer
 
 from db import db
@@ -8,7 +9,7 @@ from db.task import Task
 from worker.thread_pool import pool
 from route.util import make_response
 from flask import Blueprint
-from service.StandingPhotoAnalyzer import StandingPhotoAnalyzer
+from service.photo_analyzer import StandingPhotoAnalyzer
 
 import logging
 
@@ -37,40 +38,43 @@ def callback_generator(task_uuid):
     return callback
 
 
-
-@bp.route("photos/analysis", methods=['POST'])
-def photo_analysis():
+@bp.route("analysis", methods=['POST'])
+def analysis():
     req = request.get_json()
-    
-    ana = StandingPhotoAnalyzer(req['front'], req['right'], req['height'])
 
-    task_uuid = str(uuid.uuid4())
-    new_task = Task(task_uuid, 'PHOTO_STANDINGPOSE')
-    db.session.add(new_task)
-    db.session.commit()
+    types = {
+        "PHOTO_STANDING": StandingPhotoAnalyzer,
+        "VIDEO_HIGHKNEES": HighKneesVideoAnalyzer,
+        "VIDEO_CRUNCH": CrunchVideoAnalyzer,
+        "VIDEO_STANDINGLONGJUMP": StandingPhotoAnalyzer,
+        "VIDEO_PLANK": PlankVideoAnalyzer,
+        "VIDEO_SQUAT": SquatVideoAnalyzer
+    }
 
-    # ana.analyze(callback_generator(task_uuid))
 
-    pool.submit(ana.analyze, callback_generator(task_uuid))
+    ana_type = types.get(req.get('type'))
 
-    return make_response(201, "success", {'task_uuid' : task_uuid})
+    if ana_type is None:
+        return make_response(400, "invalid type", {})
 
-@bp.route("videos/analysis", methods=['POST'])
-def video_analysis():
-    req = request.get_json()
-    
-    # ana_3d = Video3DAnalyzer()
-    ana = BaseVideoAnalyzer(video_uuid=req['video_uuid'])
+    try:
+        ana = ana_type(req.get('data'))
+    except Exception as e:
+        return make_response(400, str(e), {})
 
-    task_uuid = str(uuid.uuid4())
-    new_task = Task(task_uuid, "VIDEO_CRUNCH")
-    db.session.add(new_task)
-    db.session.commit()
+    
 
-    pool.submit(ana.analyze, callback_generator(task_uuid))
+    with __import__("app").app.app_context():
+        task_uuid = str(uuid.uuid4())
+        new_task = Task(task_uuid, req.get('type'))
+        db.session.add(new_task)
+        db.session.commit()
 
-    return make_response(201, "success", {'task_uuid' : task_uuid})
+    logging.debug("task created.")
 
+    pool.submit(ana.analyze, callback_generator(task_uuid))
 
+    logging.debug("task submited")
 
+    return make_response(201, "success", {'task_uuid': task_uuid})
 

+ 5 - 1
backend_refactor/route/result.py

@@ -1,13 +1,17 @@
-from operator import imod
 from flask import Blueprint
 from route.util import make_response
 from db.task import Task
+import uuid
 
 bp = Blueprint("result", __name__, url_prefix='/')
 
 
 @bp.route("/result/<task_uuid>", methods=["GET"])
 def get_result(task_uuid):
+    try:
+        uuid.UUID(task_uuid, version=4)
+    except:
+        return make_response(400, "invalid uuid", {})
     task = Task.query.get(task_uuid)
     status = task.status