Update app.py
Browse files
app.py
CHANGED
|
@@ -24,34 +24,25 @@ def process_image(image, prompt, threshold, alpha_value, draw_rectangles):
|
|
| 24 |
preds = outputs.logits
|
| 25 |
|
| 26 |
pred = torch.sigmoid(preds)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
mat = pred[0].cpu().numpy() # If the shape is (channels, height, width)
|
| 32 |
-
|
| 33 |
-
mask = Image.fromarray(np.uint8(mat * 255), "L") # Convert to PIL Image
|
| 34 |
-
|
| 35 |
-
# Convert the mask to a NumPy array for calculation
|
| 36 |
-
mask_array = np.array(mask)
|
| 37 |
|
| 38 |
# normalize the mask
|
| 39 |
-
mask_min =
|
| 40 |
-
mask_max =
|
| 41 |
-
|
| 42 |
|
| 43 |
# threshold the mask
|
| 44 |
-
bmask =
|
| 45 |
# zero out values below the threshold
|
| 46 |
-
|
| 47 |
|
| 48 |
-
bmask = Image.fromarray(
|
| 49 |
|
| 50 |
return bmask
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
@app.route('/')
|
| 56 |
def index():
|
| 57 |
return "Hello, World! clipseg2"
|
|
|
|
| 24 |
preds = outputs.logits
|
| 25 |
|
| 26 |
pred = torch.sigmoid(preds)
|
| 27 |
+
mat = pred.cpu().numpy()
|
| 28 |
+
mask = Image.fromarray(np.uint8(mat * 255), "L")
|
| 29 |
+
mask = mask.resize(image.size)
|
| 30 |
+
mask = np.array(mask)[:, :, 0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# normalize the mask
|
| 33 |
+
mask_min = mask.min()
|
| 34 |
+
mask_max = mask.max()
|
| 35 |
+
mask = (mask - mask_min) / (mask_max - mask_min)
|
| 36 |
|
| 37 |
# threshold the mask
|
| 38 |
+
bmask = mask > threshold
|
| 39 |
# zero out values below the threshold
|
| 40 |
+
mask[mask < threshold] = 0
|
| 41 |
|
| 42 |
+
bmask = Image.fromarray(bmask.astype(np.uint8) * 255, "L")
|
| 43 |
|
| 44 |
return bmask
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.route('/')
|
| 47 |
def index():
|
| 48 |
return "Hello, World! clipseg2"
|