You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_body_2d_keypoints.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import os.path as osp
  4. import pdb
  5. import unittest
  6. import cv2
  7. import numpy as np
  8. import torch
  9. from modelscope.outputs import OutputKeys
  10. from modelscope.pipelines import pipeline
  11. from modelscope.pipelines.base import Pipeline
  12. from modelscope.utils.constant import Tasks
  13. from modelscope.utils.test_utils import test_level
  14. lst_parent_ids_17 = [0, 0, 0, 1, 2, 0, 0, 5, 6, 7, 8, 5, 6, 11, 12, 13, 14]
  15. lst_left_ids_17 = [1, 3, 5, 7, 9, 11, 13, 15]
  16. lst_right_ids_17 = [2, 4, 6, 8, 10, 12, 14, 16]
  17. lst_spine_ids_17 = [0]
  18. lst_parent_ids_15 = [0, 0, 1, 2, 3, 1, 5, 6, 14, 8, 9, 14, 11, 12, 1]
  19. lst_left_ids_15 = [2, 3, 4, 8, 9, 10]
  20. lst_right_ids_15 = [5, 6, 7, 11, 12, 13]
  21. lst_spine_ids_15 = [0, 1, 14]
  22. def draw_joints(image, np_kps, score, threshold=0.2):
  23. if np_kps.shape[0] == 17:
  24. lst_parent_ids = lst_parent_ids_17
  25. lst_left_ids = lst_left_ids_17
  26. lst_right_ids = lst_right_ids_17
  27. elif np_kps.shape[0] == 15:
  28. lst_parent_ids = lst_parent_ids_15
  29. lst_left_ids = lst_left_ids_15
  30. lst_right_ids = lst_right_ids_15
  31. for i in range(len(lst_parent_ids)):
  32. pid = lst_parent_ids[i]
  33. if i == pid:
  34. continue
  35. if (score[i] < threshold or score[1] < threshold):
  36. continue
  37. if i in lst_left_ids and pid in lst_left_ids:
  38. color = (0, 255, 0)
  39. elif i in lst_right_ids and pid in lst_right_ids:
  40. color = (255, 0, 0)
  41. else:
  42. color = (0, 255, 255)
  43. cv2.line(image, (int(np_kps[i, 0]), int(np_kps[i, 1])),
  44. (int(np_kps[pid][0]), int(np_kps[pid, 1])), color, 3)
  45. for i in range(np_kps.shape[0]):
  46. if score[i] < threshold:
  47. continue
  48. cv2.circle(image, (int(np_kps[i, 0]), int(np_kps[i, 1])), 5,
  49. (0, 0, 255), -1)
  50. def draw_box(image, box):
  51. cv2.rectangle(image, (int(box[0][0]), int(box[0][1])),
  52. (int(box[1][0]), int(box[1][1])), (0, 0, 255), 2)
  53. class Body2DKeypointsTest(unittest.TestCase):
  54. def setUp(self) -> None:
  55. self.model_id = 'damo/cv_hrnetv2w32_body-2d-keypoints_image'
  56. self.test_image = 'data/test/images/keypoints_detect/000000438862.jpg'
  57. self.human_detect_model_id = 'damo/cv_resnet18_human-detection'
  58. def pipeline_inference(self, pipeline: Pipeline):
  59. output = pipeline(self.test_image)
  60. poses = np.array(output[OutputKeys.POSES])
  61. scores = np.array(output[OutputKeys.SCORES])
  62. boxes = np.array(output[OutputKeys.BOXES])
  63. assert len(poses) == len(scores) and len(poses) == len(boxes)
  64. image = cv2.imread(self.test_image, -1)
  65. for i in range(len(poses)):
  66. draw_box(image, np.array(boxes[i]))
  67. draw_joints(image, np.array(poses[i]), np.array(scores[i]))
  68. cv2.imwrite('pose_keypoint.jpg', image)
  69. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  70. def test_run_modelhub(self):
  71. human_detector = pipeline(
  72. Tasks.human_detection, model=self.human_detect_model_id)
  73. body_2d_keypoints = pipeline(
  74. Tasks.body_2d_keypoints,
  75. human_detector=human_detector,
  76. model=self.model_id)
  77. self.pipeline_inference(body_2d_keypoints)
  78. if __name__ == '__main__':
  79. unittest.main()