gittech. site

for different kinds of informations and explorations.

Posture Detection System: computer vision to prevent neck strain

Published at
Dec 29, 2024

Posture Detection System

A real-time posture detection system using computer vision to help maintain good sitting posture and prevent neck strain.

Features

  • Real-time posture detection using webcam
  • Neck angle calculation and monitoring
  • Visual feedback with status and angles
  • Audio alerts for prolonged bad posture
  • Timer display for bad posture duration

Requirements

  • Python 3.8 or higher
  • Webcam
  • Audio output capability

Installation

  1. Clone this repository:
git clone https://github.com/JordiNeil/bad_posture.git
  1. Create and activate a virtual environment:

Move to the backend directory

cd backend

Then create and activate a virtual environment:

Windows:

python -m venv venv
venv\Scripts\activate

macOS/Linux:

python3 -m venv venv
source venv/bin/activate
  1. Install the required packages:
pip install -r requirements.txt

Make sure you're in the backend directory and virtual environment is activated

cd backend
uvicorn main:app --reload
  1. Open your web browser and navigate to: http://localhost:8000/docs

  2. Click the "Start Detection" button and allow webcam access when prompted.

  3. Position yourself in front of the webcam

  4. The program will:

    • Show your posture status (Good/Bad)
    • Display your current neck angle
    • Show a timer when in bad posture
    • Play an alert sound after 2 minutes of bad posture

Project Structure

The project has the following structure:

bad_posture/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ main.py            # FastAPI backend server
β”‚   └── requirements.txt   # Python dependencies
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ index.html        # Main HTML file
β”‚   β”œβ”€β”€ styles.css        # Styles
β”‚   β”œβ”€β”€ app.js            # Frontend JavaScript
β”‚   └── sounds/
β”‚       └── soft-alert.mp3 # Alert sound
└── README.md

Posture Guidelines

  • Good posture: Neck angle between 65Β° and 100Β°
  • Bad posture: Neck angle outside this range
  • Try to maintain your head aligned with your shoulders

Troubleshooting

  1. No webcam found:

    • Check webcam connection
    • Try changing camera index (0 or 1) in the code
  2. Sound not playing:

    • Verify sound file exists in correct location
    • Check system audio settings
    • Try using winsound alternative (Windows only)
  3. MediaPipe errors:

    • Ensure good lighting conditions
    • Check if camera is properly positioned

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Running the Application

  1. Activate the virtual environment:

Windows:

venv\Scripts\activate

macOS/Linux:

source venv/bin/activate
  1. Start the application:
cd backend
uvicorn main:app --reload
  1. Open your web browser and navigate to: http://localhost:8000

  2. Click the "Start Detection" button and allow webcam access when prompted.

  3. The application will periodically send images to the backend for processing and update the UI with the posture status.