Spaces:
Running
on
T4
Running
on
T4
Commit
·
fad49fa
1
Parent(s):
05ad3d5
feat: invert-mask option in UI and core; add docs (no secrets)
Browse files- API_Usage_Guide.md +217 -0
- app.py +9 -2
- src/core.py +5 -2
API_Usage_Guide.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# API Usage Guide - Photo Object Removal
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
This guide provides step-by-step instructions for using the Photo Object Removal API to remove objects from images using AI inpainting.
|
| 5 |
+
|
| 6 |
+
**Base URL:** `https://logicgoinfotechspaces-object-remover.hf.space`
|
| 7 |
+
**Authentication:** Bearer token (optional)
|
| 8 |
+
|
| 9 |
+
## Quick Start
|
| 10 |
+
|
| 11 |
+
### 1. Health Check
|
| 12 |
+
Verify the API is running:
|
| 13 |
+
```bash
|
| 14 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 15 |
+
https://logicgoinfotechspaces-object-remover.hf.space/health
|
| 16 |
+
```
|
| 17 |
+
**Response:** `{"status":"healthy"}`
|
| 18 |
+
|
| 19 |
+
### 2. Upload Image
|
| 20 |
+
Upload the image you want to edit:
|
| 21 |
+
```bash
|
| 22 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 23 |
+
-F image=@your_image.jpg \
|
| 24 |
+
https://logicgoinfotechspaces-object-remover.hf.space/upload-image
|
| 25 |
+
```
|
| 26 |
+
**Response:** `{"id":"9cf61445-f83b-4c97-9272-c81647f90d68","filename":"your_image.jpg"}`
|
| 27 |
+
|
| 28 |
+
### 3. Upload Mask
|
| 29 |
+
Upload a mask showing areas to remove (white/colored areas = remove):
|
| 30 |
+
```bash
|
| 31 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 32 |
+
-F [email protected] \
|
| 33 |
+
https://logicgoinfotechspaces-object-remover.hf.space/upload-mask
|
| 34 |
+
```
|
| 35 |
+
**Response:** `{"id":"d044a390-dde2-408a-b7cf-d508385e56ed","filename":"mask.png"}`
|
| 36 |
+
|
| 37 |
+
### 4. Process Image
|
| 38 |
+
Remove objects using the uploaded image and mask:
|
| 39 |
+
```bash
|
| 40 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 41 |
+
-H "Content-Type: application/json" \
|
| 42 |
+
-d '{"image_id":"9cf61445-f83b-4c97-9272-c81647f90d68","mask_id":"d044a390-dde2-408a-b7cf-d508385e56ed"}' \
|
| 43 |
+
https://logicgoinfotechspaces-object-remover.hf.space/inpaint
|
| 44 |
+
```
|
| 45 |
+
**Response:** `{"result":"output_b09568698bbd4aa591b1598c01f2f745.png"}`
|
| 46 |
+
|
| 47 |
+
### 5. View Result
|
| 48 |
+
Open the result image in your browser:
|
| 49 |
+
```
|
| 50 |
+
https://logicgoinfotechspaces-object-remover.hf.space/result/output_b09568698bbd4aa591b1598c01f2f745.png
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
## Alternative Methods
|
| 54 |
+
|
| 55 |
+
### Method 1: Get Direct URL
|
| 56 |
+
Use `/inpaint-url` to get a shareable URL:
|
| 57 |
+
```bash
|
| 58 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 59 |
+
-H "Content-Type: application/json" \
|
| 60 |
+
-d '{"image_id":"<image_id>","mask_id":"<mask_id>"}' \
|
| 61 |
+
https://logicgoinfotechspaces-object-remover.hf.space/inpaint-url
|
| 62 |
+
```
|
| 63 |
+
**Response:**
|
| 64 |
+
```json
|
| 65 |
+
{
|
| 66 |
+
"result":"output_xxx.png",
|
| 67 |
+
"url":"https://logicgoinfotechspaces-object-remover.hf.space/download/output_xxx.png"
|
| 68 |
+
}
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### Method 2: One-Step Processing
|
| 72 |
+
Upload and process in a single request:
|
| 73 |
+
```bash
|
| 74 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 75 |
+
-F [email protected] \
|
| 76 |
+
-F [email protected] \
|
| 77 |
+
https://logicgoinfotechspaces-object-remover.hf.space/inpaint-multipart
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### Method 3: Download Result
|
| 81 |
+
Download the result file:
|
| 82 |
+
```bash
|
| 83 |
+
curl -L https://logicgoinfotechspaces-object-remover.hf.space/download/output_xxx.png \
|
| 84 |
+
-o result.png
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
## Postman Setup
|
| 88 |
+
|
| 89 |
+
### 1. Upload Image
|
| 90 |
+
- **Method:** POST
|
| 91 |
+
- **URL:** `https://logicgoinfotechspaces-object-remover.hf.space/upload-image`
|
| 92 |
+
- **Headers:** `Authorization: Bearer <API_TOKEN>`
|
| 93 |
+
- **Body:** form-data
|
| 94 |
+
- Key: `image` (Type: File)
|
| 95 |
+
- Value: Select your image file
|
| 96 |
+
|
| 97 |
+
### 2. Upload Mask
|
| 98 |
+
- **Method:** POST
|
| 99 |
+
- **URL:** `https://logicgoinfotechspaces-object-remover.hf.space/upload-mask`
|
| 100 |
+
- **Headers:** `Authorization: Bearer <API_TOKEN>`
|
| 101 |
+
- **Body:** form-data
|
| 102 |
+
- Key: `mask` (Type: File)
|
| 103 |
+
- Value: Select your mask file
|
| 104 |
+
|
| 105 |
+
### 3. Process Image
|
| 106 |
+
- **Method:** POST
|
| 107 |
+
- **URL:** `https://logicgoinfotechspaces-object-remover.hf.space/inpaint`
|
| 108 |
+
- **Headers:**
|
| 109 |
+
- `Authorization: Bearer <API_TOKEN>`
|
| 110 |
+
- `Content-Type: application/json`
|
| 111 |
+
- **Body:** raw JSON
|
| 112 |
+
```json
|
| 113 |
+
{
|
| 114 |
+
"image_id": "9cf61445-f83b-4c97-9272-c81647f90d68",
|
| 115 |
+
"mask_id": "d044a390-dde2-408a-b7cf-d508385e56ed"
|
| 116 |
+
}
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
## Mask Creation Guide
|
| 120 |
+
|
| 121 |
+
### Mask Formats
|
| 122 |
+
- **RGBA PNG:** Pixels with alpha=0 are treated as areas to remove
|
| 123 |
+
- **RGB/Grayscale:** Pixels with value > 0 are treated as areas to remove
|
| 124 |
+
|
| 125 |
+
### Creating Masks
|
| 126 |
+
1. **Paint Method:** Use any image editor to paint white/colored areas over objects you want to remove
|
| 127 |
+
2. **Transparency Method:** Create a PNG with transparent areas where you want objects removed
|
| 128 |
+
3. **Grayscale Method:** Create a black image and paint white areas over objects to remove
|
| 129 |
+
|
| 130 |
+
### Tips
|
| 131 |
+
- Use high contrast (white on black) for best results
|
| 132 |
+
- Make sure mask areas are clearly defined
|
| 133 |
+
- Avoid thin lines or small details in the mask
|
| 134 |
+
|
| 135 |
+
## Error Handling
|
| 136 |
+
|
| 137 |
+
### Common Errors
|
| 138 |
+
|
| 139 |
+
**422 Unprocessable Entity**
|
| 140 |
+
- **Cause:** Missing required fields or invalid file format
|
| 141 |
+
- **Solution:** Check file upload and field names
|
| 142 |
+
|
| 143 |
+
**404 Not Found**
|
| 144 |
+
- **Cause:** Invalid image_id or mask_id
|
| 145 |
+
- **Solution:** Re-upload files to get fresh IDs
|
| 146 |
+
|
| 147 |
+
**401 Unauthorized**
|
| 148 |
+
- **Cause:** Missing or invalid API token
|
| 149 |
+
- **Solution:** Add correct Authorization header
|
| 150 |
+
|
| 151 |
+
**403 Forbidden**
|
| 152 |
+
- **Cause:** Wrong API token
|
| 153 |
+
- **Solution:** Use the correct token you configured
|
| 154 |
+
|
| 155 |
+
### Troubleshooting
|
| 156 |
+
|
| 157 |
+
**File Upload Issues in Postman:**
|
| 158 |
+
- Remove warning icons next to filenames
|
| 159 |
+
- Re-select files if warning appears
|
| 160 |
+
- Try different file formats (PNG, JPG)
|
| 161 |
+
- Check file isn't corrupted
|
| 162 |
+
|
| 163 |
+
**Slow Processing:**
|
| 164 |
+
- Large images take longer to process
|
| 165 |
+
- Typical processing time: 1-3 seconds
|
| 166 |
+
- Wait for completion before checking results
|
| 167 |
+
|
| 168 |
+
## Complete Workflow Example
|
| 169 |
+
|
| 170 |
+
```bash
|
| 171 |
+
# 1. Health check
|
| 172 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 173 |
+
https://logicgoinfotechspaces-object-remover.hf.space/health
|
| 174 |
+
|
| 175 |
+
# 2. Upload image
|
| 176 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 177 |
+
-F [email protected] \
|
| 178 |
+
https://logicgoinfotechspaces-object-remover.hf.space/upload-image
|
| 179 |
+
|
| 180 |
+
# 3. Upload mask
|
| 181 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 182 |
+
-F [email protected] \
|
| 183 |
+
https://logicgoinfotechspaces-object-remover.hf.space/upload-mask
|
| 184 |
+
|
| 185 |
+
# 4. Process (replace with actual IDs from steps 2&3)
|
| 186 |
+
curl -H "Authorization: Bearer <API_TOKEN>" \
|
| 187 |
+
-H "Content-Type: application/json" \
|
| 188 |
+
-d '{"image_id":"IMAGE_ID","mask_id":"MASK_ID"}' \
|
| 189 |
+
https://logicgoinfotechspaces-object-remover.hf.space/inpaint
|
| 190 |
+
|
| 191 |
+
# 5. View result (replace with actual filename from step 4)
|
| 192 |
+
# Open in browser: https://logicgoinfotechspaces-object-remover.hf.space/result/OUTPUT_FILENAME
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
## API Endpoints Summary
|
| 196 |
+
|
| 197 |
+
| Method | Endpoint | Description | Auth Required |
|
| 198 |
+
|--------|----------|-------------|---------------|
|
| 199 |
+
| GET | `/health` | Health check | No |
|
| 200 |
+
| GET | `/` | API information | No |
|
| 201 |
+
| POST | `/upload-image` | Upload image file | Optional |
|
| 202 |
+
| POST | `/upload-mask` | Upload mask file | Optional |
|
| 203 |
+
| POST | `/inpaint` | Process with IDs | Optional |
|
| 204 |
+
| POST | `/inpaint-url` | Process with URL response | Optional |
|
| 205 |
+
| POST | `/inpaint-multipart` | One-step processing | Optional |
|
| 206 |
+
| GET | `/download/{filename}` | Download result | No |
|
| 207 |
+
| GET | `/result/{filename}` | View result in browser | No |
|
| 208 |
+
| GET | `/logs` | View processing logs | Optional |
|
| 209 |
+
| GET | `/docs` | API documentation | No |
|
| 210 |
+
|
| 211 |
+
## Support
|
| 212 |
+
|
| 213 |
+
For issues or questions:
|
| 214 |
+
1. Check the API documentation at `/docs`
|
| 215 |
+
2. Verify your request format matches examples
|
| 216 |
+
3. Ensure files are properly uploaded
|
| 217 |
+
4. Check authentication token is correct
|
app.py
CHANGED
|
@@ -82,6 +82,7 @@ def run_streamlit_ui():
|
|
| 82 |
img_input = img_input.resize((new_width, new_height))
|
| 83 |
|
| 84 |
stroke_width = st.slider("Brush size", 1, 100, 50)
|
|
|
|
| 85 |
|
| 86 |
st.write("**Now draw (brush) the part of image that you want to remove.**")
|
| 87 |
|
|
@@ -121,11 +122,17 @@ def run_streamlit_ui():
|
|
| 121 |
|
| 122 |
if st.button('Submit'):
|
| 123 |
with st.spinner("AI is doing the magic!"):
|
| 124 |
-
output = process_inpaint(np.array(img_input), np.array(im))
|
| 125 |
img_output = Image.fromarray(output).convert("RGB")
|
| 126 |
|
| 127 |
st.write("AI has finished the job!")
|
| 128 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
| 131 |
image_download_button(
|
|
|
|
| 82 |
img_input = img_input.resize((new_width, new_height))
|
| 83 |
|
| 84 |
stroke_width = st.slider("Brush size", 1, 100, 50)
|
| 85 |
+
invert_mask = st.checkbox("Painted area = remove", value=True)
|
| 86 |
|
| 87 |
st.write("**Now draw (brush) the part of image that you want to remove.**")
|
| 88 |
|
|
|
|
| 122 |
|
| 123 |
if st.button('Submit'):
|
| 124 |
with st.spinner("AI is doing the magic!"):
|
| 125 |
+
output = process_inpaint(np.array(img_input), np.array(im), invert_mask=invert_mask)
|
| 126 |
img_output = Image.fromarray(output).convert("RGB")
|
| 127 |
|
| 128 |
st.write("AI has finished the job!")
|
| 129 |
+
col1, col2 = st.columns(2)
|
| 130 |
+
with col1:
|
| 131 |
+
st.caption("Before")
|
| 132 |
+
st.image(img_input.convert("RGB"))
|
| 133 |
+
with col2:
|
| 134 |
+
st.caption("After")
|
| 135 |
+
st.image(img_output)
|
| 136 |
|
| 137 |
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
| 138 |
image_download_button(
|
src/core.py
CHANGED
|
@@ -442,7 +442,7 @@ def get_args_parser():
|
|
| 442 |
return parser.parse_args()
|
| 443 |
|
| 444 |
|
| 445 |
-
def process_inpaint(image, mask):
|
| 446 |
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 447 |
original_shape = image.shape
|
| 448 |
interpolation = cv2.INTER_CUBIC
|
|
@@ -458,7 +458,10 @@ def process_inpaint(image, mask):
|
|
| 458 |
print(f"Resized image shape: {image.shape}")
|
| 459 |
image = norm_img(image)
|
| 460 |
|
| 461 |
-
mask
|
|
|
|
|
|
|
|
|
|
| 462 |
mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
|
| 463 |
mask = norm_img(mask)
|
| 464 |
|
|
|
|
| 442 |
return parser.parse_args()
|
| 443 |
|
| 444 |
|
| 445 |
+
def process_inpaint(image, mask, invert_mask=True):
|
| 446 |
image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 447 |
original_shape = image.shape
|
| 448 |
interpolation = cv2.INTER_CUBIC
|
|
|
|
| 458 |
print(f"Resized image shape: {image.shape}")
|
| 459 |
image = norm_img(image)
|
| 460 |
|
| 461 |
+
# Convert RGBA mask to single-channel mask.
|
| 462 |
+
# When invert_mask=True (default), areas the user paints (alpha=0) become 255 (to remove).
|
| 463 |
+
alpha_channel = mask[:,:,3]
|
| 464 |
+
mask = (255 - alpha_channel) if invert_mask else alpha_channel
|
| 465 |
mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
|
| 466 |
mask = norm_img(mask)
|
| 467 |
|