photo_analyzer.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import copy
  2. from typing import Callable
  3. import cv2
  4. from config import UPLOAD_DIR, FILE_URL
  5. from service.ai.photo.standing_photo import *
  6. from service.ai.util.torch_openpose import torch_openpose
  7. from service.ai.util.util import draw_bodypose
  8. class BasePhotoAnalyzer:
  9. def __init__(self):
  10. pass
  11. class StandingPhotoAnalyzer(BasePhotoAnalyzer):
  12. def __init__(self, data: dict):
  13. super().__init__()
  14. assert len(data) == 3, "Invalid arguments: expect 2 photo uuids and height."
  15. self.front = data['front']
  16. self.right = data['right']
  17. self.height = data['height']
  18. def analyze(self, callback: Callable) -> None:
  19. try:
  20. callback("RUNNING", progress=0)
  21. photos = []
  22. npys = []
  23. for index, i in enumerate((self.front, self.right)):
  24. test_image = UPLOAD_DIR + '{}.jpg'.format(i)
  25. ori_img = cv2.imread(test_image) # B,G,R order
  26. tp = torch_openpose('body_25')
  27. poses = tp(ori_img)
  28. if index == 0:
  29. npys.append(analyse_npy_front(poses, self.height))
  30. else:
  31. npys.append(analyse_npy_side(poses, self.height))
  32. canvas = copy.deepcopy(ori_img)
  33. canvas = draw_bodypose(canvas, poses, 'body_25')
  34. cv2.imwrite(UPLOAD_DIR + 'result/{}.result.jpg'.format(i), canvas)
  35. photos.append(FILE_URL + '{}.result.jpg'.format(i))
  36. callback("FINISHED", result={"data": {"front": {"file": photos[0]}, "right": {"file": photos[1]}},
  37. "advice": {"front": npys[0], "right": npys[1], "points": poses}}, )
  38. except Exception as e:
  39. callback("ERROR", error=e)
  40. raise e