A sophisticated system for analyzing baby posture in images to detect potential safety risks during sleep. This project uses computer vision and machine learning to identify potentially dangerous sleeping positions and provide recommendations for safer baby sleep practices.
- Advanced Image Analysis: Process and enhance images for optimal pose detection
- MediaPipe Integration: Skeletal keypoint detection with 3D coordinates
- ML-Powered Risk Assessment: Evaluate baby sleeping positions for safety concerns
- Detailed Analysis Reports: Get comprehensive insights on posture risks
- API-First Design: RESTful endpoints for easy integration with other systems
- Interactive Web Interface: Simple visual testing of the analysis system
- 🔥 Firebase Real-time Threshold Listener: Tự động cập nhật threshold từ Firebase mà không cần đọc liên tục
The system follows a modular pipeline architecture:
- Image normalization and enhancement
- Noise reduction and filtering
- Dimension standardization
- Quality optimization for pose detection
- MediaPipe Pose for skeletal keypoint detection
- 33 standard body keypoints with 3D coordinates (x, y, z)
- Confidence scores for reliable pose estimation
- Visibility metrics for each detected keypoint
- Head-torso angle detection for sleeping position
- Limb angle calculations for unnatural positions
- Blanket coverage detection
- Face-down position detection (higher risk factor)
- Comprehensive assessment of sleeping position safety
- Risk scoring system (1-10 scale)
- Detailed reasoning for risk assessment
- Specific recommendations based on identified risks
- Python 3.8+: Core programming language
- FastAPI: High-performance web framework
- MediaPipe: Google's ML solution for pose estimation
- OpenCV: Computer vision for image processing
- NumPy/Pandas: Data manipulation and analysis
- Scikit-learn: Machine learning models
- PIL/Pillow: Image handling and processing
- Python 3.8 or higher
- pip (Python package manager)
- Git
-
Clone the repository:
git clone https://github.com/givoxxs/baby_posture_analysis.git cd baby_posture_analysis -
Set up a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Environment configuration: Copy the
.env.examplefile to create your own.envfile:cp .env.example .env
Then edit the
.envfile to configure your settings:# Firebase Storage CLOUDINARY_CLOUD_NAME=your_cloud_name CLOUDINARY_API_KEY=your_api_key CLOUDINARY_API_SECRET=your_api_secret # Ngrok Configuration (optional, for public URL access) NGROK_AUTHTOKEN=your_ngrok_authtoken # FastAPI Settings API_HOST=0.0.0.0 API_PORT=8080 # Other settings...
You can start the server using one of the following methods:
-
Using the Python module directly:
python -m app.main
-
Using Uvicorn with options from your
.envfile:uvicorn app.main:app --reload --host 0.0.0.0 --port 8080
-
Or simply with default configuration:
uvicorn app.main:app --reload
-
Access the API documentation: Open your web browser and navigate to:
http://localhost:8080/docsThis provides interactive Swagger UI documentation for testing all API endpoints.
-
Test with the web interface: Open your browser and go to:
http://localhost:8080The simple web interface allows you to upload images and visualize the analysis results.
- POST
/api/images/process- Process an image with quality enhancements
- Parameters:
file: Image file upload (required)high_resolution: Boolean to maintain higher resolution (optional)apply_filter: Boolean to apply image enhancement filters (optional)
- Returns: Processed image with quality improvements
- POST
/api/pose/detect- Detect pose keypoints from an image
- Parameters:
file: Image file upload (required)high_resolution: Boolean to maintain higher resolution (optional)include_annotated_image: Boolean to include visualized keypoints (optional)include_analysis: Boolean to include basic analysis (optional)
- Returns: Detected keypoints and optional analysis
- POST
/api/pose/analyze- Analyze baby posture and risk level
- Parameters:
file: Image file upload (required)high_resolution: Boolean to maintain higher resolution (optional)
- Returns: Comprehensive posture analysis and risk assessment
- POST
/api/pipeline/analyze- Process image, detect pose, and analyze risk in one request
- Parameters:
file: Image file upload (required)high_resolution: Boolean to maintain higher resolution (optional)
- Returns: Complete analysis with:
- Position detection
- Face-down detection
- Coverage assessment
- Risk level and score
- Confidence rating
- Analysis reasoning
- Safety recommendations
- Annotated image
- POST
/api/video/analyze- Analyze baby posture from video footage
- Parameters:
file: Video file upload (required)sample_rate: Frames per second to analyze (optional)
- Returns: Time-series analysis of posture throughout the video
This system uses trained machine learning models to analyze posture features:
- Random Forest Classification: Used for posture classification
- Feature Scaling: Input normalization for consistent analysis
- Pre-trained Models: Located in the
app/modelsdirectory - Model Training: Notebooks for model training in
ML_traindirectory
The project includes notebooks for testing and validation:
ML_train/5_test_each_image.ipynb: Individual image testingML_train/4_test.ipynb: Model validation tests
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Project Developer - Phan Văn Toàn
Project Link: https://github.com/givoxxs/baby_posture_analysis