{"slug": "stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vue-js", "title": "Stop Slouching! Build a Real-time AI Posture Guard with MediaPipe and Vue.js", "summary": "A developer has published a tutorial for building \"Posture Guardian,\" a browser-based tool that uses MediaPipe's pose estimation model and Vue.js to detect slouching, forward head posture, and uneven shoulders in real time. The tool captures webcam frames, extracts 33 skeletal landmarks, and applies trigonometry to ear-to-shoulder offsets, triggering browser notifications when posture thresholds are exceeded.", "body_md": "Let's be honest: as engineers, our \"focus mode\" usually involves leaning into the monitor until our noses almost touch the screen. We start the day sitting like a king and end it looking like a question mark. Back pain isn't just a meme; it's a productivity killer.\n\nIn this tutorial, we’re building **Posture Guardian**, a browser-based tool using **real-time computer vision** and **pose estimation** to detect slouching, forward head posture (the \"tech neck\"), and uneven shoulders. We'll leverage **MediaPipe** and **Vue.js** to create a seamless experience that alerts you the moment your ergonomics fail. If you’ve been looking for a practical application for **machine learning in the browser**, this is it.\n\nHow does a browser \"know\" you're slouching? We need to capture frames from the webcam, process them through a pre-trained model to find skeletal landmarks, and then apply some basic trigonometry to calculate angles.\n\n``` php\ngraph TD\n    A[Webcam Stream] --> B[MediaPipe Pose Model]\n    B --> C{Landmark Detection}\n    C -->|Coordinates| D[Ergonomics Logic Engine]\n    D --> E[Calculate Neck & Shoulder Angles]\n    E --> F{Threshold Exceeded?}\n    F -->|Yes| G[Browser Notification + Visual Alert]\n    F -->|No| H[Status: Healthy]\n    G --> A\n    H --> A\n```\n\nTo follow along, you’ll need:\n\n`@mediapipe/pose`)\nFirst, we need to initialize the MediaPipe Pose model. This model identifies 33 landmarks on the human body. For posture, we specifically care about the ears, shoulders, and hips.\n\n``` js\n// postureService.js\nimport { Pose } from \"@mediapipe/pose\";\n\nexport const createPoseEstimator = (onResults) => {\n  const pose = new Pose({\n    locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/pose/${file}`,\n  });\n\n  pose.setOptions({\n    modelComplexity: 1,\n    smoothLandmarks: true,\n    minDetectionConfidence: 0.5,\n    minTrackingConfidence: 0.5,\n  });\n\n  pose.onResults(onResults);\n  return pose;\n};\n```\n\nTo detect \"Head Forward\" posture, we calculate the horizontal distance and angle between the **Ear** and the **Shoulder**. If the ear moves too far forward relative to the shoulder line, you're officially a turtle. 🐢\n\n```\n// Logic to detect Forward Head Posture\nfunction analyzePosture(landmarks) {\n  const leftEar = landmarks[7];\n  const rightEar = landmarks[8];\n  const leftShoulder = landmarks[11];\n  const rightShoulder = landmarks[12];\n\n  // Calculate the average ear-to-shoulder horizontal offset\n  const earMidX = (leftEar.x + rightEar.x) / 2;\n  const shoulderMidX = (leftShoulder.x + rightShoulder.x) / 2;\n\n  const diff = Math.abs(earMidX - shoulderMidX);\n\n  // If the head is more than 15% forward relative to the body\n  if (diff > 0.15) {\n    return { status: 'bad', message: 'Sit up straight! Your neck is straining.' };\n  }\n  return { status: 'good', message: 'Perfect Posture!' };\n}\n```\n\nNow, let's wrap this in a Vue component. We'll use the **Webcam API** to feed the video stream into the Pose model.\n\n```\n<template>\n  <div class=\"posture-container\">\n    <video ref=\"videoElement\" class=\"hidden-video\" autoplay></video>\n    <canvas ref=\"canvasElement\" class=\"overlay-canvas\"></canvas>\n\n    <div :class=\"['alert-box', statusClass]\">\n      <h2>{{ currentStatus }}</h2>\n    </div>\n  </div>\n</template>\n\n<script setup>\nimport { ref, onMounted } from 'vue';\nimport { createPoseEstimator } from './postureService';\n\nconst videoElement = ref(null);\nconst canvasElement = ref(null);\nconst currentStatus = ref('Initializing...');\nconst statusClass = ref('neutral');\n\nonMounted(async () => {\n  const pose = createPoseEstimator((results) => {\n    // Draw landmarks on canvas\n    drawUserFeedback(results);\n\n    // Run our logic\n    const analysis = analyzePosture(results.poseLandmarks);\n    currentStatus.value = analysis.message;\n    statusClass.value = analysis.status;\n\n    if (analysis.status === 'bad') {\n       triggerNotification();\n    }\n  });\n\n  const camera = new Camera(videoElement.value, {\n    onFrame: async () => {\n      await pose.send({ image: videoElement.value });\n    },\n    width: 640,\n    height: 480\n  });\n  camera.start();\n});\n</script>\n```\n\nBuilding a simple detection tool is one thing, but making it production-ready involves handling lighting variations, camera calibration, and state management (to avoid spamming notifications every second).\n\nFor deeper dives into **advanced AI patterns**, production-grade **Mediapipe configurations**, or how to optimize **TensorFlow.js performance** in large-scale Vue apps, I highly recommend checking out the technical deep-dives over at [WellAlly Blog](https://www.wellally.tech/blog). They cover excellent strategies for integrating wellness tech into the modern developer workflow.\n\nDon't just show a message on the screen—tell the user even if they are in another tab.\n\n```\nfunction triggerNotification() {\n  if (Notification.permission === \"granted\") {\n    // Throttle notifications so we don't annoy the user\n    if (Date.now() - lastNotificationTime > 60000) {\n      new Notification(\"Posture Alert! 🚨\", {\n        body: \"You're slouching again. Take a deep breath and sit up.\",\n        icon: \"/guardian-logo.png\"\n      });\n      lastNotificationTime = Date.now();\n    }\n  }\n}\n```\n\nBy combining the power of **MediaPipe** with a reactive framework like **Vue.js**, we’ve built a tool that actually improves your daily life. No expensive hardware, just a few lines of JavaScript and a webcam.\n\n**What's next?**\n\nHow do you stay ergonomic at your desk? Let me know in the comments below! And don't forget to fix your posture right now—I know you're leaning forward while reading this. 😉\n\nHappy coding! 🥑", "url": "https://wpnews.pro/news/stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vue-js", "canonical_source": "https://dev.to/beck_moulton/stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vuejs-255n", "published_at": "2026-09-15 00:18:00+00:00", "updated_at": "2026-09-15 01:01:42.519775+00:00", "lang": "en", "topics": ["computer-vision", "machine-learning", "ai-tools", "developer-tools"], "entities": ["MediaPipe", "Vue.js", "Posture Guardian"], "alternates": {"html": "https://wpnews.pro/news/stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vue-js", "markdown": "https://wpnews.pro/news/stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vue-js.md", "text": "https://wpnews.pro/news/stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vue-js.txt", "jsonld": "https://wpnews.pro/news/stop-slouching-build-a-real-time-ai-posture-guard-with-mediapipe-and-vue-js.jsonld"}}