|
|
import cv2 |
|
|
import mediapipe as mp |
|
|
import numpy as np |
|
|
import math |
|
|
import os |
|
|
from ultralytics import YOLO |
|
|
|
|
|
class BodyMeasurement: |
|
|
def __init__(self, model_path="yolo11s-seg.pt"): |
|
|
self.model = YOLO(model_path) |
|
|
self.pose = mp.solutions.pose |
|
|
|
|
|
|
|
|
def segment(self, img): |
|
|
results = self.model(img, verbose=False) |
|
|
r = results[0] |
|
|
|
|
|
if r.masks is None: |
|
|
print("❌ No person detected!") |
|
|
return img |
|
|
|
|
|
|
|
|
mask = r.masks.data[0].cpu().numpy() |
|
|
mask = cv2.resize(mask, (img.shape[1], img.shape[0])) |
|
|
mask = (mask * 255).astype("uint8") |
|
|
|
|
|
|
|
|
mask3 = cv2.merge([mask, mask, mask]) |
|
|
segmented = cv2.bitwise_and(img, mask3) |
|
|
|
|
|
|
|
|
contours, _ = cv2.findContours( |
|
|
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
|
|
) |
|
|
|
|
|
|
|
|
cv2.drawContours( |
|
|
segmented, |
|
|
contours, |
|
|
-1, |
|
|
(0, 255, 0), |
|
|
3 |
|
|
) |
|
|
|
|
|
return segmented |
|
|
|
|
|
|
|
|
|
|
|
def edges(self, rgb): |
|
|
gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) |
|
|
gray = cv2.GaussianBlur(gray, (5,5), 0) |
|
|
edges = cv2.Canny(gray,50,150) |
|
|
return cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) |
|
|
|
|
|
|
|
|
def keypoints(self,rgb): |
|
|
h,w,_ = rgb.shape |
|
|
pts = {} |
|
|
with self.pose.Pose(static_image_mode=True) as p: |
|
|
res = p.process(rgb) |
|
|
if not res.pose_landmarks: return pts |
|
|
for i,lm in enumerate(res.pose_landmarks.landmark): |
|
|
x,y = int(lm.x*w), int(lm.y*h) |
|
|
pts[self.pose.PoseLandmark(i).name] = (x,y) |
|
|
return pts |
|
|
|
|
|
|
|
|
def dist(self,a,b): return math.dist(a,b) |
|
|
|
|
|
def measure(self,pts,img_h,height_cm=None): |
|
|
M={}; G=lambda k: pts.get(k) |
|
|
LS,RS = G("LEFT_SHOULDER"), G("RIGHT_SHOULDER") |
|
|
LH,RH = G("LEFT_HIP"), G("RIGHT_HIP") |
|
|
LA,RA = G("LEFT_ANKLE"), G("RIGHT_ANKLE") |
|
|
nose = G("NOSE") |
|
|
|
|
|
if LS and RS: M["shoulder_px"] = self.dist(LS,RS) |
|
|
if LH and RH: M["hip_px"] = self.dist(LH,RH) |
|
|
|
|
|
if nose and LA and RA: |
|
|
mid=((LA[0]+RA[0])//2,(LA[1]+RA[1])//2) |
|
|
height_px=self.dist(nose,mid) |
|
|
else: |
|
|
height_px=img_h |
|
|
|
|
|
M["height_px"]=height_px |
|
|
|
|
|
if height_cm: |
|
|
scale=height_cm/height_px |
|
|
M["scale_cm/px"]=scale |
|
|
if "shoulder_px" in M: M["shoulder_cm"]=M["shoulder_px"] * scale |
|
|
if "hip_px" in M: M["hip_cm"]=M["hip_px"] * scale |
|
|
|
|
|
return M |
|
|
|
|
|
|
|
|
def run(self, img_path, height_cm=None, output_folder="results"): |
|
|
if not os.path.exists(output_folder): |
|
|
os.makedirs(output_folder) |
|
|
|
|
|
img=cv2.imread(img_path) |
|
|
if img is None: |
|
|
raise FileNotFoundError("Image not found!") |
|
|
|
|
|
seg=self.segment(img) |
|
|
rgb=cv2.cvtColor(seg, cv2.COLOR_BGR2RGB) |
|
|
edge=self.edges(rgb) |
|
|
pts=self.keypoints(rgb) |
|
|
M=self.measure(pts, rgb.shape[0], height_cm) |
|
|
|
|
|
overlay=cv2.addWeighted(rgb,0.7,edge,0.3,0) |
|
|
for x,y in pts.values(): |
|
|
cv2.circle(overlay,(x,y),3,(0,255,0),-1) |
|
|
|
|
|
cv2.imwrite(f"{output_folder}/segmented.png", seg) |
|
|
cv2.imwrite(f"{output_folder}/edges.png", cv2.cvtColor(edge, cv2.COLOR_RGB2BGR)) |
|
|
cv2.imwrite(f"{output_folder}/overlay.png", cv2.cvtColor(overlay, cv2.COLOR_RGB2BGR)) |
|
|
|
|
|
print("\n Output Saved In Folder:", output_folder) |
|
|
print("Files:") |
|
|
print(" - segmented.png\n - edges.png\n - overlay.png") |
|
|
|
|
|
print("\n Measurements:") |
|
|
for k,v in M.items(): print(k,":",round(v,2)) |
|
|
|
|
|
return M |
|
|
|
|
|
|
|
|
|
|
|
if __name__=="__main__": |
|
|
img_path = "/home/abhishek/Desktop/body_measurement /body_measurement/app/images/unnamed.jpg" |
|
|
height_cm = 180 |
|
|
output_folder = "Image_output" |
|
|
|
|
|
BodyMeasurement().run(img_path, height_cm, output_folder) |
|
|
|