Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
os.system('pip install voxelmorph -q')
|
| 6 |
+
os.system('pip install tensorflow -q')
|
| 7 |
+
|
| 8 |
+
import sys
|
| 9 |
+
import numpy as np
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
from PIL import Image
|
| 12 |
+
|
| 13 |
+
from codes import *
|
| 14 |
+
|
| 15 |
+
## Print samples
|
| 16 |
+
vxm_model_loaded = create_model(dim = 128)
|
| 17 |
+
vxm_model_loaded.load_weights('./modelbest/')
|
| 18 |
+
|
| 19 |
+
vxm_model_loaded_affine = create_model(dim = 128)
|
| 20 |
+
vxm_model_loaded_affine.load_weights('./modelbest/')
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def imgnt_reg(img1,img2, model_selected):
|
| 25 |
+
fixed_images = np.empty((1, 128, 128, 3))
|
| 26 |
+
moving_images = np.empty((1, 128, 128, 3))
|
| 27 |
+
# prepare inputs:
|
| 28 |
+
fixed_images[0] = preprocess_image(img1, dim = 128)
|
| 29 |
+
moving_images[0] = preprocess_image(img2, dim = 128)
|
| 30 |
+
|
| 31 |
+
model_inputs = [moving_images/255, fixed_images/255]
|
| 32 |
+
if model_selected == "Imagenet-wild":
|
| 33 |
+
Y_predict = vxm_model_loaded(model_inputs)
|
| 34 |
+
else:
|
| 35 |
+
Y_predict = vxm_model_loaded_affine(model_inputs)
|
| 36 |
+
img_out = Y_predict[0][0].numpy()*255
|
| 37 |
+
registered_img = Image.fromarray(img_out.astype(np.uint8))
|
| 38 |
+
return registered_img
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
#gr.Markdown("<h1><center> Message Encryption</center></h1>")
|
| 44 |
+
#gr.Markdown("<center> Encrypt your message and let your friends decrypt it on the same day.</center>")
|
| 45 |
+
|
| 46 |
+
image_1 = gr.Image(
|
| 47 |
+
label = "Fixed Image",
|
| 48 |
+
source = "upload",
|
| 49 |
+
type = "filepath",
|
| 50 |
+
elem_id = "image-in",
|
| 51 |
+
)
|
| 52 |
+
image_2 = gr.Image(
|
| 53 |
+
label = "Moving Image",
|
| 54 |
+
source = "upload",
|
| 55 |
+
type = "filepath",
|
| 56 |
+
elem_id = "image-in",
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
model_list = gr.Dropdown(
|
| 60 |
+
["Imagenet-wild", "Imagenet-affine"], label="Model", info="select a model"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
out_image = gr.Image(placeholder='Output', label = "ٌRegistered image",
|
| 64 |
+
#source = "upload",
|
| 65 |
+
#type = "filepath",
|
| 66 |
+
elem_id = "image-out"
|
| 67 |
+
)
|
| 68 |
+
inputs = [image_1, image_2, model_list]
|
| 69 |
+
iface = gr.Interface(fn=imgnt_reg, inputs=inputs,outputs=out_image,
|
| 70 |
+
title="Imagenet registration V1",
|
| 71 |
+
description="Upload 2 images to generate a registered one:",
|
| 72 |
+
examples=[["./examples/ex1.jpg","./examples/ex2.jpg"]],
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
demo.queue(default_enabled = True).launch(debug = True)
|