-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediapipe_tracking.py
52 lines (42 loc) · 1.75 KB
/
mediapipe_tracking.py
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
#import packages
import cv2
import mediapipe as mp
import numpy as np
# initialize mediapipe pose solution
mp_pose = mp.solutions.pose
mp_draw = mp.solutions.drawing_utils
pose = mp_pose.Pose()
video_path = "/home/rakshit/Downloads/3_08_29_00.mp4"
# take video input for pose detection
# you can put here video of your choice
cap = cv2.VideoCapture(0)
# take live camera input for pose detection
# cap = cv2.VideoCapture(0)
# read each frame/image from capture object
while True:
ret, img = cap.read()
# resize image/frame so we can accommodate it on our screen
img = cv2.resize(img, (600, 400))
# do Pose detection
results = pose.process(img)
# draw the detected pose on original video/ live stream
mp_draw.draw_landmarks(img, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_draw.DrawingSpec((255, 0, 0), 2, 2),
mp_draw.DrawingSpec((255, 0, 255), 2, 2)
)
# Display pose on original video/live stream
cv2.imshow("Pose Estimation", img)
# Extract and draw pose on plain white image
h, w, c = img.shape # get shape of original frame
opImg = np.zeros([h, w, c]) # create blank image with original frame size
opImg.fill(255) # set white background. put 0 if you want to make it black
# draw extracted pose on black white image
mp_draw.draw_landmarks(opImg, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_draw.DrawingSpec((255, 0, 0), 2, 2),
mp_draw.DrawingSpec((255, 0, 255), 2, 2)
)
# display extracted pose on blank images
cv2.imshow("Extracted Pose", opImg)
# print all landmarks
print(results.pose_landmarks)
cv2.waitKey(1)