This commit is contained in:
aparnah 2024-08-09 15:32:34 +05:30
parent 5856721002
commit 1b0fb383c6
2 changed files with 265 additions and 192 deletions

View File

@ -1,9 +1,9 @@
const fs = require('fs'); const fs = require("fs");
const path = require('path'); const path = require("path");
const ArgumentParser = require('argparse'); const { ArgumentParser } = require("argparse");
const cv = require('opencv'); const cv = require("@techstark/opencv-js");
const yaml = require('js-yaml'); const yaml = require("js-yaml");
const { Pose, POSE_LANDMARKS } = require('@mediapipe/pose'); const { Pose, POSE_LANDMARKS } = require("@mediapipe/pose");
const logging = console; const logging = console;
const warnings = console; const warnings = console;
@ -25,8 +25,14 @@ class Landmarker {
this.frontImage = cv.imread(this.args.frontImage); this.frontImage = cv.imread(this.args.frontImage);
this.sideImage = cv.imread(this.args.sideImage); this.sideImage = cv.imread(this.args.sideImage);
this.frontImageResized = this.frontImage.resize(Landmarker.resizedHeight, Landmarker.resizedWidth); this.frontImageResized = cv.resize(
this.sideImageResized = this.sideImage.resize(Landmarker.resizedHeight, Landmarker.resizedWidth); this.frontImage,
new cv.Size(Landmarker.resizedWidth, Landmarker.resizedHeight),
);
this.sideImageResized = cv.resize(
this.sideImage,
new cv.Size(Landmarker.resizedWidth, Landmarker.resizedHeight),
);
this.distances = {}; this.distances = {};
@ -36,7 +42,7 @@ class Landmarker {
this.pose = new Pose({ this.pose = new Pose({
locateFile: (file) => { locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/pose/${file}`; return `https://cdn.jsdelivr.net/npm/@mediapipe/pose/${file}`;
} },
}); });
this.landmarksIndices = [ this.landmarksIndices = [
@ -51,12 +57,12 @@ class Landmarker {
POSE_LANDMARKS.LEFT_KNEE, POSE_LANDMARKS.LEFT_KNEE,
POSE_LANDMARKS.RIGHT_KNEE, POSE_LANDMARKS.RIGHT_KNEE,
POSE_LANDMARKS.LEFT_ANKLE, POSE_LANDMARKS.LEFT_ANKLE,
POSE_LANDMARKS.RIGHT_ANKLE POSE_LANDMARKS.RIGHT_ANKLE,
]; ];
} }
loadLandmarks() { loadLandmarks() {
const file = fs.readFileSync(this.args.yamlFile, 'utf8'); const file = fs.readFileSync(this.args.yamlFile, "utf8");
const landmarksData = yaml.load(file); const landmarksData = yaml.load(file);
const measurements = {}; const measurements = {};
for (const measurement of landmarksData.measurements) { for (const measurement of landmarksData.measurements) {
@ -66,15 +72,53 @@ class Landmarker {
} }
parseArgs() { parseArgs() {
const parser = new ArgumentParser(); const parser = new ArgumentParser({
parser.add_argument('--front', { dest: 'frontImage', type: 'str', help: 'Front image' }); description: "Process images and calculate measurements",
parser.add_argument('--side', { dest: 'sideImage', type: 'str', help: 'Side image' }); });
parser.add_argument('--poseDetectionConfidence', { dest: 'poseDetectionConfidence', default: 0.5, type: 'float', help: 'Confidence score for pose detection' }); parser.add_argument("--front", {
parser.add_argument('--poseTrackingConfidence', { dest: 'poseTrackingConfidence', default: 0.5, type: 'float', help: 'Confidence score for pose tracking' }); dest: "frontImage",
parser.add_argument('--personHeight', { dest: 'personHeight', type: 'int', help: 'Person height in cm' }); required: true,
parser.add_argument('--pixelHeight', { dest: 'pixelHeight', type: 'int', help: 'Pixel height of person' }); help: "Path to the front image",
parser.add_argument('--measurement', { dest: 'measurement', nargs: '+', type: 'str', help: 'Type of measurement' }); });
parser.add_argument('--yamlFile', { dest: 'yamlFile', type: 'str', help: 'Path to the YAML file containing landmarks' }); parser.add_argument("--side", {
dest: "sideImage",
required: true,
help: "Path to the side image",
});
parser.add_argument("--poseDetectionConfidence", {
dest: "poseDetectionConfidence",
default: 0.5,
type: "float",
help: "Confidence score for pose detection",
});
parser.add_argument("--poseTrackingConfidence", {
dest: "poseTrackingConfidence",
default: 0.5,
type: "float",
help: "Confidence score for pose tracking",
});
parser.add_argument("--personHeight", {
dest: "personHeight",
required: true,
type: "int",
help: "Person height in cm",
});
parser.add_argument("--pixelHeight", {
dest: "pixelHeight",
type: "int",
help: "Pixel height of person",
});
parser.add_argument("--measurement", {
dest: "measurement",
nargs: "+",
type: "str",
help: "Type of measurement",
});
parser.add_argument("--yamlFile", {
dest: "yamlFile",
required: true,
help: "Path to the YAML file containing landmarks",
});
return parser.parse_args(); return parser.parse_args();
} }
@ -90,13 +134,19 @@ class Landmarker {
if (!this.measurements[m]) { if (!this.measurements[m]) {
throw new Error("Incorrect input (input not present in config.yml)"); throw new Error("Incorrect input (input not present in config.yml)");
} else { } else {
const distance = this.calculateDistanceBetweenLandmarks(frontResults, m); const distance = this.calculateDistanceBetweenLandmarks(
frontResults,
m,
);
table.push([m, distance]); table.push([m, distance]);
} }
} }
} else { } else {
for (const m in this.measurements) { for (const m in this.measurements) {
const distance = this.calculateDistanceBetweenLandmarks(frontResults, m); const distance = this.calculateDistanceBetweenLandmarks(
frontResults,
m,
);
table.push([m, distance]); table.push([m, distance]);
} }
} }
@ -110,23 +160,30 @@ class Landmarker {
const frontResults = await this.pose.estimatePoses(this.frontImageResized); const frontResults = await this.pose.estimatePoses(this.frontImageResized);
const sideResults = await this.pose.estimatePoses(this.sideImageResized); const sideResults = await this.pose.estimatePoses(this.sideImageResized);
this.sideImageKeypoints = this.sideImageResized.copy(); this.sideImageKeypoints = this.sideImageResized.clone();
this.frontImageKeypoints = this.frontImageResized.copy(); this.frontImageKeypoints = this.frontImageResized.clone();
if (frontResults[0].landmarks) { if (frontResults[0].landmarks) {
this.drawLandmarks(this.frontImageKeypoints, frontResults[0].landmarks, this.landmarksIndices); this.drawLandmarks(
this.frontImageKeypoints,
frontResults[0].landmarks,
this.landmarksIndices,
);
} }
if (sideResults[0].landmarks) { if (sideResults[0].landmarks) {
this.drawLandmarks(this.sideImageKeypoints, sideResults[0].landmarks, this.landmarksIndices); this.drawLandmarks(
this.sideImageKeypoints,
sideResults[0].landmarks,
this.landmarksIndices,
);
} }
return { return {
frontResults: frontResults[0], frontResults: frontResults[0],
sideResults: sideResults[0] sideResults: sideResults[0],
}; };
} }
pixelToMetricRatio() { pixelToMetricRatio() {
this.pixelHeight = this.pixelDistance * 2;
const pixelToMetricRatio = this.personHeight / this.pixelHeight; const pixelToMetricRatio = this.personHeight / this.pixelHeight;
logging.debug("pixelToMetricRatio %s", pixelToMetricRatio); logging.debug("pixelToMetricRatio %s", pixelToMetricRatio);
return pixelToMetricRatio; return pixelToMetricRatio;
@ -144,7 +201,7 @@ class Landmarker {
} }
circle(image, cx, cy) { circle(image, cx, cy) {
return image.drawCircle(new cv.Point(cx, cy), 2, new cv.Vec(255, 0, 0), -1); cv.circle(image, new cv.Point(cx, cy), 2, new cv.Scalar(255, 0, 0), -1);
} }
calculateDistanceBetweenLandmarks(frontResults, measurementName) { calculateDistanceBetweenLandmarks(frontResults, measurementName) {
@ -163,7 +220,7 @@ class Landmarker {
current.x * Landmarker.resizedWidth, current.x * Landmarker.resizedWidth,
current.y * Landmarker.resizedHeight, current.y * Landmarker.resizedHeight,
next.x * Landmarker.resizedWidth, next.x * Landmarker.resizedWidth,
next.y * Landmarker.resizedHeight next.y * Landmarker.resizedHeight,
); );
const realDistance = pixelDistance * this.pixelToMetricRatio(); const realDistance = pixelDistance * this.pixelToMetricRatio();
totalDistance += realDistance; totalDistance += realDistance;
@ -176,43 +233,57 @@ class Landmarker {
} }
getCenterTopPoint(sideResults) { getCenterTopPoint(sideResults) {
const grayImage = this.sideImageKeypoints.cvtColor(cv.COLOR_BGR2GRAY); const grayImage = cv.cvtColor(this.sideImageKeypoints, cv.COLOR_BGR2GRAY);
const blurredImage = grayImage.gaussianBlur(new cv.Size(5, 5), 0); const blurredImage = cv.GaussianBlur(grayImage, new cv.Size(5, 5), 0);
const roi = blurredImage.getRegion(new cv.Rect(0, 0, this.sideImageResized.cols, Math.floor(this.sideImageResized.rows / 2))); const roi = blurredImage.roi(
this.edges = roi.canny(50, 150); new cv.Rect(
const contours = this.edges.findContours(cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE); 0,
0,
this.sideImageResized.cols,
Math.floor(this.sideImageResized.rows / 2),
),
);
this.edges = cv.Canny(roi, 50, 150);
const contours = this.edges.findContours(
cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE,
);
let xt, yt; let xt, yt;
this.topmostPoint = null; this.topmostPoint = null;
if (contours.length > 0) { for (const contour of contours) {
const largestContour = contours.sort((a, b) => b.area - a.area)[0]; const [xt, yt] = contour.minEnclosingCircle();
this.topmostPoint = largestContour.minBy(pt => pt.y); if (this.topmostPoint === null || yt < this.topmostPoint[1]) {
xt = this.topmostPoint.x; this.topmostPoint = [xt, yt];
yt = this.topmostPoint.y; }
this.circle(this.sideImageKeypoints, xt, yt);
} }
let xc, yc; const { x, y } = sideResults.landmarks[POSE_LANDMARKS.NOSE];
if (sideResults.landmarks) {
const leftHip = sideResults.landmarks[POSE_LANDMARKS.LEFT_HIP];
const rightHip = sideResults.landmarks[POSE_LANDMARKS.RIGHT_HIP];
const centerPoint = [ const centerPoint = [
(leftHip.x + rightHip.x) / 2, x * Landmarker.resizedWidth,
(leftHip.y + rightHip.y) / 2 y * Landmarker.resizedHeight,
]; ];
xc = Math.round(centerPoint[0] * this.sideImageResized.cols); this.pixelHeight = Math.abs(centerPoint[1] - this.topmostPoint[1]);
yc = Math.round(centerPoint[1] * this.sideImageResized.rows);
this.circle(this.sideImageKeypoints, xc, yc);
}
this.pixelDistance = this.euclideanDistance(xt, yt, xc, yc); cv.circle(
this.pixelDistance *= 2; this.sideImageKeypoints,
new cv.Point(centerPoint[0], centerPoint[1]),
2,
new cv.Scalar(255, 0, 0),
-1,
);
cv.circle(
this.sideImageKeypoints,
new cv.Point(this.topmostPoint[0], this.topmostPoint[1]),
2,
new cv.Scalar(255, 0, 0),
-1,
);
} }
} }
(async () => {
const landmarker = new Landmarker(); const landmarker = new Landmarker();
await landmarker.run(); landmarker.run().catch((error) => {
})(); console.error(error);
});

View File

@ -189,6 +189,7 @@ class Landmarker:
tablefmt="plain", tablefmt="plain",
) )
print(output) print(output)
self.pose.close() self.pose.close()
def process_images(self): def process_images(self):
@ -279,6 +280,7 @@ class Landmarker:
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return distance return distance
def get_center_top_point(self, side_results): def get_center_top_point(self, side_results):
gray_image = cv2.cvtColor( gray_image = cv2.cvtColor(
self.side_image_keypoints, self.side_image_keypoints,