This project provides a graphical user interface (GUI) for creating AI-powered image masks based on positive and negative prompts using the Replicate API.
Built with DelphiFMX for Python, this application enables users to generate masks and inverted masks for uploaded images. It runs background processes asynchronously, ensuring the UI remains smooth and responsive during processing.
- AI Mask Generation: Generate masks for selected images using advanced AI models.
- Positive and Negative Prompts: Control the areas to include or exclude in masks using descriptive text prompts.
- Dual Outputs: View both the mask and inverted mask generated from the original image.
- Real-Time Status Updates: Live updates on image processing progress.
- Background Processing: Runs AI calls asynchronously, so the interface never freezes.
Below is an example screenshot of the application interface:
- Python 3.7 or higher
- Replicate API Token
(You must set this up and add it to your environment variables) - DelphiFMX for Python
-
Clone this repository:
git clone https://github.com/your-username/ai-image-masker.git cd ai-image-masker -
Install required dependencies:
pip install delphifmx replicate
-
Set your Replicate API token:
export REPLICATE_API_TOKEN=your_replicate_api_token -
Run the application:
python main.py
-
Upload an Image
- Click the "Select Image" button to choose an image from your computer.
- Supported formats:
.png,.jpg,.jpeg
-
Enter Prompts
- Positive Prompt: Specifies the elements you want to include in the mask.
- Negative Prompt: Specifies the elements you want to exclude from the mask.
- Prompts should be comma-separated (e.g.,
dog, person, tree).
-
Processing
- The app automatically sends the image and prompts to the Grounded SAM model hosted on Replicate.
- Progress updates appear in the status bar at the bottom of the app.
-
View Results
- Once processing is complete, both the mask and inverted mask will be saved locally and displayed in the GUI.
ai-image-masker/
│
├── main.py # Main Python script containing GUI and API logic
├── Air.style # Custom DelphiFMX style file for the GUI
├── mask.png # Generated mask image (output)
├── inverted_mask.png # Generated inverted mask image (output)
└── screenshot.png # Example screenshot of the app
import os
import replicate
import urllib.request
from delphifmx import *- Replicate API: Handles AI model execution for mask generation.
- DelphiFMX: Powers the cross-platform GUI.
- urllib.request: Downloads the mask and inverted mask images.
-
Image Selection
- User selects an image via the GUI file dialog.
-
Prompt Input
- Users provide positive and/or negative prompts to guide the AI.
-
Replicate Prediction
-
The app sends the image and prompts to the Replicate model using this code:
self.prediction = replicate.predictions.create( version="ee871c19efb1941f55f66a3d7d960428c8a5afcb77449547fe8e5a3ab9ebc21c", input={ "image": open(self.original_image_path, "rb"), "mask_prompt": positive_prompt, "negative_mask_prompt": negative_prompt, "adjustment_factor": -15 } )
-
Processing runs in the background to keep the UI responsive.
-
-
Results Display
- Final images (
mask.pngandinverted_mask.png) are downloaded and displayed in the application.
- Final images (
- Select an image of a person standing with a dog.
- Enter:
- Positive Prompt:
person - Negative Prompt:
dog
- Positive Prompt:
- Click Process.
- The app generates:
- Mask: Only the person is highlighted.
- Inverted Mask: Everything except the person is highlighted.
-
Error: "Invalid API Key"
- Ensure your Replicate API token is correctly set:
echo $REPLICATE_API_TOKEN
- Ensure your Replicate API token is correctly set:
-
Empty Mask Output
- Refine your positive and negative prompts for better results.
-
Slow Processing
- Processing speed depends on network speed and Replicate server load.
Contributions are welcome!
Submit a pull request or open an issue for bug reports, feature requests, or enhancements.
This project is licensed under the MIT License.
