- handler.py +66 -0
- handler_test.py +13 -0
- test.py +57 -0
handler.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import torch
|
| 4 |
+
import requests
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
just_get_sd_mask_function = None
|
| 9 |
+
|
| 10 |
+
print(os.listdir('/usr/local/'))
|
| 11 |
+
print(torch.version.cuda)
|
| 12 |
+
|
| 13 |
+
class EndpointHandler():
|
| 14 |
+
def __init__(self, path="."):
|
| 15 |
+
global just_get_sd_mask_function
|
| 16 |
+
|
| 17 |
+
is_production = True
|
| 18 |
+
|
| 19 |
+
if False:
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
os.chdir(path)
|
| 23 |
+
|
| 24 |
+
os.environ['AM_I_DOCKER'] = 'False'
|
| 25 |
+
os.environ['BUILD_WITH_CUDA'] = 'True'
|
| 26 |
+
os.environ['CUDA_HOME'] = '/usr/local/cuda-11.7/' if is_production else '/usr/local/cuda-12.1/'
|
| 27 |
+
|
| 28 |
+
# Install Segment Anything
|
| 29 |
+
subprocess.run(["python", "-m", "pip", "install", "-e", "segment_anything"])
|
| 30 |
+
|
| 31 |
+
# Install Grounding DINO
|
| 32 |
+
subprocess.run(["python", "-m", "pip", "install", "-e", "GroundingDINO"])
|
| 33 |
+
|
| 34 |
+
subprocess.run("wget https://huggingface.co/Uminosachi/sam-hq/resolve/main/sam_hq_vit_h.pth -O ./sam_hq_vit_h.pth", shell=True)
|
| 35 |
+
|
| 36 |
+
# Install diffusers
|
| 37 |
+
subprocess.run(["pip", "install", "--upgrade", "diffusers[torch]"])
|
| 38 |
+
|
| 39 |
+
# Install osx
|
| 40 |
+
subprocess.run(["git", "submodule", "update", "--init", "--recursive"])
|
| 41 |
+
subprocess.run(["bash", "grounded-sam-osx/install.sh"], cwd="grounded-sam-osx")
|
| 42 |
+
|
| 43 |
+
# Install RAM & Tag2Text
|
| 44 |
+
subprocess.run(["git", "clone", "https://github.com/xinyu1205/recognize-anything.git"])
|
| 45 |
+
subprocess.run(["pip", "install", "-r", "./recognize-anything/requirements.txt"])
|
| 46 |
+
subprocess.run(["pip", "install", "-e", "./recognize-anything/"])
|
| 47 |
+
|
| 48 |
+
from test import just_get_sd_mask
|
| 49 |
+
just_get_sd_mask_function = just_get_sd_mask
|
| 50 |
+
|
| 51 |
+
def __call__(self, data):
|
| 52 |
+
mask_pil = just_get_sd_mask(Image.open("assets/demo1.jpg"), "bear", 10)
|
| 53 |
+
|
| 54 |
+
if mask_pil.mode != 'RGB':
|
| 55 |
+
mask_pil = mask_pil.convert('RGB')
|
| 56 |
+
|
| 57 |
+
# Convert PIL image to byte array
|
| 58 |
+
img_byte_arr = BytesIO()
|
| 59 |
+
mask_pil.save(img_byte_arr, format='JPEG')
|
| 60 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 61 |
+
|
| 62 |
+
# Upload to file.io
|
| 63 |
+
response = requests.post("https://file.io/", files={"file": img_byte_arr})
|
| 64 |
+
url = response.json().get('link')
|
| 65 |
+
|
| 66 |
+
return {"url": url}
|
handler_test.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from handler import EndpointHandler
|
| 2 |
+
|
| 3 |
+
# init handler
|
| 4 |
+
my_handler = EndpointHandler(path=".")
|
| 5 |
+
|
| 6 |
+
# prepare sample payload
|
| 7 |
+
non_holiday_payload = {"inputs": "I am quite excited how this will turn out", "date": "2022-08-08"}
|
| 8 |
+
|
| 9 |
+
# test the handler
|
| 10 |
+
non_holiday_pred=my_handler(non_holiday_payload)
|
| 11 |
+
|
| 12 |
+
# show results
|
| 13 |
+
print("non_holiday_pred", non_holiday_pred)
|
test.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from grounded_sam_demo import grounded_sam_demo
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from scipy.ndimage import convolve
|
| 5 |
+
from scipy.ndimage import binary_dilation
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def get_sd_mask(color_mask_pil, target=(72, 4, 84), tolerance=50):
|
| 9 |
+
image_array = np.array(color_mask_pil)
|
| 10 |
+
|
| 11 |
+
# Update target based on the number of color channels in the image array
|
| 12 |
+
target = np.array(list(target) + [255] *
|
| 13 |
+
(image_array.shape[-1] - len(target)))
|
| 14 |
+
|
| 15 |
+
mask = np.abs(image_array - target) <= tolerance
|
| 16 |
+
mask = np.all(mask, axis=-1)
|
| 17 |
+
|
| 18 |
+
new_image_array = np.ones_like(image_array) * 255 # Start with white
|
| 19 |
+
# Apply black where condition met
|
| 20 |
+
new_image_array[mask] = [0] * image_array.shape[-1]
|
| 21 |
+
|
| 22 |
+
return Image.fromarray(new_image_array)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def expand_white_pixels(input_pil, expand_by=1):
|
| 26 |
+
img_array = np.array(input_pil)
|
| 27 |
+
is_white = np.all(img_array == 255, axis=-1)
|
| 28 |
+
|
| 29 |
+
kernel = np.ones((2*expand_by+1, 2*expand_by+1), bool)
|
| 30 |
+
expanded_white = binary_dilation(is_white, structure=kernel)
|
| 31 |
+
|
| 32 |
+
expanded_array = np.where(expanded_white[..., None], 255, img_array)
|
| 33 |
+
|
| 34 |
+
expanded_pil = Image.fromarray(expanded_array.astype('uint8'))
|
| 35 |
+
return expanded_pil
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
config_file = "GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py"
|
| 39 |
+
grounded_checkpoint = "groundingdino_swint_ogc.pth"
|
| 40 |
+
sam_checkpoint = "sam_hq_vit_h.pth"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def just_get_sd_mask(input_pil, text_prompt, padding):
|
| 44 |
+
print("Doing sam")
|
| 45 |
+
|
| 46 |
+
colored_mask_pil = grounded_sam_demo(
|
| 47 |
+
input_pil, config_file, grounded_checkpoint, sam_checkpoint, text_prompt)
|
| 48 |
+
|
| 49 |
+
print("doing to white")
|
| 50 |
+
|
| 51 |
+
sd_mask_pil = get_sd_mask(colored_mask_pil)
|
| 52 |
+
|
| 53 |
+
print("expanding white pixels")
|
| 54 |
+
|
| 55 |
+
sd_mask_withpadding_pil = expand_white_pixels(sd_mask_pil, padding)
|
| 56 |
+
|
| 57 |
+
return sd_mask_withpadding_pil
|