BMI
BMI
import mediapipe as mp
import numpy as np
import math
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
if height == 0:
return 0
return bmi
def estimate_height(landmarks):
nose = landmarks[mp_pose.PoseLandmark.NOSE]
chin = landmarks[mp_pose.PoseLandmark.CHIN]
# Estimate height using the sum of distances as a proportion of the full body height
return estimated_height
# Function to estimate weight based on estimated height (heuristic model)
def estimate_weight(height):
# A very simple heuristic based on height (this can be improved with a more advanced model)
# Rough weight estimate using height in cm, e.g., height * a constant factor
weight = (height - 100) * 1.2 # Example: Weight increases with height (in kg)
return weight
# Video Capture
while cap.isOpened():
if not ret:
break
frame = cv2.flip(frame, 1)
results = pose.process(rgb_frame)
if results.pose_landmarks:
landmarks = results.pose_landmarks.landmark
height = estimate_height(landmarks)
weight = estimate_weight(height)
# Calculate BMI
break
cap.release()
cv2.destroyAllWindows()
1. Imports:
cv2: OpenCV is used for video processing and displaying the webcam feed.
mediapipe: MediaPipe is a framework for building multimodal applied ML pipelines. Here, it's used for
detecting body landmarks (pose detection).
numpy: This library is used for numerical calculations, though it's not heavily used in the code.
math: Used for basic mathematical operations, including the Euclidean distance calculation.
pose: An instance of the Pose model with a minimum detection confidence of 0.5 and tracking confidence of
0.5. This defines the minimum thresholds for detecting and tracking the body landmarks.
mp_drawing: This is used for drawing the detected landmarks on the frame.
3. Functions:
calculate_bmi(weight, height):
This function takes weight (in kilograms) and height (in centimeters) as input and returns the Body Mass
Index (BMI) using the formula: BMI=weight(height in meters)2\text{BMI} = \frac{\text{weight}}{(\text{height
in meters})^2}BMI=(height in meters)2weight
The height is first converted from centimeters to meters before performing the calculation.
euclidean_distance(p1, p2):
This function computes the Euclidean distance between two 3D points (p1 and p2) using the formula:
distance=
It's used to calculate the physical distances between body landmarks (e.g., from the nose to the chin or from
the chin to the chest).
estimate_height(landmarks):
This function estimates the height of the person based on the distances between key body landmarks.
o It uses the Nose landmark and approximates the chin using the MOUTH_LEFT landmark (since
MediaPipe doesn’t provide a direct CHIN landmark).
o The function calculates the Euclidean distances from the Nose to Chin and Chin to Chest (shoulder).
o These distances are added together and multiplied by an empirical scaling factor (*3) to estimate the
total height in centimeters.
estimate_weight(height):
This is a simple heuristic function to estimate the person's weight based on their height.
This is a rough estimation, assuming that the person's weight is proportional to their height (this could be
refined with a better model or dataset).
cv2.VideoCapture(0): This line opens the webcam for capturing video frames.
while cap.isOpened(): Starts an infinite loop that continuously processes the video frames.
o The cap.read() function reads the current frame from the webcam.
o frame = cv2.flip(frame, 1): This line flips the frame horizontally for better user experience (so that
the user sees a mirror image of themselves).
Landmarks Detection:
o If pose landmarks are detected in the frame (if results.pose_landmarks:), the detected landmarks
are drawn onto the frame using mp_drawing.draw_landmarks.
o The function estimate_height(landmarks) is called to calculate the height using the detected
landmarks.
o Then, estimate_weight(height) is called to estimate the weight based on the calculated height.
BMI Calculation:
5. Display Output:
The webcam feed with the drawn landmarks and the calculated BMI value is displayed in a window called
Live BMI Detection.
The cv2.waitKey(1) allows for a 1ms delay between frames, and the loop will break if the "q" key is pressed.
6. Cleanup:
After exiting the loop, the webcam feed is released using cap.release(), and all OpenCV windows are closed
using cv2.destroyAllWindows().