Spaces:
Sleeping
Sleeping
Add picture preprocessing
Browse files
app.py
CHANGED
|
@@ -1,39 +1,68 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn.functional as F
|
| 3 |
-
from transformers import
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
|
|
|
| 6 |
|
|
|
|
| 7 |
processor = AutoImageProcessor.from_pretrained("nomic-ai/nomic-embed-vision-v1.5")
|
| 8 |
vision_model = AutoModel.from_pretrained("nomic-ai/nomic-embed-vision-v1.5", trust_remote_code=True)
|
| 9 |
|
| 10 |
-
def
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
|
| 14 |
Args:
|
| 15 |
-
image (PIL.Image.Image
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
-
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
img_emb = vision_model(**inputs).last_hidden_state
|
| 24 |
img_embeddings = F.normalize(img_emb[:, 0], p=2, dim=1)
|
| 25 |
|
| 26 |
-
return img_embeddings[0].tolist()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
-
with gr.Blocks() as demo:
|
| 31 |
-
img = gr.Image();
|
| 32 |
-
out = gr.Text();
|
| 33 |
-
|
| 34 |
-
btn = gr.Button("Get Embeddings")
|
| 35 |
-
btn.click(ImgEmbed, [img], [out])
|
| 36 |
-
|
| 37 |
-
|
| 38 |
if __name__ == "__main__":
|
| 39 |
-
demo.launch(
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn.functional as F
|
| 3 |
+
from transformers import AutoModel, AutoImageProcessor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from rembg import remove
|
| 6 |
import gradio as gr
|
| 7 |
import spaces
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
+
# Load the Nomic embed model
|
| 11 |
processor = AutoImageProcessor.from_pretrained("nomic-ai/nomic-embed-vision-v1.5")
|
| 12 |
vision_model = AutoModel.from_pretrained("nomic-ai/nomic-embed-vision-v1.5", trust_remote_code=True)
|
| 13 |
|
| 14 |
+
def focus_on_subject(image: Image.Image) -> Image.Image:
|
| 15 |
"""
|
| 16 |
+
Remove background and crop to the main object using rembg.
|
| 17 |
|
| 18 |
Args:
|
| 19 |
+
image (PIL.Image.Image): Input image.
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
+
PIL.Image.Image: Cropped image with background removed.
|
| 23 |
"""
|
| 24 |
+
image = image.convert("RGB")
|
| 25 |
+
|
| 26 |
+
# Remove background
|
| 27 |
+
img_bytes = io.BytesIO()
|
| 28 |
+
image.save(img_bytes, format="PNG")
|
| 29 |
+
img_bytes = img_bytes.getvalue()
|
| 30 |
+
result_bytes = remove(img_bytes)
|
| 31 |
+
|
| 32 |
+
result_image = Image.open(io.BytesIO(result_bytes)).convert("RGBA")
|
| 33 |
+
bbox = result_image.getbbox()
|
| 34 |
+
cropped = result_image.crop(bbox) if bbox else result_image
|
| 35 |
+
|
| 36 |
+
return cropped.convert("RGB")
|
| 37 |
|
| 38 |
+
def ImgEmbed(image: Image.Image):
|
| 39 |
+
"""
|
| 40 |
+
Preprocess image, generate normalized embedding, and return both embedding and processed image.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
image (PIL.Image.Image): Input image.
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
Tuple: (embedding list, processed image)
|
| 47 |
+
"""
|
| 48 |
+
focused_image = focus_on_subject(image)
|
| 49 |
+
inputs = processor(focused_image, return_tensors="pt")
|
| 50 |
img_emb = vision_model(**inputs).last_hidden_state
|
| 51 |
img_embeddings = F.normalize(img_emb[:, 0], p=2, dim=1)
|
| 52 |
|
| 53 |
+
return img_embeddings[0].tolist(), focused_image
|
| 54 |
|
| 55 |
+
# Gradio UI
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
img = gr.Image(label="Upload Image")
|
| 60 |
+
btn = gr.Button("Get Embeddings")
|
| 61 |
+
with gr.Column():
|
| 62 |
+
pre_img = gr.Image(label="Preprocessed Image")
|
| 63 |
+
out = gr.Text(label="Image Embedding")
|
| 64 |
|
| 65 |
+
btn.click(ImgEmbed, inputs=[img], outputs=[out, pre_img])
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|