Skip to content
Snippets Groups Projects
Commit a1f9598c authored by Felipe Delestro Matos's avatar Felipe Delestro Matos
Browse files

Hotfix for annotation tool

parent c29d9908
Branches
Tags
No related merge requests found
import tifffile import tifffile
import tempfile
import os import os
import time import time
import getpass import getpass
...@@ -16,8 +17,7 @@ class Session: ...@@ -16,8 +17,7 @@ class Session:
self.masks_rgb = None self.masks_rgb = None
self.mask_names = {0: "red", 1: "green", 2: "blue"} self.mask_names = {0: "red", 1: "green", 2: "blue"}
self.temp_files = [] self.temp_files = []
self.gradio_temp = None self.temp_dir = None
self.username = getpass.getuser()
class Interface: class Interface:
...@@ -27,6 +27,7 @@ class Interface: ...@@ -27,6 +27,7 @@ class Interface:
self.height = 768 self.height = 768
self.interface = None self.interface = None
self.username = getpass.getuser() self.username = getpass.getuser()
self.temp_dir = os.path.join(tempfile.gettempdir(), f"qim-{self.username}")
# CSS path # CSS path
current_dir = os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.dirname(os.path.abspath(__file__))
...@@ -34,9 +35,8 @@ class Interface: ...@@ -34,9 +35,8 @@ class Interface:
def launch(self, img=None, **kwargs): def launch(self, img=None, **kwargs):
# Create gradio interfaces # Create gradio interfaces
# img = "/tmp/qim-fima/2dimage.png"
self.interface = self.create_interface(img) self.interface = self.create_interface(img)
self.gradio_temp = self.interface.GRADIO_CACHE
# Set gradio verbose level # Set gradio verbose level
if self.verbose: if self.verbose:
...@@ -55,12 +55,11 @@ class Interface: ...@@ -55,12 +55,11 @@ class Interface:
def get_result(self): def get_result(self):
# Get the temporary files from gradio # Get the temporary files from gradio
base = os.path.join(self.gradio_temp, "qim3d", self.username)
temp_path_list = [] temp_path_list = []
for filename in os.listdir(base): for filename in os.listdir(self.temp_dir):
if "mask" in str(filename): if "mask" in str(filename):
# Get the list of the temporary files # Get the list of the temporary files
temp_path_list.append(os.path.join(base, filename)) temp_path_list.append(os.path.join(self.temp_dir, filename))
# Make dictionary of maks # Make dictionary of maks
masks = {} masks = {}
...@@ -81,7 +80,7 @@ class Interface: ...@@ -81,7 +80,7 @@ class Interface:
else: else:
custom_css = "annotation-tool no-img" custom_css = "annotation-tool no-img"
with gr.Blocks(css=self.css_path) as gradio_interface: with gr.Blocks(css=self.css_path, title=self.title) as gradio_interface:
brush = gr.Brush( brush = gr.Brush(
colors=[ colors=[
...@@ -126,13 +125,13 @@ class Interface: ...@@ -126,13 +125,13 @@ class Interface:
elem_classes=custom_css, elem_classes=custom_css,
) )
temp_path = gr.Textbox(value=gradio_interface.GRADIO_CACHE, visible=False) temp_dir = gr.Textbox(value=self.temp_dir, visible=False)
session = gr.State([]) session = gr.State([])
inputs = [img_editor] inputs = [img_editor]
operations = Operations() operations = Operations()
# fmt: off # fmt: off
btn_update.click( btn_update.click(
fn=operations.start_session, inputs=[img_editor,temp_path] , outputs=session).then( fn=operations.start_session, inputs=[img_editor,temp_dir] , outputs=session).then(
fn=operations.preview, inputs=session, outputs=overlay_img).then( fn=operations.preview, inputs=session, outputs=overlay_img).then(
fn=self.set_visible, inputs=None, outputs=overlay_img).then( fn=self.set_visible, inputs=None, outputs=overlay_img).then(
fn=operations.separate_masks, inputs=session, outputs=[session, masks_download]).then( fn=operations.separate_masks, inputs=session, outputs=[session, masks_download]).then(
...@@ -147,17 +146,15 @@ class Operations: ...@@ -147,17 +146,15 @@ class Operations:
def start_session(self, *args): def start_session(self, *args):
session = Session() session = Session()
session.img_editor = args[0] session.img_editor = args[0]
session.gradio_temp = args[1] session.temp_dir = args[1]
# Clean temp files
base = os.path.join(session.gradio_temp, "qim3d", session.username)
# Clean up old files
try: try:
files = os.listdir(base) files = os.listdir(session.temp_dir)
for filename in files: for filename in files:
# Check if "mask" is in the filename # Check if "mask" is in the filename
if "mask" in filename: if "mask" in filename:
file_path = os.path.join(base, filename) file_path = os.path.join(session.temp_dir, filename)
os.remove(file_path) os.remove(file_path)
except FileNotFoundError: except FileNotFoundError:
...@@ -235,10 +232,9 @@ class Operations: ...@@ -235,10 +232,9 @@ class Operations:
if np.sum(mask) > 0: if np.sum(mask) > 0:
mask_list.append(mask) mask_list.append(mask)
filename = f"mask_{session.mask_names[idx]}.tif" filename = f"mask_{session.mask_names[idx]}.tif"
base = os.path.join(session.gradio_temp, "qim3d", session.username) if not os.path.exists(session.temp_dir):
if not os.path.exists(base): os.makedirs(session.temp_dir)
os.makedirs(base) filepath = os.path.join(session.temp_dir, filename)
filepath = os.path.join(base, filename)
files_list.append(filepath) files_list.append(filepath)
save(filepath, mask, replace=True) save(filepath, mask, replace=True)
......
...@@ -79,7 +79,7 @@ def get_local_ip(): ...@@ -79,7 +79,7 @@ def get_local_ip():
try: try:
# doesn't even have to be reachable # doesn't even have to be reachable
_socket.connect(("192.255.255.255", 1)) _socket.connect(("192.255.255.255", 1))
ip_address = _socket.getsockname()[0] ip_address = _socket.getsockname()
except socket.error: except socket.error:
ip_address = "127.0.0.1" ip_address = "127.0.0.1"
finally: finally:
...@@ -279,7 +279,6 @@ def run_gradio_app(gradio_interface, host = "0.0.0.0"): ...@@ -279,7 +279,6 @@ def run_gradio_app(gradio_interface, host = "0.0.0.0"):
else: else:
raise Exception("Port not specified from QIM API") raise Exception("Port not specified from QIM API")
print(port_dict)
gradio_header(gradio_interface.title, port) gradio_header(gradio_interface.title, port)
# Create FastAPI with mounted gradio interface # Create FastAPI with mounted gradio interface
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment