This project demonstrates a web application for removing backgrounds from images using a FastAPI backend and a ReactJS frontend. The backend utilizes the P3M-Net model for image segmentation and background removal.
The application allows users to upload an image through a ReactJS frontend. The image is sent to a FastAPI backend, where the P3M-Net model processes it to remove the background. The resulting image with a transparent background is then sent back to the frontend for display.
- Backend:
- FastAPI
- P3M-Net
- Frontend:
- ReactJS
P3M-Net is a deep learning model specifically designed for portrait matting. It excels at accurately separating foreground objects (typically people) from their backgrounds, even in complex scenarios. In this project, P3M-Net is used to generate an alpha matte (transparency mask) that is then used to create the final image with a transparent background.
-
Clone the repository:
git clone <your_repository_url> cd <your_repository_directory>/backend
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # Linux/macOS venv\Scripts\activate # Windows
-
Install dependencies:
pip install -r requirements.txt
-
Run the FastAPI server:
uvicorn main:app --reload
(Replace
main
with your Python file name if needed.)
-
Navigate to the frontend directory:
cd ../frontend
-
Install dependencies:
npm install
-
Run the React development server:
npm run dev
-
Access the application:
Open your web browser and navigate to
http://localhost:5173
.
- Backend:
- Modify the
origins
list in the FastAPICORSMiddleware
setup (main.py
) to include the origins of your frontend application. - Ensure that the P3M-Net model weights are located in the correct directory (backend/models/).
- Modify the
- Frontend:
- Change the base URL in the
axios
requests (ImageBackgroundRemoval.js
) to match the URL of your FastAPI backend.
- Change the base URL in the
- Open the web application in your browser.
- Click the "Choose File" button to select an image.
- Click the "Remove Background" button.
- The processed image with a transparent background will be displayed below the button.
- See
backend/requirements.txt
for backend dependencies. - See
frontend/package.json
for frontend dependencies.
- Ensure that the P3M-Net model weights are correctly placed in the backend directory.
- Configure CORS settings in the FastAPI backend to allow requests from your ReactJS frontend.
- Raise a issue if stuck somewhere