This is the final project for the subject 'Datascience for IoT'. With the help of OpenCV and its documentation on poor posture detection, it has been possible to create and modify examples to a working live-feed poor posture detection program. By adding a live-feed, SMS notification service, and changes in the code, this prototype will notify you via SMS whenever your posture has been poor for 3 minutes.
PostureCheck-Demo.mp4
This demo demonstrates that excessive neck or torso inclination can trigger the program to send an SMS. A frame is considered bad if neck inclination exceeds 29 degrees or torso inclination exceeds 19 degrees. Based on FPS, the program calculates the duration of poor posture and takes action.
As I spend significant time on my computer, it's crucial for me to maintain good posture. This led me to come up with an idea to solve this problem for myself. I began exploring potential solutions and came across the concept of body pose tracking. It is commonly used in yoga to ensure proper alignment in poses and during workouts to ensure exercises are done correctly. This technology was an ideal fit for my idea, and so I delved deeper into researching body pose tracking.
During my research on body pose tracking, I encountered the names MediaPipe and Yolov7. After reviewing the results, I ultimately decided to use MediaPipe, as I found it to be more suitable for my project after watching a video on it.
While searching for MediaPipe tutorials that addressed poor posture, I discovered a page on OpenCV that utilized the technology I desired and provided an excellent tutorial for the concept I aimed to create. My plan was to first implement it on my PC, and then later move it to a Raspberry Pi. The tutorial was excellent, but it was designed for video inputs such as mp4 or mov files. I wanted a live-feed using my webcam, so I began modifying the tutorial's code to accommodate this.
I decided to incorporate an SMS service into the code so that it would send notifications when necessary. After evaluating various SMS services, I chose Twilio, as it offered a free trial and a credit balance to test with.
So far, the project had progressed smoothly, but installing it on the Raspberry Pi proved to be more challenging than anticipated. Upon attempting to install the required python packages, I quickly discovered that some of them were not compatible with the existing Raspberry Pi OS. I then learned that I had to install a different version of the OS, called Raspbian Buster, in order to proceed. After installing this OS, all necessary packages were successfully installed.
When I first ran the program, I unfortunately discovered that the camera's frame rate was very poor. Additionally, there was an issue with the duration of poor posture. Typically, a SMS would be sent after 10 seconds, however, this 10 seconds was taking almost a full minute. I discovered that the function to get the FPS of OpenCV does not work on a live-feed and will always return 30 fps. This meant that the program was calculating the time with 30 fps, even when the actual FPS was much lower, such as 5. This caused the time to be 6 times slower in the program. I was able to fix this problem by calculating the FPS for each frame individually.
- Raspberry Pi 4
- Webcam or camera for Raspberry Pi
- Phone number for receiving text messages
- Twilio trial account
- Raspbian Buster OS
- Python v3.7.3 or above
It is recommended to use a virtual environment for your python packages. You also NEED Raspbian Buster OS.
-
Install OpenCV by running
pip install opencv-python
check out this installation guide if you have errors -
Install mediapipe for Raspberry Pi by running
pip install mediapipe-rpi4
-
Install Twilio by running
pip install twilio
Now that we have installed all the packages, we only have to change a few parameters in the code.
There are the 2 scripts in this program:
- main.py ( in this file you can find all the calculations and UI code for this program)
- sms.py ( in this file you can setup your sms service to get notified)
You have to change a few things in sms.py to make this work. These values are commented with #Todo. For more info on this check out Twilio.
Note: if you have 2 cameras on your pc, you might need to change a value in main.py. Change the value from 0 to 1, if this is still not working try 2, 3 and so on.
Line 45: cap = cv2.VideoCapture(0)
Now that everything is set up, you can run the program. Make sure you have your camera plugged in, and position yourself in front of the camera with a perfect side view.
Run the script using the command python main.py
The script will begin monitoring the side-view of a person and will calculate the neck and torso inclination. If poor body posture is detected, a text message will be sent to the configured phone number after a certain amount of time.
Make sure to use the compatible version of the packages with your python version. For more info about the code, check out the comments in the code.
This project uses Mediapipe and OpenCV to detect poor body posture and alert the user via text message. By using a Raspberry Pi and a webcam, the project is able to monitor the side-view of a person and calculate the neck and torso inclination in real-time.