visualizer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. from PIL import ImageDraw
  16. from pycocotools.coco import COCO
  17. def draw_keypoint(image, coco_info: COCO, img_id):
  18. """
  19. Draw keypoints on image for the COCO human keypoint dataset with 17 keypoints.
  20. Args:
  21. image: PIL.Image object
  22. coco_info: COCO object (from pycocotools.coco)
  23. img_id: Image ID
  24. Returns:
  25. image: PIL.Image object with keypoints and skeleton drawn
  26. """
  27. # Initialize the drawing context
  28. image = image.convert("RGB")
  29. draw = ImageDraw.Draw(image)
  30. image_size = image.size
  31. width = int(max(image_size) * 0.005) # Line thickness for drawing
  32. # Define the skeleton connections based on COCO keypoint indexes
  33. skeleton = [
  34. [15, 13],
  35. [13, 11],
  36. [16, 14],
  37. [14, 12],
  38. [11, 12],
  39. [5, 11],
  40. [6, 12],
  41. [5, 6],
  42. [5, 7],
  43. [6, 8],
  44. [7, 9],
  45. [8, 10],
  46. [1, 2],
  47. [0, 1],
  48. [0, 2],
  49. [1, 3],
  50. [2, 4],
  51. [3, 5],
  52. [4, 6],
  53. ]
  54. # Define colors for each keypoint (you can customize these colors)
  55. keypoint_colors = [
  56. (255, 0, 0), # Nose
  57. (255, 85, 0), # Left Eye
  58. (255, 170, 0), # Right Eye
  59. (255, 255, 0), # Left Ear
  60. (170, 255, 0), # Right Ear
  61. (85, 255, 0), # Left Shoulder
  62. (0, 255, 0), # Right Shoulder
  63. (0, 255, 85), # Left Elbow
  64. (0, 255, 170), # Right Elbow
  65. (0, 255, 255), # Left Wrist
  66. (0, 170, 255), # Right Wrist
  67. (0, 85, 255), # Left Hip
  68. (0, 0, 255), # Right Hip
  69. (85, 0, 255), # Left Knee
  70. (170, 0, 255), # Right Knee
  71. (255, 0, 255), # Left Ankle
  72. (255, 0, 170), # Right Ankle
  73. ]
  74. # Get annotations for the image
  75. annotations = coco_info.loadAnns(coco_info.getAnnIds(imgIds=img_id))
  76. # Loop over each person annotation
  77. for ann in annotations:
  78. keypoints = ann.get("keypoints", [])
  79. if not keypoints:
  80. continue # Skip if no keypoints are present
  81. # Reshape keypoints into (num_keypoints, 3)
  82. keypoints = np.array(keypoints).reshape(-1, 3)
  83. # Draw keypoints
  84. for idx, (x, y, v) in enumerate(keypoints):
  85. if v == 2: # v=2 means the keypoint is labeled and visible
  86. radius = max(1, int(width / 2))
  87. x, y = float(x), float(y)
  88. color = keypoint_colors[idx % len(keypoint_colors)]
  89. draw.ellipse(
  90. (x - radius, y - radius, x + radius, y + radius), fill=color
  91. )
  92. # Draw skeleton by connecting keypoints
  93. for sk in skeleton:
  94. kp1_idx, kp2_idx = sk[0], sk[1]
  95. x1, y1, v1 = keypoints[kp1_idx]
  96. x2, y2, v2 = keypoints[kp2_idx]
  97. if v1 == 2 and v2 == 2:
  98. # Both keypoints are visible
  99. x1, y1 = float(x1), float(y1)
  100. x2, y2 = float(x2), float(y2)
  101. draw.line(
  102. (x1, y1, x2, y2),
  103. fill=(0, 255, 0), # Line color (you can customize)
  104. width=width,
  105. )
  106. return image