Posture Corrector is a Python application designed to help monitor and correct poor posture using real-time webcam feed analysis. This project demonstrates proficiency in computer vision, real-time processing, and user feedback mechanisms.
"Posture Corrector" is an interactive tool that tracks shoulder and neck angles using a webcam feed to detect and correct poor posture. The core functionality is powered by the mediapipe
library, and the application provides real-time feedback, including visual alerts and sound notifications.
- Python: The programming language used to build the application.
- OpenCV: For real-time video capture and processing.
- Mediapipe: Leveraged for pose detection and landmark tracking.
- Numpy: Used for numerical computations.
- Simpleaudio: Used to play alert sounds when poor posture is detected.
- main.py: Entry point of the application that captures the webcam feed, processes the frames to calculate angles, and provides real-time feedback.
- utils.py: Contains utility functions for extracting landmark coordinates, calculating angles, and drawing angles on frames.
- sounds/alert.wav: Sound file used for audio notifications.
- Python 3.12 or later
- Virtual environment (venv)
- OpenCV
- Mediapipe
- Simpleaudio
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Ensure the sound file is in the correct location:
Place the
alert.wav
sound file in thesounds
directory.
-
Run the application:
python src/main.py
-
Calibration: The application will automatically calibrate for the first 30 frames.
-
Real-Time Feedback: The application will monitor your posture and provide real-time feedback through visual cues and sound alerts if poor posture is detected.
In the current implementation, the VideoProcessor
class in video_processor.py
initializes the camera with index 1. This is done because index 0 defaults to connecting to the iPhone via Continuity Camera. If this is not the case for you, you should set the camera index to 0 to use your default camera.
-
Initialize the VideoCapture object with the desired camera index:
0
- Connects to the default camera on your system (usually the webcam, but can be a USB camera or other video source, in my case, the iPhone).1
- Connects to the secondary camera on your system (in my case, the MacBook's built-in camera).self.capture = cv2.VideoCapture(0)
To help you understand and potentially improve the performance of this application, you can profile the code to identify bottlenecks.
-
Import the Profiler:
Add the following lines at the beginning of your script to enable profiling:
import cProfile import pstats
-
Profile the Main Function:
Wrap the
main()
function call with the profiler:if __name__ == "__main__": profiler = cProfile.Profile() profiler.enable() main() profiler.disable() stats = pstats.Stats(profiler).sort_stats('cumtime') stats.print_stats(10) # Print the 10 most time-consuming functions
-
Run the Script:
Execute your script as usual. The profiler will output a list of functions and the time spent in each.
-
Analyze the Output:
Look for functions with the highest cumulative time (
cumtime
). These are the areas where your code spends most of its time and may be the best targets for optimization.