JuanLozada97
commited on
Commit
•
a413c26
1
Parent(s):
57ec1a2
Upload app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ from typing import Tuple, Dict
|
|
8 |
from timeit import default_timer as timer
|
9 |
from skimage import io, transform
|
10 |
import os
|
|
|
|
|
11 |
|
12 |
import torch.nn.functional as F
|
13 |
|
@@ -104,7 +106,7 @@ def predict(img) -> Tuple[Dict, float]:
|
|
104 |
with torch.inference_mode():
|
105 |
image_embedding = medsam_model.image_encoder(img_1024_tensor) # (1, 256, 64, 64)
|
106 |
# define the inputbox
|
107 |
-
input_box = np.array([[
|
108 |
# transfer box_np t0 1024x1024 scale
|
109 |
box_1024 = input_box / np.array([W, H, W, H]) * 1024
|
110 |
|
@@ -120,10 +122,13 @@ def predict(img) -> Tuple[Dict, float]:
|
|
120 |
show_box(input_box[0], ax[1])
|
121 |
ax[1].set_title("MedSAM Segmentation")
|
122 |
# Calculate the prediction time
|
123 |
-
|
|
|
|
|
|
|
124 |
|
125 |
# Return the prediction dictionary and prediction time
|
126 |
-
return fig, pred_time
|
127 |
|
128 |
# 4. Gradio app
|
129 |
# Create title, description and article strings
|
@@ -138,7 +143,8 @@ example_list = [["examples/" + example] for example in os.listdir("examples")]
|
|
138 |
demo = gr.Interface(fn=predict, # mapping function from input to output
|
139 |
inputs=gr.Image(type="pil"), # what are the inputs?
|
140 |
outputs=[gr.Plot(label="Predictions"), # what are the outputs?
|
141 |
-
gr.Number(label="Prediction time (s)")
|
|
|
142 |
examples=example_list,
|
143 |
title=title,
|
144 |
description=description,
|
|
|
8 |
from timeit import default_timer as timer
|
9 |
from skimage import io, transform
|
10 |
import os
|
11 |
+
import base64
|
12 |
+
import json
|
13 |
|
14 |
import torch.nn.functional as F
|
15 |
|
|
|
106 |
with torch.inference_mode():
|
107 |
image_embedding = medsam_model.image_encoder(img_1024_tensor) # (1, 256, 64, 64)
|
108 |
# define the inputbox
|
109 |
+
input_box = np.array([[125, 275, 190, 350]])
|
110 |
# transfer box_np t0 1024x1024 scale
|
111 |
box_1024 = input_box / np.array([W, H, W, H]) * 1024
|
112 |
|
|
|
122 |
show_box(input_box[0], ax[1])
|
123 |
ax[1].set_title("MedSAM Segmentation")
|
124 |
# Calculate the prediction time
|
125 |
+
image_embedding = image_embedding.cpu().numpy().tobytes()
|
126 |
+
|
127 |
+
# Serialize the response data to JSON format
|
128 |
+
serialized_data = json.dumps([base64.b64encode(image_embedding).decode('ascii')])
|
129 |
|
130 |
# Return the prediction dictionary and prediction time
|
131 |
+
return fig, pred_time,serialized_data
|
132 |
|
133 |
# 4. Gradio app
|
134 |
# Create title, description and article strings
|
|
|
143 |
demo = gr.Interface(fn=predict, # mapping function from input to output
|
144 |
inputs=gr.Image(type="pil"), # what are the inputs?
|
145 |
outputs=[gr.Plot(label="Predictions"), # what are the outputs?
|
146 |
+
gr.Number(label="Prediction time (s)"),
|
147 |
+
gr.JSON(label="Embedding Image")], # our fn has two outputs, therefore we have two outputs
|
148 |
examples=example_list,
|
149 |
title=title,
|
150 |
description=description,
|