Fix: Add missing imports and URL image loading in example code
#53
by
anon-repair-bot
- opened
README.md
CHANGED
|
@@ -124,6 +124,13 @@ pillow_image = pipe(image_path) # applies mask on input and returns a pillow ima
|
|
| 124 |
|
| 125 |
Or load the model
|
| 126 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
from transformers import AutoModelForImageSegmentation
|
| 128 |
from torchvision.transforms.functional import normalize
|
| 129 |
model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4",trust_remote_code=True)
|
|
@@ -153,6 +160,7 @@ model.to(device)
|
|
| 153 |
image_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"
|
| 154 |
orig_im = io.imread(image_path)
|
| 155 |
orig_im_size = orig_im.shape[0:2]
|
|
|
|
| 156 |
image = preprocess_image(orig_im, model_input_size).to(device)
|
| 157 |
|
| 158 |
# inference
|
|
@@ -163,7 +171,8 @@ result_image = postprocess_image(result[0][0], orig_im_size)
|
|
| 163 |
|
| 164 |
# save result
|
| 165 |
pil_mask_im = Image.fromarray(result_image)
|
| 166 |
-
|
|
|
|
| 167 |
no_bg_image = orig_image.copy()
|
| 168 |
no_bg_image.putalpha(pil_mask_im)
|
| 169 |
```
|
|
|
|
| 124 |
|
| 125 |
Or load the model
|
| 126 |
```python
|
| 127 |
+
import numpy as np
|
| 128 |
+
import torch
|
| 129 |
+
import torch.nn.functional as F
|
| 130 |
+
import requests
|
| 131 |
+
from io import BytesIO
|
| 132 |
+
from PIL import Image
|
| 133 |
+
from skimage import io
|
| 134 |
from transformers import AutoModelForImageSegmentation
|
| 135 |
from torchvision.transforms.functional import normalize
|
| 136 |
model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4",trust_remote_code=True)
|
|
|
|
| 160 |
image_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"
|
| 161 |
orig_im = io.imread(image_path)
|
| 162 |
orig_im_size = orig_im.shape[0:2]
|
| 163 |
+
model_input_size = [1024, 1024]
|
| 164 |
image = preprocess_image(orig_im, model_input_size).to(device)
|
| 165 |
|
| 166 |
# inference
|
|
|
|
| 171 |
|
| 172 |
# save result
|
| 173 |
pil_mask_im = Image.fromarray(result_image)
|
| 174 |
+
response = requests.get(image_path)
|
| 175 |
+
orig_image = Image.open(BytesIO(response.content))
|
| 176 |
no_bg_image = orig_image.copy()
|
| 177 |
no_bg_image.putalpha(pil_mask_im)
|
| 178 |
```
|