Course notes: Pose estimation

Preamble

Notes from Latest Pose Estimation Realtime (24 FPS) using CPU | Computer Vision | OpenCV Python 2021

Related video courses

See also

Course notes: Personal AI trainer

Courses

Required files

Required packages

  • opencv-python
  • mediapipe

Course Notes

New project: PoseEstimationProject

New file: PoseEstimationMin.py

Template

 

 

Create model

7:11

  • static_image_mode=True = always detect (heavy on CPU)
  • False, => detect and when found then moves to tracking (it doesn’t keep on detecting)
  • upper_bosy_only 33 or 25 landmarks
  • smooth_landmarks
  • min_detection_confidence
  • min_tracking_confidence

 

Skip for simplicity

mpPose = mp.solutions.pose
pose = mpPose.pose

while True:

    success, image = cap.read()
    imageRGB = cv2.cvtcolor(image, cv2.COLOUR_BGR2RGB)
    results = pose.process(imageRGB)

Print the results

print(results)  # just shows a class
print(results.pose_landmarks)

results is just a class.

.pose_landmarks shows x and y and the visibility as well

Drawing

add variable

mpDraw = mp.solutions.draw_utils

then in loop

if results.pose_landmarks:
    # mpDraw.draw_landmarks(image, results.pose_landmarks)
    mpDraw.draw_landmarks(image, results.pose_landmarks, mp.POSE_CONNECTIONS)

How to know which landmark is which

13:30

There is no id for the landmarks – we must enumerate and put in a list

From http://google.github.io/mediapipe/solutions/pose.html

 

for id, lm in enumerate(results.pose_landmarks.landmark):
    print(id, lm)

To get actual pixel value

cx, cy = lm.x*iW, lm.y*iH

draw circles to check

cv2.circle(image, (cx, cy), 10, (255, 0, 0), cv2.FILLED)

 

Make a module

19:30

PoseModule.py

 

main()

def main():
    camW, camH = 640, 480

    cap = cv2.VideoCapture('PoseVideos/2.mp4')
    cap.set(3, camW)
    cap.set(4, camH)
    pTime = 0

    while true:
        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime

        cv2.putText(image, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2)

        cv2.imshow('Image', image)
        cv2.waitKey(1)


if __name__ == "__main__":
    main()

init()

class poseDetector:
    def __init__(self, mode=False, upper_body=False, smooth=True, detectionCon=0.5, trackingCon=0.5):
        self.mode = mode
        self.upper_body = upper_body
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackingCon = trackingCon

        self.mpPose = mp.solutions.pose
        self.pose = self.mpPose.Pose(self.mode, self.upper_body, self.smooth, self.detectionCon, self.trackingCon)
        self.mpDraw = mp.solutions.draw_utils

findPose()

def findPose(self, image, draw=True):

    imageRGB = cv2.cvtcolor(image, cv2.COLOUR_BGR2RGB)
    results = self.pose.process(imageRGB)

    # print(results)  # just shows a class
    # print(results.pose_landmarks)
    if results.pose_landmarks:
        if draw:
            # mpDraw.draw_landmarks(image, results.pose_landmarks)
            self.mpDraw.draw_landmarks(image, results.pose_landmarks, self.mp.POSE_CONNECTIONS)
    return(image)

Don’t forget to return the image!

Finish the main()

28:12

Add the detector:

def main():
    camW, camH = 640, 480

    cap = cv2.VideoCapture('PoseVideos/2.mp4')
    cap.set(3, camW)
    cap.set(4, camH)
    pTime = 0
    detector = poseDetector()

    while True:
        success, image = cap.read()
        image = detector.findPose(image)

        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime

        cv2.putText(image, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2)

        cv2.imshow('Image', image)
        cv2.waitKey(1)

Find the points

29:45

We need

iW, iH, iC = image.shape

Add a conditional for if there are any results

Add a list for the landmarks

def findPosition(self, image, draw=True):
    iW, iH, iC = image.shape
    lmList = []
    if self.results.pose_landmarks:
        for id, lm in enumerate(self.results.pose_landmarks.landmark):
            # print(id, lm)
            cx, cy = int(lm.x*iW), int(lm.y*iH)
            lmList.append([id, cx, cy])
            if draw:
                cv2.circle(image, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
    return lmList, image

Return both lmList and the image

Update main()

def main():
    camW, camH = 640, 480

    cap = cv2.VideoCapture('PoseVideos/2.mp4')
    cap.set(3, camW)
    cap.set(4, camH)
    pTime = 0
    detector = poseDetector()

    while True:
        success, image = cap.read()
        image = detector.findPose(image)
        lmList = detector.findPosition(image)
        print(lmList)

        # You can track any particular landmark
        print(lmList[14])
        cv2.circle(image, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED)

        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime

        cv2.putText(image, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2)

        cv2.imshow('Image', image)
        cv2.waitKey(1)

 

Using the module

35:21

Create OurAwesomePoseProject.py

Paste in the code from main() and add

import cv2
import time
import PoseModule as pm

and change

detector = pm.poseDetector()

 

Need to add conditional in case there are no detections (ensure that this is in main()in the module as well)

if len(lmList) != 0:
    # You can track any particular landmark
    print(lmList[14])
    cv2.circle(image, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED)

in main()

def main():
    camW, camH = 640, 480

    cap = cv2.VideoCapture('PoseVideos/2.mp4')
    cap.set(3, camW)
    cap.set(4, camH)
    pTime = 0
    detector = poseDetector()

    while True:
        success, image = cap.read()
        image = detector.findPose(image)
        lmList = detector.findPosition(image)
        print(lmList)
        if len(lmList) != 0:
            # You can track any particular landmark
            print(lmList[14])
            cv2.circle(image, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED)

        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime

        cv2.putText(image, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2)

        cv2.imshow('Image', image)
        cv2.waitKey(1)

 

This is the end, my friend

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s