raddoc commited on
Commit
86ac9e8
·
verified ·
1 Parent(s): eb51f25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -23
app.py CHANGED
@@ -1,32 +1,50 @@
1
- # Archive paper: https://arxiv.org/abs/2404.13000
2
-
3
  import os
4
  import gradio as gr
5
- import matplotlib.pyplot as plt
6
  import numpy as np
7
  import skimage
8
- from skimage import io, transform
9
-
10
- from io_utils import LoadImageD
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # Gradio helper functions
13
 
14
- current_img = None
15
- live_preds = None
16
-
17
- def rotate_img(image, angles):
18
- """Rotate the input image based on the given angles."""
19
- xt, yt, zt = angles
20
-
21
- # Apply rotations (this is just a placeholder, replace with your actual logic)
22
- rotated_img = skimage.transform.rotate(image, angle=xt, mode='edge')
23
- rotated_img = skimage.transform.rotate(rotated_img, angle=yt, mode='edge')
24
- rotated_img = skimage.transform.rotate(rotated_img, angle=zt, mode='edge')
25
-
26
- return rotated_img
27
 
28
  def rotate_btn_fn(img, xt, yt, zt, add_bone_cmap=False):
29
- global current_img
30
  try:
31
  angles = (xt, yt, zt)
32
  print(f"Rotating with angles: {angles}")
@@ -40,8 +58,8 @@ def rotate_btn_fn(img, xt, yt, zt, add_bone_cmap=False):
40
  else:
41
  raise ValueError("Invalid input image")
42
 
43
- # Apply the rotation
44
- out_img = rotate_img(img, angles)
45
 
46
  if not add_bone_cmap:
47
  return out_img
@@ -49,7 +67,6 @@ def rotate_btn_fn(img, xt, yt, zt, add_bone_cmap=False):
49
  cmap = plt.get_cmap('bone')
50
  out_img = cmap(out_img)
51
  out_img = (out_img[..., :3] * 255).astype(np.uint8)
52
- current_img = out_img
53
  return out_img
54
 
55
  except Exception as e:
 
 
 
1
  import os
2
  import gradio as gr
 
3
  import numpy as np
4
  import skimage
5
+ from skimage import io
6
+ import torch
7
+ import monai
8
+ from monai.transforms import Rotate
9
+
10
+ # Placeholder for the 3D reconstruction model
11
+ class Simple3DReconstructionModel:
12
+ def __init__(self):
13
+ # Load your pre-trained model here
14
+ self.model = None # replace with actual model loading
15
+
16
+ def reconstruct_3d(self, image):
17
+ # Implement the 3D reconstruction logic here
18
+ # This is a placeholder example
19
+ return np.zeros((128, 128, 128))
20
+
21
+ def rotate_3d(self, volume, angles):
22
+ # Rotate the 3D volume using MONAI
23
+ rotate = Rotate(angles, mode='bilinear')
24
+ rotated_volume = rotate(volume)
25
+ return rotated_volume
26
+
27
+ def project_2d(self, volume):
28
+ # Project the 3D volume back to 2D
29
+ # This is a placeholder example
30
+ projection = np.max(volume, axis=0)
31
+ return projection
32
+
33
+ # Initialize the model
34
+ model = Simple3DReconstructionModel()
35
 
36
  # Gradio helper functions
37
 
38
+ def process_image(img, xt, yt, zt):
39
+ # Reconstruct the 3D volume
40
+ volume = model.reconstruct_3d(img)
41
+ # Rotate the 3D volume
42
+ rotated_volume = model.rotate_3d(volume, (xt, yt, zt))
43
+ # Project the rotated volume back to 2D
44
+ output_img = model.project_2d(rotated_volume)
45
+ return output_img
 
 
 
 
 
46
 
47
  def rotate_btn_fn(img, xt, yt, zt, add_bone_cmap=False):
 
48
  try:
49
  angles = (xt, yt, zt)
50
  print(f"Rotating with angles: {angles}")
 
58
  else:
59
  raise ValueError("Invalid input image")
60
 
61
+ # Process the image with the model
62
+ out_img = process_image(img, xt, yt, zt)
63
 
64
  if not add_bone_cmap:
65
  return out_img
 
67
  cmap = plt.get_cmap('bone')
68
  out_img = cmap(out_img)
69
  out_img = (out_img[..., :3] * 255).astype(np.uint8)
 
70
  return out_img
71
 
72
  except Exception as e: