mport cv2
import mediapipe as mp
import math

mp\_drawing = mp.solutions.drawing\_utils
mp\_pose = mp.solutions.pose

cap = cv2.VideoCapture(0)

with mp\_pose.Pose(min\_detection\_confidence=0.5, min\_tracking\_confidence=0.5) as pose:
 while True:
 ret, frame = cap.read()
 
 # Convert the image to RGB format
 image = cv2.cvtColor(frame, cv2.COLOR\_BGR2RGB)
 image.flags.writeable = False
 
 # Make a detection
 results = pose.process(image)
 
 # Convert the image back to BGR format
 image.flags.writeable = True
 image = cv2.cvtColor(image, cv2.COLOR\_RGB2BGR)
 
 if results.pose\_landmarks is not None:
 # Get the positions of the key points
 left\_hip = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_HIP]
 left\_knee = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_KNEE]
 left\_ankle = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_ANKLE]
 left\_shoulder = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_SHOULDER]
 left\_elbow = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_ELBOW]
 left\_wrist = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_WRIST]
 right\_hip = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_HIP]
 right\_knee = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_KNEE]
 right\_ankle = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_ANKLE]
 right\_shoulder = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_SHOULDER]
 right\_elbow = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_ELBOW]
 right\_wrist = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_WRIST]
 
 # Calculate the distance between the hips
 hip\_distance = math.sqrt((right\_hip.x - left\_hip.x) \*\* 2 + (right\_hip.y - left\_hip.y) \*\* 2)
 
 # Calculate the angle between the torso and left thigh
 torso\_angle = math.degrees(
 math.atan2(left\_shoulder.y - left\_hip.y, left\_shoulder.x - left\_hip.x) -
 math.atan2(left\_knee.y - left\_hip.y, left\_knee.x - left\_hip.x))
 
 # Calculate the angle of the knee joint
 knee\_angle = math.degrees(
 math.atan2(left\_ankle.y - left\_knee.y, left\_ankle.x - left\_knee.x) -
 math.atan2(left\_knee.y - left\_hip.y, left\_knee.x - left\_hip.x))
 
 # Calculate the angle between the torso and right thigh
 torso\_angle\_r = math.degrees(
 math.atan2(right\_shoulder.y - right\_hip.y, right\_shoulder.x - right\_hip.x) -
 math.atan2(right\_knee.y - right\_hip.y, right\_knee.x - right\_hip.x))
 
 # Calculate the angle between the right thigh and shin
 thigh\_angle\_r = math.degrees(
 math.atan2(right\_

give remaining code
thigh.y - right\_hip.y, right\_thigh.x - right\_hip.x) -
math.atan2(right\_ankle.y - right\_knee.y, right\_ankle.x - right\_knee.x))

scss
Copy code
 # Calculate the angle between the left thigh and shin
 thigh\_angle\_l = math.degrees(
 math.atan2(left\_thigh.y - left\_hip.y, left\_thigh.x - left\_hip.x) -
 math.atan2(left\_ankle.y - left\_knee.y, left\_ankle.x - left\_knee.x))
 
 # Calculate the angle between the left thigh and shin
 thigh\_angle\_r = math.degrees(
 math.atan2(right\_thigh.y - right\_hip.y, right\_thigh.x - right\_hip.x) -
 math.atan2(right\_ankle.y - right\_knee.y, right\_ankle.x - right\_knee.x))
 
 # Calculate the angle of the elbow joint
 elbow\_angle = math.degrees(
 math.atan2(left\_wrist.y - left\_elbow.y, left\_wrist.x - left\_elbow.x) -
 math.atan2(left\_elbow.y - left\_shoulder.y, left\_elbow.x - left\_shoulder.x))
 
 # Calculate the angle of the elbow joint
 elbow\_angle\_r = math.degrees(
 math.atan2(right\_wrist.y - right\_elbow.y, right\_wrist.x - right\_elbow.x) -
 math.atan2(right\_elbow.y - right\_shoulder.y, right\_elbow.x - right\_shoulder.x))
 
 # Display the angle measurements
 cv2.putText(image, str(round(hip\_distance, 2)), 
 (50, 50), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(torso\_angle, 2)), 
 (50, 100), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(knee\_angle, 2)), 
 (50, 150), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(torso\_angle\_r, 2)), 
 (50, 200), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(thigh\_angle\_r, 2)), 
 (50, 250), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(thigh\_angle\_l, 2)), 
 (50, 300), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(elbow\_angle, 2)), 
 (50, 350), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(elbow\_angle\_r, 2)), 
 (50, 400), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)

 # Visualize the poses
 mp\_drawing.draw\_landmarks(image, results.pose\_landmarks, mp\_pose.P

give the remaining code
포트 CV2
메디아파이프를 mp로 임포트
수학 가져오기

mp\_drawing = mp.solutions.drawing\_utils
mp\_pose = mp.solutions.pose

cap = cv2.VideoCapture(0)

mp\_pose.Pose(min\_detection\_confidence=0.5, min\_tracking\_confidence=0.5)를 pose로 설정합니다:
 while True:
 ret, frame = cap.read()
 
 # 이미지를 RGB 형식으로 변환
 image = cv2.cvtColor(frame, cv2.COLOR\_BGR2RGB)
 image.flags.writeable = False
 
 # 감지 만들기
 results = pose.process(image)
 
 # 이미지를 BGR 형식으로 다시 변환
 image.flags.writeable = True
 image = cv2.cvtColor(image, cv2.COLOR\_RGB2BGR)
 
 results.pose\_landmarks가 None이 아닌 경우:
 # 주요 포인트의 위치 가져오기
 left\_hip = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_HIP]
 왼쪽\_무릎 = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_KNEE]
 left\_ankle = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_ANKLE]
 left\_shoulder = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_SHOULDER]
 left\_elbow = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_ELBOW]
 left\_wrist = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.LEFT\_WRIST]
 right\_hip = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_HIP]
 right\_knee = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_KNEE]
 오른쪽\_앵클 = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_ANKLE]
 right\_shoulder = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_SHOULDER]
 right\_elbow = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_ELBOW]
 right\_wrist = results.pose\_landmarks.landmark[mp\_pose.PoseLandmark.RIGHT\_WRIST]
 
 # 엉덩이 사이의 거리를 계산합니다.
 hip\_distance = math.sqrt((오른쪽\_hip.x - 왼쪽\_hip.x) \*\* 2 + (오른쪽\_hip.y - 왼쪽\_hip.y) \*\* 2)
 
 # 몸통과 왼쪽 허벅지 사이의 각도를 계산합니다.
 몸통\_angle = math.degrees(
 math.atan2(왼쪽\_어깨.y - 왼쪽\_엉덩이.y, 왼쪽\_어깨.x - 왼쪽\_엉덩이.x) -
 math.atan2(left\_knee.y - left\_hip.y, left\_knee.x - left\_hip.x))
 
 # 무릎 관절의 각도를 계산합니다.
 무릎\_각도 = math.degrees(
 math.atan2(left\_ankle.y - left\_knee.y, left\_ankle.x - left\_knee.x) -
 math.atan2(left\_knee.y - left\_hip.y, left\_knee.x - left\_hip.x))
 
 # 몸통과 오른쪽 허벅지 사이의 각도를 계산합니다.
 몸통\_각도\_r = math.degrees(
 math.atan2(오른쪽\_어깨.y - 오른쪽\_엉덩이.y, 오른쪽\_어깨.x - 오른쪽\_엉덩이.x) -
 math.atan2(right\_knee.y - right\_hip.y, right\_knee.x - right\_hip.x))
 
 # 오른쪽 허벅지와 정강이 사이의 각도를 계산합니다.
 thigh\_angle\_r = math.degrees(
 math.atan2(right\_

나머지 코드 제공
thigh.y - 오른쪽\_허벅지.y, 오른쪽\_허벅지.x - 오른쪽\_허벅지.x) - -
math.atan2(오른쪽\_앵클.y - 오른쪽\_무릎.y, 오른쪽\_앵클.x - 오른쪽\_무릎.x))

scss
코드 복사
 # 왼쪽 허벅지와 정강이 사이의 각도를 계산합니다.
 허벅지\_각도\_l = math.degrees(
 math.atan2(왼쪽\_허벅지.y - 왼쪽\_엉덩이.y, 왼쪽\_허벅지.x - 왼쪽\_엉덩이.x) -
 math.atan2(left\_ankle.y - left\_knee.y, left\_ankle.x - left\_knee.x))
 
 # 왼쪽 허벅지와 정강이 사이의 각도를 계산합니다.
 허벅지\_각도\_r = math.degrees(
 math.atan2(오른쪽\_허벅지.y - 오른쪽\_엉덩이.y, 오른쪽\_허벅지.x - 오른쪽\_엉덩이.x) -
 math.atan2(right\_ankle.y - right\_knee.y, right\_ankle.x - right\_knee.x))
 
 # 팔꿈치 관절의 각도를 계산합니다.
 elbow\_angle = math.degrees(
 math.atan2(왼쪽\_손목.y - 왼쪽\_팔꿈치.y, 왼쪽\_손목.x - 왼쪽\_팔꿈치.x) -
 math.atan2(left\_elbow.y - left\_shoulder.y, left\_elbow.x - left\_shoulder.x))
 
 # 팔꿈치 관절의 각도를 계산합니다.
 elbow\_angle\_r = math.degrees(
 math.atan2(오른쪽\_손목.y - 오른쪽\_팔꿈치.y, 오른쪽\_손목.x - 오른쪽\_팔꿈치.x) -
 math.atan2(right\_elbow.y - right\_shoulder.y, right\_elbow.x - right\_shoulder.x))
 
 # 각도 측정값 표시
 cv2.putText(image, str(round(hip\_distance, 2)), 
 (50, 50), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(torso\_angle, 2)), 
 (50, 100), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(knee\_angle, 2)), 
 (50, 150), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(torso\_angle\_r, 2)), 
 (50, 200), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(thigh\_angle\_r, 2)), 
 (50, 250), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(thigh\_angle\_l, 2)), 
 (50, 300), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(elbow\_angle, 2)), 
 (50, 350), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)
 cv2.putText(image, str(round(elbow\_angle\_r, 2)), 
 (50, 400), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE\_AA)

 # 포즈 시각화하기
 mp\_drawing.draw\_landmarks(image, results.pose\_landmarks, mp\_pose.P

나머지 코드를 제공하세요.