ho11laqe commited on
Commit
84f0951
1 Parent(s): f6aa191

task conversion

Browse files
app.py CHANGED
@@ -2,28 +2,38 @@ import gradio as gr
2
  import subprocess
3
  import os
4
  import numpy as np
 
5
 
6
  os.environ['data_raw'] = 'data_raw/'
7
  os.environ['nnUNet_raw_data_base'] = 'nnUNet_raw_data_base/'
8
  os.environ['nnUNet_preprocessed'] = 'nnUNet_preprocessed/'
9
  os.environ['RESULTS_FOLDER'] = 'calvingfronts/'
10
 
 
11
 
12
 
13
  def run_front_detection(input_img):
14
- input_img.save('data_raw/test.png')
 
15
  subprocess.run(
16
  ['python3', 'nnunet/dataset_conversion/Task500_Glacier_inference.py', '-data_percentage', '100', '-base',
17
  os.environ['data_raw']])
18
  cmd = [
19
  'python3', 'nnunet/inference/predict_simple.py',
20
- '-i', os.path.join('$nnUNet_raw_data_base', 'nnUNet_raw_data/Task500_Glacier_zonefronts/imagesTs/'),
21
- '-o', os.path.join('$RESULTS_FOLDER', 'fold_0'),
22
  '-t', '500','-m','2d','-f','0','-p', 'nnUNetPlansv2.1', '-tr','nnUNetTrainerV2', '-model_folder_name',
23
- '$model'
24
  ]
25
- #subprocess.run(cmd)
26
- #TODO remove files
 
27
 
28
- demo = gr.Interface(run_front_detection, gr.Image(type='pil'), "image")
 
 
 
 
 
 
29
  demo.launch()
 
2
  import subprocess
3
  import os
4
  import numpy as np
5
+ from PIL import Image
6
 
7
  os.environ['data_raw'] = 'data_raw/'
8
  os.environ['nnUNet_raw_data_base'] = 'nnUNet_raw_data_base/'
9
  os.environ['nnUNet_preprocessed'] = 'nnUNet_preprocessed/'
10
  os.environ['RESULTS_FOLDER'] = 'calvingfronts/'
11
 
12
+ model_path = 'Task500_Glacier_zonefronts'
13
 
14
 
15
  def run_front_detection(input_img):
16
+ image_gray = input_img.convert("L")
17
+ image_gray.save('data_raw/test.png')
18
  subprocess.run(
19
  ['python3', 'nnunet/dataset_conversion/Task500_Glacier_inference.py', '-data_percentage', '100', '-base',
20
  os.environ['data_raw']])
21
  cmd = [
22
  'python3', 'nnunet/inference/predict_simple.py',
23
+ '-i', os.path.join(os.environ['nnUNet_raw_data_base'], 'nnUNet_raw_data/Task500_Glacier_zonefronts/imagesTs/'),
24
+ '-o', os.path.join(os.environ['RESULTS_FOLDER'], 'fold_0'),
25
  '-t', '500','-m','2d','-f','0','-p', 'nnUNetPlansv2.1', '-tr','nnUNetTrainerV2', '-model_folder_name',
26
+ model_path
27
  ]
28
+ subprocess.run(cmd)
29
+ subprocess.run(['python3', 'nnunet/dataset_conversion/Task500_Glacier_reverse.py', '-i',
30
+ os.path.join(os.environ['RESULTS_FOLDER'], 'fold_0')])
31
 
32
+ os.remove('data_raw/test.png')
33
+ os.remove('nnUNet_raw_data_base/nnUNet_raw_data/Task500_Glacier_zonefronts/imagesTs/test_0000.nii.gz')
34
+
35
+ front = Image.open(os.path.join(os.environ['RESULTS_FOLDER'], 'tifs/test_front.png'))
36
+ return front
37
+
38
+ demo = gr.Interface(run_front_detection, gr.Image(type='pil'), "image",interface_size=(1000, 1000))
39
  demo.launch()
data_raw/test.png DELETED
Binary file (274 kB)
 
nnunet/dataset_conversion/Task500_Glacier_inference.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from nnunet.utilities.file_conversions import convert_2d_image_to_nifti
2
  from nnunet.paths import nnUNet_raw_data, preprocessing_output_dir
3
  from nnunet.dataset_conversion.utils import generate_dataset_json
@@ -10,73 +11,6 @@ import json
10
  from typing import List
11
 
12
 
13
- def subdirs(folder: str, join: bool = True, prefix: str = None, suffix: str = None, sort: bool = True) -> List[str]:
14
- if join:
15
- l = os.path.join
16
- else:
17
- l = lambda x, y: y
18
- res = [l(folder, i) for i in os.listdir(folder) if os.path.isdir(os.path.join(folder, i))
19
- and (prefix is None or i.startswith(prefix))
20
- and (suffix is None or i.endswith(suffix))]
21
- if sort:
22
- res.sort()
23
- return res
24
-
25
-
26
- def subfiles(folder: str, join: bool = True, prefix: str = None, suffix: str = None, sort: bool = True) -> List[str]:
27
- if join:
28
- l = os.path.join
29
- else:
30
- l = lambda x, y: y
31
- res = [l(folder, i) for i in os.listdir(folder) if os.path.isfile(os.path.join(folder, i))
32
- and (prefix is None or i.startswith(prefix))
33
- and (suffix is None or i.endswith(suffix))]
34
- if sort:
35
- res.sort()
36
- return res
37
-
38
-
39
- def nifti_files(folder: str, join: bool = True, sort: bool = True) -> List[str]:
40
- return subfiles(folder, join=join, sort=sort, suffix='.nii.gz')
41
-
42
-
43
- def maybe_mkdir_p(directory: str) -> None:
44
- os.makedirs(directory, exist_ok=True)
45
-
46
-
47
- def load_pickle(file: str, mode: str = 'rb'):
48
- with open(file, mode) as f:
49
- a = pickle.load(f)
50
- return a
51
-
52
-
53
- def write_pickle(obj, file: str, mode: str = 'wb') -> None:
54
- with open(file, mode) as f:
55
- pickle.dump(obj, f)
56
-
57
-
58
- def load_json(file: str):
59
- with open(file, 'r') as f:
60
- a = json.load(f)
61
- return a
62
-
63
-
64
- def save_json(obj, file: str, indent: int = 4, sort_keys: bool = True) -> None:
65
- with open(file, 'w') as f:
66
- json.dump(obj, f, sort_keys=sort_keys, indent=indent)
67
-
68
-
69
- def pardir(path: str):
70
- return os.path.join(path, os.pardir)
71
-
72
-
73
- def split_path(path: str) -> List[str]:
74
- """
75
- splits at each separator. This is different from os.path.split which only splits at last separator
76
- """
77
- return path.split(os.sep)
78
-
79
-
80
  # I'm tired of typing these out
81
  join = os.path.join
82
  isdir = os.path.isdir
 
1
+ from batchgenerators.utilities.file_and_folder_operations import *
2
  from nnunet.utilities.file_conversions import convert_2d_image_to_nifti
3
  from nnunet.paths import nnUNet_raw_data, preprocessing_output_dir
4
  from nnunet.dataset_conversion.utils import generate_dataset_json
 
11
  from typing import List
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # I'm tired of typing these out
15
  join = os.path.join
16
  isdir = os.path.isdir
nnunet/dataset_conversion/Task500_Glacier_reverse.py CHANGED
@@ -1,6 +1,7 @@
1
  import SimpleITK as sitk
2
  import argparse
3
 
 
4
  import numpy as np
5
  import torch
6
  import os
@@ -40,13 +41,17 @@ def main(input_folder):
40
  color_front = extract_front_from_zones(color_zone, 10)
41
  color_front[color_front==255] =1
42
 
43
- output_path_zone = join(output_folder, file[:-len('.nii.gz')] + '_zone.tif')
44
- output_path_front = join(output_folder, file[:-len('.nii.gz')] + '_front.tif')
45
 
 
 
 
46
  img_zone = Image.fromarray(color_zone)
47
  img_front = Image.fromarray(color_front)
48
  img_zone.save(output_path_zone)
49
  img_front.save(output_path_front)
 
50
 
51
  if __name__ == '__main__':
52
  parser = argparse.ArgumentParser()
 
1
  import SimpleITK as sitk
2
  import argparse
3
 
4
+ import imageio
5
  import numpy as np
6
  import torch
7
  import os
 
41
  color_front = extract_front_from_zones(color_zone, 10)
42
  color_front[color_front==255] =1
43
 
44
+ output_path_zone = join(output_folder, file[:-len('.nii.gz')] + '_zone.png')
45
+ output_path_front = join(output_folder, file[:-len('.nii.gz')] + '_front.png')
46
 
47
+ imageio.imwrite(output_path_zone, color_zone)
48
+ imageio.imwrite(output_path_front, color_front)
49
+ """
50
  img_zone = Image.fromarray(color_zone)
51
  img_front = Image.fromarray(color_front)
52
  img_zone.save(output_path_zone)
53
  img_front.save(output_path_front)
54
+ """
55
 
56
  if __name__ == '__main__':
57
  parser = argparse.ArgumentParser()
requirements.txt CHANGED
@@ -18,4 +18,7 @@ astropy
18
  einops
19
  plotly
20
  imageio
21
- SimpleITK
 
 
 
 
18
  einops
19
  plotly
20
  imageio
21
+ SimpleITK
22
+ gradio
23
+ torch
24
+ MedPy