1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| import cv2 import mediapipe as mp import matplotlib.pyplot as plt import numpy as np
from Function.Show import look_image
def image(path):
mp_pose = mp.solutions.pose mp_drawing = mp.solutions.drawing_utils
pose = mp_pose.Pose(static_image_mode=True, model_complexity=2, smooth_landmarks=True, enable_segmentation=True, min_detection_confidence=0.5, min_tracking_confidence=0.5)
img = cv2.imread(path) print('original') print('-----') look_image(img)
img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) results = pose.process(img_RGB)
mp_drawing.draw_landmarks(img, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) print('result') print('-----') look_image(img)
print('world') print('-----') mp_drawing.plot_landmarks(results.pose_world_landmarks, mp_pose.POSE_CONNECTIONS)
mask = results.segmentation_mask print(mask.shape) print(img.shape) mask = mask > 0.5
print('-----') print('mask') print('-----') plt.imshow(mask) plt.show()
mask_3 = np.stack((mask, mask, mask), axis=-1) MASK_COLOR = [0, 200, 0] fg_image = np.zeros(img.shape, dtype=np.uint8) fg_image[:] = MASK_COLOR FG_img = np.where(mask_3, img, fg_image) BG_img = np.where(~mask_3, img, fg_image)
print('forward') print('-----') look_image(FG_img)
print('back') print('-----') look_image(BG_img)
print(results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW])
h = img.shape[0] w = img.shape[1]
print(results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW].x * w) print(results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW].y * h)
cx = int(results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_KNEE].x * w) cy = int(results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_KNEE].y * h) cz = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_KNEE].z
img = cv2.circle(img, (cx, cy), 15, (255, 0, 0), -1) print('-----') print('knee') print('-----') look_image(img)
for i in range(33): cx = int(results.pose_landmarks.landmark[i].x * w) cy = int(results.pose_landmarks.landmark[i].y * h) cz = results.pose_landmarks.landmark[i].z
radius = 10
if i == 0: img = cv2.circle(img, (cx, cy), radius, (0, 0, 255), -1) elif i in [11, 12]: img = cv2.circle(img, (cx, cy), radius, (223, 155, 6), -1) elif i in [23, 24]: img = cv2.circle(img, (cx, cy), radius, (1, 240, 255), -1) elif i in [13, 14]: img = cv2.circle(img, (cx, cy), radius, (140, 47, 240), -1) elif i in [25, 26]: img = cv2.circle(img, (cx, cy), radius, (0, 0, 255), -1) elif i in [15, 16, 27, 28]: img = cv2.circle(img, (cx, cy), radius, (223, 155, 60), -1) elif i in [17, 19, 21]: img = cv2.circle(img, (cx, cy), radius, (94, 218, 121), -1) elif i in [18, 20, 22]: img = cv2.circle(img, (cx, cy), radius, (16, 144, 247), -1) elif i in [27, 29, 31]: img = cv2.circle(img, (cx, cy), radius, (29, 123, 243), -1) elif i in [28, 30, 32]: img = cv2.circle(img, (cx, cy), radius, (193, 182, 255), -1) elif i in [9, 10]: img = cv2.circle(img, (cx, cy), radius, (205, 235, 255), -1) elif i in [1, 2, 3, 4, 5, 6, 7, 8]: img = cv2.circle(img, (cx, cy), radius, (94, 218, 121), -1) else: img = cv2.circle(img, (cx, cy), radius, (0, 255, 0), -1)
print('all') look_image(img)
|