1
1
from pathlib import Path
2
- from typing import Optional
2
+ from typing import Annotated , Optional
3
3
4
+ import torch
4
5
import typer
5
6
6
7
from kelp_o_matic import (
9
10
find_mussels as find_mussels_ ,
10
11
)
11
12
12
- cli = typer .Typer (context_settings = {"help_option_names" : ["-h" , "--help" ]})
13
+ cli = typer .Typer (context_settings = {"help_option_names" : ["-h" , "--help" ]}, add_completion = False )
13
14
14
15
15
16
@cli .command ()
16
17
def find_kelp (
17
- source : Path = typer .Argument (..., help = "Input image with Byte data type." ),
18
- dest : Path = typer .Argument (..., help = "File path location to save output to." ),
19
- species : bool = typer .Option (
20
- False ,
21
- "--species/--presence" ,
22
- help = "Segment to species or presence/absence level." ,
23
- ),
24
- crop_size : int = typer .Option (
25
- 1024 ,
26
- help = "The data window size to run through the segmentation model." ,
27
- ),
28
- use_nir : bool = typer .Option (
29
- False ,
30
- "--rgbi/--rgb" ,
31
- help = "Use RGB and NIR bands for classification. Assumes RGBI ordering." ,
32
- ),
33
- band_order : Optional [list [int ]] = typer .Option (
34
- None ,
35
- "-b" ,
36
- help = "GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
37
- "To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`." ,
38
- ),
39
- use_gpu : bool = typer .Option (
40
- True , "--gpu/--no-gpu" , help = "Enable or disable GPU, if available."
41
- ),
42
- use_tta : bool = typer .Option (
43
- False ,
44
- "--tta/--no-tta" ,
45
- help = "Use test time augmentation to improve accuracy at the cost of "
46
- "processing time." ,
47
- ),
18
+ source : Annotated [
19
+ Path ,
20
+ typer .Argument (
21
+ exists = True ,
22
+ dir_okay = False ,
23
+ file_okay = True ,
24
+ readable = True ,
25
+ help = "Input image with Byte data type." ,
26
+ ),
27
+ ],
28
+ dest : Annotated [
29
+ Path ,
30
+ typer .Argument (
31
+ exists = True ,
32
+ dir_okay = False ,
33
+ file_okay = True ,
34
+ writable = True ,
35
+ help = "File path location to save output to." ,
36
+ ),
37
+ ],
38
+ species : Annotated [
39
+ bool ,
40
+ typer .Option (
41
+ "--species/--presence" ,
42
+ help = "Segment to species or presence/absence level." ,
43
+ ),
44
+ ] = False ,
45
+ crop_size : Annotated [
46
+ int ,
47
+ typer .Option (
48
+ help = "The data window size to run through the segmentation model." ,
49
+ ),
50
+ ] = 1024 ,
51
+ use_nir : Annotated [
52
+ bool ,
53
+ typer .Option (
54
+ "--rgbi/--rgb" ,
55
+ help = "Use RGB and NIR bands for classification. Assumes RGBI ordering." ,
56
+ ),
57
+ ] = False ,
58
+ band_order : Annotated [
59
+ Optional [list [int ]],
60
+ typer .Option (
61
+ "-b" ,
62
+ help = "GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
63
+ "To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`." ,
64
+ ),
65
+ ] = None ,
66
+ use_gpu : Annotated [
67
+ bool ,
68
+ typer .Option ("--gpu/--no-gpu" , help = "Enable or disable GPU, if available." ),
69
+ ] = True ,
70
+ use_tta : Annotated [
71
+ bool ,
72
+ typer .Option (
73
+ "--tta/--no-tta" ,
74
+ help = "Use test time augmentation to improve accuracy at the cost of "
75
+ "processing time." ,
76
+ ),
77
+ ] = False ,
48
78
):
49
79
"""
50
80
Detect kelp in image at path SOURCE and output the resulting classification raster
@@ -55,27 +85,52 @@ def find_kelp(
55
85
56
86
@cli .command ()
57
87
def find_mussels (
58
- source : Path = typer .Argument (..., help = "Input image with Byte data type." ),
59
- dest : Path = typer .Argument (..., help = "File path location to save output to." ),
60
- crop_size : int = typer .Option (
61
- 1024 ,
62
- help = "The data window size to run through the segmentation model." ,
63
- ),
64
- band_order : Optional [list [int ]] = typer .Option (
65
- None ,
66
- "-b" ,
67
- help = "GDAL-style band re-ordering flag. Defaults to RGB order. "
68
- "To e.g., reorder a BGR image at runtime, pass flags `-b 3 -b 2 -b 1`." ,
69
- ),
70
- use_gpu : bool = typer .Option (
71
- True , "--gpu/--no-gpu" , help = "Enable or disable GPU, if available."
72
- ),
73
- use_tta : bool = typer .Option (
74
- False ,
75
- "--tta/--no-tta" ,
76
- help = "Use test time augmentation to improve accuracy at the cost of "
77
- "processing time." ,
78
- ),
88
+ source : Annotated [
89
+ Path ,
90
+ typer .Argument (
91
+ exists = True ,
92
+ dir_okay = False ,
93
+ file_okay = True ,
94
+ readable = True ,
95
+ help = "Input image with Byte data type." ,
96
+ ),
97
+ ],
98
+ dest : Annotated [
99
+ Path ,
100
+ typer .Argument (
101
+ exists = True ,
102
+ dir_okay = False ,
103
+ file_okay = True ,
104
+ writable = True ,
105
+ help = "File path location to save output to." ,
106
+ ),
107
+ ],
108
+ crop_size : Annotated [
109
+ int ,
110
+ typer .Option (
111
+ help = "The data window size to run through the segmentation model." ,
112
+ ),
113
+ ] = 1024 ,
114
+ band_order : Annotated [
115
+ Optional [list [int ]],
116
+ typer .Option (
117
+ "-b" ,
118
+ help = "GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
119
+ "To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`." ,
120
+ ),
121
+ ] = None ,
122
+ use_gpu : Annotated [
123
+ bool ,
124
+ typer .Option ("--gpu/--no-gpu" , help = "Enable or disable GPU, if available." ),
125
+ ] = True ,
126
+ use_tta : Annotated [
127
+ bool ,
128
+ typer .Option (
129
+ "--tta/--no-tta" ,
130
+ help = "Use test time augmentation to improve accuracy at the cost of "
131
+ "processing time." ,
132
+ ),
133
+ ] = False ,
79
134
):
80
135
"""
81
136
Detect mussels in image at path SOURCE and output the resulting classification
@@ -90,11 +145,33 @@ def version_callback(value: bool) -> None:
90
145
raise typer .Exit ()
91
146
92
147
148
+ def gpu_callback (value : bool ) -> None :
149
+ if value :
150
+ typer .echo (f"GPU detected: { torch .cuda .is_available ()} " )
151
+ raise typer .Exit ()
152
+
153
+
93
154
@cli .callback ()
94
155
def main (
95
- version : bool = typer .Option (
96
- None , "--version" , "-v" , callback = version_callback , is_eager = True
97
- ),
156
+ version : Annotated [
157
+ bool ,
158
+ typer .Option (
159
+ "--version" ,
160
+ "-v" ,
161
+ callback = version_callback ,
162
+ is_eager = True ,
163
+ help = "Show version and exit." ,
164
+ ),
165
+ ] = False ,
166
+ gpu_test : Annotated [
167
+ bool ,
168
+ typer .Option (
169
+ "--gpu-test" ,
170
+ callback = gpu_callback ,
171
+ is_eager = True ,
172
+ help = "Test if GPU is detected and exit." ,
173
+ ),
174
+ ] = False ,
98
175
):
99
176
return
100
177
0 commit comments