|
| 1 | +import cv2 |
| 2 | +import mediapipe as mp |
| 3 | +import time |
| 4 | +import math |
| 5 | + |
| 6 | + |
| 7 | +class poseDetector(): |
| 8 | + |
| 9 | + def __init__(self, mode=False, upBody=False, smooth=True, |
| 10 | + detectionCon=0.5, trackCon=0.5): |
| 11 | + |
| 12 | + self.mode = mode |
| 13 | + self.upBody = upBody |
| 14 | + self.smooth = smooth |
| 15 | + self.detectionCon = detectionCon |
| 16 | + self.trackCon = trackCon |
| 17 | + |
| 18 | + self.mpDraw = mp.solutions.drawing_utils |
| 19 | + self.mpPose = mp.solutions.pose |
| 20 | + self.pose = self.mpPose.Pose(self.mode, self.upBody, self.smooth, |
| 21 | + self.detectionCon, self.trackCon) |
| 22 | + |
| 23 | + def findPose(self, img, draw=True): |
| 24 | + imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 25 | + self.results = self.pose.process(imgRGB) |
| 26 | + if self.results.pose_landmarks: |
| 27 | + if draw: |
| 28 | + self.mpDraw.draw_landmarks(img, self.results.pose_landmarks, |
| 29 | + self.mpPose.POSE_CONNECTIONS) |
| 30 | + return img |
| 31 | + |
| 32 | + def findPosition(self, img, draw=True): |
| 33 | + self.lmList = [] |
| 34 | + if self.results.pose_landmarks: |
| 35 | + for id, lm in enumerate(self.results.pose_landmarks.landmark): |
| 36 | + h, w, c = img.shape |
| 37 | + # print(id, lm) |
| 38 | + cx, cy = int(lm.x * w), int(lm.y * h) |
| 39 | + self.lmList.append([id, cx, cy]) |
| 40 | + if draw: |
| 41 | + cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED) |
| 42 | + return self.lmList |
| 43 | + |
| 44 | + def findAngle(self, img, p1, p2, p3, draw=True): |
| 45 | + |
| 46 | + # Get the landmarks |
| 47 | + x1, y1 = self.lmList[p1][1:] |
| 48 | + x2, y2 = self.lmList[p2][1:] |
| 49 | + x3, y3 = self.lmList[p3][1:] |
| 50 | + |
| 51 | + # Calculate the Angle |
| 52 | + angle = math.degrees(math.atan2(y3 - y2, x3 - x2) - |
| 53 | + math.atan2(y1 - y2, x1 - x2)) |
| 54 | + if angle < 0: |
| 55 | + angle += 360 |
| 56 | + |
| 57 | + # print(angle) |
| 58 | + |
| 59 | + # Draw |
| 60 | + if draw: |
| 61 | + cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 3) |
| 62 | + cv2.line(img, (x3, y3), (x2, y2), (255, 255, 255), 3) |
| 63 | + cv2.circle(img, (x1, y1), 10, (0, 0, 255), cv2.FILLED) |
| 64 | + cv2.circle(img, (x1, y1), 15, (0, 0, 255), 2) |
| 65 | + cv2.circle(img, (x2, y2), 10, (0, 0, 255), cv2.FILLED) |
| 66 | + cv2.circle(img, (x2, y2), 15, (0, 0, 255), 2) |
| 67 | + cv2.circle(img, (x3, y3), 10, (0, 0, 255), cv2.FILLED) |
| 68 | + cv2.circle(img, (x3, y3), 15, (0, 0, 255), 2) |
| 69 | + cv2.putText(img, str(int(angle)), (x2 - 50, y2 + 50), |
| 70 | + cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2) |
| 71 | + return angle |
| 72 | + |
| 73 | +def main(): |
| 74 | + cap = cv2.VideoCapture('PoseVideos/1.mp4') |
| 75 | + pTime = 0 |
| 76 | + detector = poseDetector() |
| 77 | + while True: |
| 78 | + success, img = cap.read() |
| 79 | + img = detector.findPose(img) |
| 80 | + lmList = detector.findPosition(img, draw=False) |
| 81 | + if len(lmList) != 0: |
| 82 | + print(lmList[14]) |
| 83 | + cv2.circle(img, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED) |
| 84 | + |
| 85 | + cTime = time.time() |
| 86 | + fps = 1 / (cTime - pTime) |
| 87 | + pTime = cTime |
| 88 | + |
| 89 | + cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, |
| 90 | + (255, 0, 0), 3) |
| 91 | + |
| 92 | + cv2.imshow("Image", img) |
| 93 | + cv2.waitKey(1) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments