Commit
·
2038252
1
Parent(s):
58a97b6
My commit
Browse files- Image_output/edges.png +3 -0
- Image_output/overlay.png +3 -0
- Image_output/segmented.png +3 -0
- README.md +27 -0
- app/.env +0 -0
- app/AI_Scripts/body_segment.py +117 -0
- app/images/Media(3).jpeg +3 -0
- app/images/Media(4).jpeg +3 -0
- app/images/baby.jpeg +3 -0
- app/images/baby1.png +3 -0
- app/images/baby2.jpeg +3 -0
- app/images/baby3.jpeg +3 -0
- app/images/canny.bmp +3 -0
- app/images/cnn_diagram.png +3 -0
- app/images/output_image.jpg +3 -0
- app/images/shared image(2).jpeg +3 -0
- app/images/shared image(3).jpeg +3 -0
- app/images/shared image(4).jpeg +3 -0
- app/images/unnamed.jpg +3 -0
- app/images/unnamed1.jpg +3 -0
- app/images/unnamed2.jpg +3 -0
- app/images/unnamed22.jpg +3 -0
- app/main.py +0 -0
- main.py +6 -0
- pyproject.toml +7 -0
- requirements.txt +4 -0
- yolo11s-seg.pt +3 -0
Image_output/edges.png
ADDED
|
Git LFS Details
|
Image_output/overlay.png
ADDED
|
Git LFS Details
|
Image_output/segmented.png
ADDED
|
Git LFS Details
|
README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Body Measurement & Segmentation using YOLO + Mediapipe
|
| 2 |
+
|
| 3 |
+
This project detects a person from an image, segments the body, extracts pose keypoints using Mediapipe, and generates measurements such as height, shoulder width, and hip width. The script outputs segmented images with edge detection and overlay results inside an output folder.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Features
|
| 8 |
+
|
| 9 |
+
- Person segmentation using YOLO
|
| 10 |
+
- Pose keypoints detection using Mediapipe
|
| 11 |
+
- Calculates body measurements (height_px, shoulder, hip)
|
| 12 |
+
- Saves output images automatically:
|
| 13 |
+
- `segmented.png`
|
| 14 |
+
- `edges.png`
|
| 15 |
+
- `overlay.png`
|
| 16 |
+
- No command line args needed (image path inside script)
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## Project Requirements
|
| 21 |
+
|
| 22 |
+
**Python Version Required: `3.10+`**
|
| 23 |
+
|
| 24 |
+
Install project dependencies:
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
uv pip install ultralytics mediapipe==0.10.11 opencv-python numpy
|
app/.env
ADDED
|
File without changes
|
app/AI_Scripts/body_segment.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
import numpy as np
|
| 4 |
+
import math
|
| 5 |
+
import os
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
|
| 8 |
+
class BodyMeasurement:
|
| 9 |
+
def __init__(self, model_path="yolo11s-seg.pt"):
|
| 10 |
+
self.model = YOLO(model_path)
|
| 11 |
+
self.pose = mp.solutions.pose
|
| 12 |
+
|
| 13 |
+
# ---------------- PERSON SEGMENTATION -----------------
|
| 14 |
+
def segment(self, img):
|
| 15 |
+
results = self.model(img, verbose=False)
|
| 16 |
+
r = results[0]
|
| 17 |
+
|
| 18 |
+
if r.masks is None:
|
| 19 |
+
print("❌ No person detected!")
|
| 20 |
+
return img
|
| 21 |
+
|
| 22 |
+
mask = r.masks.data[0].cpu().numpy()
|
| 23 |
+
mask = cv2.resize(mask, (img.shape[1], img.shape[0]))
|
| 24 |
+
mask = (mask * 255).astype("uint8")
|
| 25 |
+
|
| 26 |
+
mask3 = cv2.merge([mask, mask, mask])
|
| 27 |
+
segmented = cv2.bitwise_and(img, mask3)
|
| 28 |
+
return segmented
|
| 29 |
+
|
| 30 |
+
# ---------------- EDGE DETECTION -----------------
|
| 31 |
+
def edges(self, rgb):
|
| 32 |
+
gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
|
| 33 |
+
gray = cv2.GaussianBlur(gray, (5,5), 0)
|
| 34 |
+
edges = cv2.Canny(gray,50,150)
|
| 35 |
+
return cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
|
| 36 |
+
|
| 37 |
+
# ---------------- KEYPOINTS -----------------
|
| 38 |
+
def keypoints(self,rgb):
|
| 39 |
+
h,w,_ = rgb.shape
|
| 40 |
+
pts = {}
|
| 41 |
+
with self.pose.Pose(static_image_mode=True) as p:
|
| 42 |
+
res = p.process(rgb)
|
| 43 |
+
if not res.pose_landmarks: return pts
|
| 44 |
+
for i,lm in enumerate(res.pose_landmarks.landmark):
|
| 45 |
+
x,y = int(lm.x*w), int(lm.y*h)
|
| 46 |
+
pts[self.pose.PoseLandmark(i).name] = (x,y)
|
| 47 |
+
return pts
|
| 48 |
+
|
| 49 |
+
# ---------------- MEASUREMENTS -----------------
|
| 50 |
+
def dist(self,a,b): return math.dist(a,b)
|
| 51 |
+
|
| 52 |
+
def measure(self,pts,img_h,height_cm=None):
|
| 53 |
+
M={}; G=lambda k: pts.get(k)
|
| 54 |
+
LS,RS = G("LEFT_SHOULDER"), G("RIGHT_SHOULDER")
|
| 55 |
+
LH,RH = G("LEFT_HIP"), G("RIGHT_HIP")
|
| 56 |
+
LA,RA = G("LEFT_ANKLE"), G("RIGHT_ANKLE")
|
| 57 |
+
nose = G("NOSE")
|
| 58 |
+
|
| 59 |
+
if LS and RS: M["shoulder_px"] = self.dist(LS,RS)
|
| 60 |
+
if LH and RH: M["hip_px"] = self.dist(LH,RH)
|
| 61 |
+
|
| 62 |
+
if nose and LA and RA:
|
| 63 |
+
mid=((LA[0]+RA[0])//2,(LA[1]+RA[1])//2)
|
| 64 |
+
height_px=self.dist(nose,mid)
|
| 65 |
+
else:
|
| 66 |
+
height_px=img_h
|
| 67 |
+
|
| 68 |
+
M["height_px"]=height_px
|
| 69 |
+
|
| 70 |
+
if height_cm:
|
| 71 |
+
scale=height_cm/height_px
|
| 72 |
+
M["scale_cm/px"]=scale
|
| 73 |
+
if "shoulder_px" in M: M["shoulder_cm"]=M["shoulder_px"] * scale
|
| 74 |
+
if "hip_px" in M: M["hip_cm"]=M["hip_px"] * scale
|
| 75 |
+
|
| 76 |
+
return M
|
| 77 |
+
|
| 78 |
+
# ---------------- PIPELINE -----------------
|
| 79 |
+
def run(self, img_path, height_cm=None, output_folder="results"):
|
| 80 |
+
if not os.path.exists(output_folder):
|
| 81 |
+
os.makedirs(output_folder)
|
| 82 |
+
|
| 83 |
+
img=cv2.imread(img_path)
|
| 84 |
+
if img is None:
|
| 85 |
+
raise FileNotFoundError("Image not found!")
|
| 86 |
+
|
| 87 |
+
seg=self.segment(img)
|
| 88 |
+
rgb=cv2.cvtColor(seg, cv2.COLOR_BGR2RGB)
|
| 89 |
+
edge=self.edges(rgb)
|
| 90 |
+
pts=self.keypoints(rgb)
|
| 91 |
+
M=self.measure(pts, rgb.shape[0], height_cm)
|
| 92 |
+
|
| 93 |
+
overlay=cv2.addWeighted(rgb,0.7,edge,0.3,0)
|
| 94 |
+
for x,y in pts.values():
|
| 95 |
+
cv2.circle(overlay,(x,y),3,(0,255,0),-1)
|
| 96 |
+
|
| 97 |
+
cv2.imwrite(f"{output_folder}/segmented.png", seg)
|
| 98 |
+
cv2.imwrite(f"{output_folder}/edges.png", cv2.cvtColor(edge, cv2.COLOR_RGB2BGR))
|
| 99 |
+
cv2.imwrite(f"{output_folder}/overlay.png", cv2.cvtColor(overlay, cv2.COLOR_RGB2BGR))
|
| 100 |
+
|
| 101 |
+
print("\n Output Saved In Folder:", output_folder)
|
| 102 |
+
print("Files:")
|
| 103 |
+
print(" - segmented.png\n - edges.png\n - overlay.png")
|
| 104 |
+
|
| 105 |
+
print("\n Measurements:")
|
| 106 |
+
for k,v in M.items(): print(k,":",round(v,2))
|
| 107 |
+
|
| 108 |
+
return M
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# ---------------- RUN WITHOUT CLI -----------------
|
| 112 |
+
if __name__=="__main__":
|
| 113 |
+
img_path = "/home/abhishek/projects/body_measurement/app/images/unnamed2.jpg" # input image path dalen
|
| 114 |
+
height_cm = 170 # Optional (human actual height)
|
| 115 |
+
output_folder = "Image_output" # Output save directory
|
| 116 |
+
|
| 117 |
+
BodyMeasurement().run(img_path, height_cm, output_folder)
|
app/images/Media(3).jpeg
ADDED
|
Git LFS Details
|
app/images/Media(4).jpeg
ADDED
|
Git LFS Details
|
app/images/baby.jpeg
ADDED
|
Git LFS Details
|
app/images/baby1.png
ADDED
|
Git LFS Details
|
app/images/baby2.jpeg
ADDED
|
Git LFS Details
|
app/images/baby3.jpeg
ADDED
|
Git LFS Details
|
app/images/canny.bmp
ADDED
|
Git LFS Details
|
app/images/cnn_diagram.png
ADDED
|
Git LFS Details
|
app/images/output_image.jpg
ADDED
|
Git LFS Details
|
app/images/shared image(2).jpeg
ADDED
|
Git LFS Details
|
app/images/shared image(3).jpeg
ADDED
|
Git LFS Details
|
app/images/shared image(4).jpeg
ADDED
|
Git LFS Details
|
app/images/unnamed.jpg
ADDED
|
Git LFS Details
|
app/images/unnamed1.jpg
ADDED
|
Git LFS Details
|
app/images/unnamed2.jpg
ADDED
|
Git LFS Details
|
app/images/unnamed22.jpg
ADDED
|
Git LFS Details
|
app/main.py
ADDED
|
File without changes
|
main.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def main():
|
| 2 |
+
print("Hello from body-measurement!")
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
if __name__ == "__main__":
|
| 6 |
+
main()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "body-measurement"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.8"
|
| 7 |
+
dependencies = []
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python
|
| 2 |
+
mediapipe==0.10.11
|
| 3 |
+
numpy
|
| 4 |
+
ultralytics
|
yolo11s-seg.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1caa81c0195412efa411b632bcfb8c184939dddb6ae41f6a80c41b211ff257c3
|
| 3 |
+
size 20669228
|