Browse Source

refactor Photo Analyzer

lyc8503 4 years ago
parent
commit
b9af5198b1

+ 7 - 1
backend_refactor/app.py

@@ -10,7 +10,6 @@ from db import db
 app = Flask(__name__)
 app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
 
-
 app.config['SQLALCHEMY_DATABASE_URI'] = MYSQL_URI
 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
 db.init_app(app)
@@ -18,6 +17,7 @@ with app.app_context():
     # db.drop_all()
     db.create_all()
 
+
 @app.route("/")
 def hello_world():
     return "<p>Hello, World!</p>"
@@ -34,6 +34,12 @@ def error_handler(e):
     return make_response(413, "entity too large, max acceptable: " + str(MAX_CONTENT_LENGTH) + " bytes", {})
 
 
+@app.errorhandler(404)
+def error_handler(e):
+    logging.exception(e)
+    return make_response(404, "path not found.", {})
+
+
 @app.errorhandler(Exception)
 def error_handler(e):
     logging.exception(e)

+ 0 - 2
backend_refactor/config/__init__.py

@@ -5,5 +5,3 @@ UPLOAD_DIR = "/data/"
 MAX_CONTENT_LENGTH = 100 * 1024 * 1024
 
 MYSQL_URI = f'mysql://{os.environ["MYSQL_USER"]}:{os.environ["MYSQL_PASSWORD"]}@{os.environ["MYSQL_ADDRESS"]}/{os.environ["MYSQL_DATABASE"]}'
-
-CACHE_DIR = "/tmp/"

+ 3 - 0
backend_refactor/service/photo/BasePhotoAnalyzer.py

@@ -0,0 +1,3 @@
+class BasePhotoAnalyzer:
+    def __init__(self):
+        pass

+ 14 - 28
backend_refactor/service/photo/StandingPhotoAnalyzer.py

@@ -1,4 +1,3 @@
-
 from service.photo import BasePhotoAnalyzer
 
 import cv2
@@ -7,47 +6,34 @@ import copy
 import numpy as np
 from PIL import Image
 import os
-# from config import CACHE_DIR
 
-CACHE_DIR = "/tmp/"
+from config import UPLOAD_DIR
 
 import service.photo.src1.util2 as util
 from service.photo.src1.body import Body
 from service.photo.src1.torch_openpose import torch_openpose
 import service.photo.src1 as src1
-# from src.infer_video_d2 import infer_image,load_predictor
-# from video_pose.inference.infer_video_d2 import run_3d
-# from video_pose.data.prepare_data_2d_custom import custom
-# from video_pose.run import run_video_reconstruction
-
-
 
-class StandingPhotoAnalyzer():
 
-    def analyze(self, *args):
+class StandingPhotoAnalyzer(BasePhotoAnalyzer):
 
-        assert len(args) == 2, "Invalid arguments: expect 2 photo uuids."
+    def __init__(self, *args):
+        super().__init__()
+        assert len(self.args) == 2, "Invalid arguments: expect 2 photo uuids."
+        self.args = args
 
-        for i in args:
-            test_image = CACHE_DIR + '{}.jpg'.format(i)
+    def analyze(self):
+        for i in self.args:
+            test_image = UPLOAD_DIR + '{}.jpg'.format(i)
 
-            oriImg = cv2.imread(test_image)  # B,G,R order
+            ori_img = cv2.imread(test_image)  # B,G,R order
 
             tp = torch_openpose('body_25')
-            poses = tp(oriImg)
+            poses = tp(ori_img)
 
-            np.save(CACHE_DIR + '{}.npy'.format(i), poses)
+            np.save(UPLOAD_DIR + '{}.npy'.format(i), poses)
 
-            canvas = copy.deepcopy(oriImg)
+            canvas = copy.deepcopy(ori_img)
             canvas = src1.util.draw_bodypose(canvas, poses, 'body_25')
 
-            cv2.imwrite(CACHE_DIR + '{}-result.jpg'.format(i), canvas)
-
-    def __init__(self) -> None:
-        super().__init__()
-
-
-
-if __name__ == "__main__":
-    ana = StandingPhotoAnalyzer()
-    ana.analyze("1", "2")
+            cv2.imwrite(UPLOAD_DIR + '{}-result.jpg'.format(i), canvas)