Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a body measurements application that can be used to measure the human body, but I'm having trouble matching the correct person size to the image size.
how can you help me ?

What I have tried:

Python
import cv2
import mediapipe as mp
import time
import math


class poseDetector():

    def __init__(self , static_image_mode=False , model_complexity=1 , smooth_landmarks=True ,
                 enable_segmentation=False ,
                 smooth_segmentation=True , min_detection_confidence=0.5 , min_tracking_confidence=0.5):

        self.static_image_mode = static_image_mode
        self.model_complexity = model_complexity
        self.smooth_landmarks = smooth_landmarks
        self.enable_segmentation = enable_segmentation
        self.smooth_segmentation = smooth_segmentation
        self.min_detection_confidence = min_detection_confidence
        self.min_tracking_confidence = min_tracking_confidence

        self.mpDraw = mp.solutions.drawing_utils
        self.mpPose = mp.solutions.pose
        self.pose = self.mpPose.Pose(self.static_image_mode, self.model_complexity, self.smooth_landmarks,
                                     self.enable_segmentation, self.smooth_segmentation,
                                     self.min_detection_confidence, self.min_tracking_confidence)

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.pose.process(imgRGB)
        if self.results.pose_landmarks:
            if draw:
                self.mpDraw.draw_landmarks(img, self.results.pose_landmarks,
                                           self.mpPose.POSE_CONNECTIONS)
        return img

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

    def findAngle(self, img, p1, p2, p3, draw=True):

        # Get the landmarks
        x1, y1 = self.lmList[p1][1:]
        x2, y2 = self.lmList[p2][1:]
        x3, y3 = self.lmList[p3][1:]

        # Calculate the Angle
        angle = math.degrees(math.atan2(y3 - y2, x3 - x2) -
                             math.atan2(y1 - y2, x1 - x2))
        if angle < 0:
            angle += 360

        # print(angle)

        # Draw
        if draw:
            cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 3)
            cv2.line(img, (x3, y3), (x2, y2), (255, 255, 255), 3)
            cv2.circle(img, (x1, y1), 10, (255, 0, 0), cv2.FILLED)
            cv2.circle(img, (x1, y1), 15, (255, 0, 0), 2)
            cv2.circle(img, (x2, y2), 10, (255, 0, 0), cv2.FILLED)
            cv2.circle(img, (x2, y2), 15, (255, 0, 0), 2)
            cv2.circle(img, (x3, y3), 10, (255, 0, 0), cv2.FILLED)
            cv2.circle(img, (x3, y3), 15, (255, 0, 0), 2)
            cv2.putText(img, str(int(angle)), (x2 - 50, y2 + 50),
                        cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
        return angle


def main():
    cap = cv2.VideoCapture('Saciidpct.jpg')
    pTime = 0
    detector = poseDetector()
    while True:
        success, img = cap.read()
        img = detector.findPose(img)
        lmList = detector.findPosition(img, draw=False)
        if len(lmList) != 0:
            # printing several landmarks for your choose
            print(lmList[11], lmList[23])
            print(lmList[12] , lmList[24])

            # Assigning landmarks to variables
            x1, y1 = lmList[11][1], lmList[11][2]
            x2, y2 = lmList[23][1], lmList[23][2]

            # coloring center of the line between these points
            cx1, cy1 = (x1 + x2) // 2, (y1 + y2) // 2

            # Assigning landmarks to variables
            x3 , y3 = lmList[12][1] , lmList[12][2]
            x4 , y4 = lmList[24][1] , lmList[24][2]

            # coloring center of the line between these points
            cx2, cy2 = (x3 + x4) // 2 , (y3 + y4) // 2

            # Coloring the heads of lines
            cv2.circle(img, (lmList[11][1], lmList[11][2]), 5, (255, 0, 0), cv2.FILLED)
            cv2.circle(img , (lmList[23][1] , lmList[23][2]) , 5 , (255 , 0 , 0) , cv2.FILLED)

            # Coloring the center and Drawing the lines
            cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
            cv2.circle(img, (cx1, cy1), 5, (255 , 0 , 0), cv2.FILLED)

            cv2.line(img , (x3 , y3) , (x4 , y4) , (255 , 0 , 255) , 3)
            cv2.circle(img , (cx2 , cy2) , 5 , (255 , 0 , 0) , cv2.FILLED)

            # length of the lines
            length1 = math.hypot(x2 - x1, y2 - y1)
            length2 = math.hypot(x4 - x3 , y4 - y3)
            print("Cabirka Bidixda: ", length1)
            print("Cabirka Midigta: " , length2)

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

        cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3,
                    (255, 0, 0), 3)

        cv2.imshow("Image", img)
        cv2.waitKey(0)


if __name__ == "__main__":
    main()
Posted
Updated 27-Jul-22 8:43am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900