File size: 2,426 Bytes
486a633
 
 
4d86ff9
91076c2
0da98c7
 
 
486a633
 
 
 
4d86ff9
486a633
 
 
 
 
 
 
 
 
 
 
4e29dfc
e166f1a
b4c575d
 
c3c28b7
e926e4b
2a4ff9f
2b7ee3a
486a633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b117a85
486a633
 
2b7ee3a
486a633
 
 
 
2b7ee3a
 
 
 
 
 
486a633
2b7ee3a
 
58bfce0
a5e0922
227e33d
486a633
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr

import os
os.system('pip install torch -q')
os.system('pip install -U scikit-learn -q')
os.system('pip install torchvision -q')
os.system('python -m pip install scipy -q')


import sys
import numpy as np
from PIL import Image
import torch

from codes import *

## Print samples
dino_model = torch.hub.load(repo_or_dir="facebookresearch/dinov2", model="dinov2_vits14")
for param in dino_model.parameters():
    param.requires_grad = False
dino_model.eval()


def dino_seg(img1,img2, model_selected):
    img_path_list = [img1, img2]
    stackedtokens_, stack_image_batch, grid_size = extract_dino_features(img_path_list,dino_model, smaller_edge_size=448)
    projections, standard_array = get_projections_and_standardarray(stackedtokens_)
    masks = get_masks(projections, grid_size,background_threshold = 0.0, apply_opening = False, apply_closing = False)
    seg_img1 = render_patch_pca3(stackedtokens_[0], standard_array, grid_size)
    #seg_img1out = Image.fromarray(seg_img1.astype(np.uint8))
    seg_img2 = render_patch_pca3(stackedtokens_[1], standard_array, grid_size)
    return [seg_img1.resize((200, 200), 0),seg_img2.resize((200, 200), 0)]



with gr.Blocks() as demo:
    
    image_1 = gr.Image(
            label = "Fixed Image",
            source = "upload", 
            type = "filepath",
            elem_id = "image-in",
        )
    image_2 = gr.Image(
            label = "Moving Image",
            source = "upload", 
            type = "filepath",
            elem_id = "image-in",
        )
    
    model_list = gr.Dropdown(
            ["small", "small"], label="Model", info="select a model"
        )
    
    out_image1 = gr.Image(placeholder='Output', label = "seg image",
            #source = "upload", 
            #type = "filepath",
            elem_id = "image-out"
        )
    out_image2 = gr.Image(placeholder='Output', label = "seg image",
            #source = "upload", 
            #type = "filepath",
            elem_id = "image-out"
        )
    
    inputs = [image_1, image_2, model_list]
    outputs = [out_image1, out_image2]
    iface = gr.Interface(fn=dino_seg, inputs=inputs,outputs=outputs,
    title="Foreground background seperation",
    description="Upload 2 images to generate a similarity map:",
    examples=[["./examples/ex3.jpg","./examples/ex2.png"]],
                         )

demo.queue(default_enabled = True).launch(debug = True)