Skip to content

Commit cc6a059

Browse files
authored
New stuff (#9)
* fix: update Google Drive file ID for model download * fix: update run function to return empty arrays on error * refactor: update Config class to use Path for input and output settings * fix: convert output_dir and model_path to string in detection method * fix: convert input_image to string before loading in run function * fix: update write_json function to use Any type for content parameter * fix: simplify line coefficient computation in LeastSquaresSolution * fix: update drawing import to use LineDrawing for line visualization * fix: update import statement for euclidean_distance in Line class * fix: return float type for euclidean_distance function result * fix: ensure boolean type for good_alignment in find_detections return value * refactor: remove unused detection methods and related code
1 parent d45fd51 commit cc6a059

File tree

21 files changed

+34
-2173
lines changed

21 files changed

+34
-2173
lines changed

tree-disk-api/src/config/preparation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def download_u2net_model():
1515
"""
1616
Download the model from Google Drive.
1717
"""
18-
file_id = "1ao1ovG1Qtx4b7EoskHXmi2E9rp5CHLcZ"
18+
file_id = "10HXfiEMT4QapiRXMSSEJEAViRmRmSDUW"
1919
url = f"https://drive.google.com/uc?id={file_id}"
2020

2121
logger.info(f"Downloading Google Drive file from: {url}")

tree-disk-pith/README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import treediskpith
2020
# Configure the analyzer
2121
treediskpith.configure(
2222
input_image="input/tree-disk4.png",
23+
model_path="models/apd_dl.h5",
2324
save_results=True,
2425
)
2526

@@ -35,24 +36,15 @@ treediskpith.configure(
3536

3637
Basic usage:
3738
```bash
38-
tree-disk-pith --input_image ./input/tree-disk3.png --new_shape 640 --debug
39-
```
40-
41-
Save intermediate results:
42-
```bash
43-
tree-disk-pith --input_image ./input/tree-disk3.png --new_shape 640 --debug --method apd_pcl --save_results
39+
tree-disk-pith --input_image ./input/tree-disk3.png
4440
```
4541

4642
Advanced usage with custom parameters:
4743
```bash
4844
tree-disk-pith \
4945
--input_image input/tree-disk3.png \
50-
--cx 1204 \
51-
--cy 1264 \
5246
--output_dir custom_output/ \
53-
--sigma 4.0 \
54-
--th_low 10 \
55-
--th_high 25 \
47+
--new_shape 640 \
5648
--save_results \
5749
--debug
5850
```
@@ -63,12 +55,7 @@ tree-disk-pith \
6355
|----------|------|----------|---------|-------------|
6456
| `--input_image` | str | Yes | - | Input image file path |
6557
| `--output_dir` | str | Yes | - | Output directory path |
66-
| `--method` | str | No | apd | Detection method to use. Choices are apd, apd_pcl, or apd_dl |
6758
| `--model_path` | str | No | - | Path to the weights file (required if using apd_dl method) |
68-
| `--percent_lo` | float | No | 0.7 | percent_lo parameter for the algorithm |
69-
| `--st_w` | int | No | 3 | st_w parameter for the algorithm |
70-
| `--lo_w` | int | No | 3 | lo_w parameter for the algorithm |
71-
| `--st_sigma` | float | No | 1.2 | st_sigma parameter for the algorithm |
7259
| `--new_shape` | int | No | 0 | New shape for resizing the input image. If 0, no resizing is done |
7360
| `--debug` | flag | No | False | Enable debug mode to save intermediate images and outputs |
7461
| `--save_results` | flag | No | False | Save intermediate images, labelme and config file |
@@ -88,17 +75,17 @@ poetry install
8875
eval $(poetry env activate)
8976
```
9077

91-
1. Running tests:
78+
2. Running tests:
9279
```bash
93-
poetry run pytest
80+
pytest
9481
```
9582

96-
1. fetch dataset
83+
3. fetch dataset
9784
```bash
9885
python fetch_dataset.py
9986
```
10087

101-
1. Download pretrained model
88+
4. Download pretrained model
10289
```bash
10390
python fetch_pretrained_model.py
10491
```

tree-disk-pith/src/treediskpith/cli.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,11 @@ def parse_arguments():
1717
parser.add_argument(
1818
"--output_dir", type=str, default=Config.output_dir, help="Output directory"
1919
)
20-
parser.add_argument(
21-
"--method",
22-
type=str,
23-
choices=["apd", "apd_pcl", "apd_dl"],
24-
default=Config.method,
25-
help="Method to use: 'apd', 'apd_pcl', or 'apd_dl'",
26-
)
2720
parser.add_argument(
2821
"--model_path",
2922
type=str,
3023
default=Config.model_path,
31-
help="Path to the weights file (required for method 'apd_dl')",
32-
)
33-
parser.add_argument(
34-
"--percent_lo",
35-
type=float,
36-
default=Config.percent_lo,
37-
help="percent_lo parameter",
38-
)
39-
parser.add_argument("--st_w", type=int, default=Config.st_w, help="st_w parameter")
40-
parser.add_argument("--lo_w", type=int, default=Config.lo_w, help="lo_w parameter")
41-
parser.add_argument(
42-
"--st_sigma", type=float, default=Config.st_sigma, help="st_sigma parameter"
24+
help="Path to the weights file",
4325
)
4426
parser.add_argument(
4527
"--new_shape",
@@ -62,11 +44,6 @@ def main():
6244
configure(
6345
input_image=args.input_image,
6446
output_dir=args.output_dir,
65-
method=args.method,
66-
percent_lo=args.percent_lo,
67-
st_w=args.st_w,
68-
lo_w=args.lo_w,
69-
st_sigma=args.st_sigma,
7047
new_shape=args.new_shape,
7148
debug=args.debug,
7249
model_path=args.model_path,

tree-disk-pith/src/treediskpith/config.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55
from typing import Any, Dict, Optional
66
from datetime import datetime
7-
from .detection.detection_method import DetectionMethod
87

98
from .utils.file_utils import ensure_directory
109

@@ -21,27 +20,17 @@ class Config:
2120
Attributes:
2221
input_image (str): Input image file path.
2322
output_dir (str): Output directory path.
24-
method (Method): Detection method to use.
25-
percent_lo (float): Percent_lo parameter for detection.
26-
st_w (int): ST_W parameter for spatial filtering.
27-
lo_w (int): LO_W parameter for local optimization.
28-
st_sigma (float): ST_Sigma parameter for Gaussian filtering.
2923
new_shape (int): New shape for resizing the image.
3024
debug (bool): Enable debug mode for additional logging.
31-
model_path (Optional[str]): Path to model file (required for 'apd_dl' method).
25+
model_path (Optional[str]): Path to model file.
3226
"""
3327

3428
# -------------- Input/Output Settings ----------------
35-
input_image: str = ""
36-
output_dir: str = "./output/"
37-
method: DetectionMethod = DetectionMethod.APD
38-
model_path: Optional[str] = None
29+
input_image: Optional[Path] = None
30+
output_dir: Path = Path("./output/")
31+
model_path: Optional[Path] = None
3932

4033
# -------------- Processing Parameters ----------------
41-
percent_lo: float = 0.1
42-
st_w: int = 5
43-
lo_w: int = 3
44-
st_sigma: float = 1.0
4534
new_shape: int = 0
4635

4736
# -------------- Operation Modes ----------------
@@ -60,8 +49,10 @@ def __post_init__(self):
6049
self._change_history[field_name] = []
6150

6251
# Validate method-specific requirements
63-
if self.method == DetectionMethod.APD_DL and not self.model_path:
64-
raise ValueError("model_path is required when using APD_DL method")
52+
if not self.model_path:
53+
logger.warning(
54+
"model_path is not set yet. It is required for proper operation. Please configure it accordingly."
55+
)
6556

6657
def _validate_and_set_paths(self):
6758
"""Validate and set all path-related fields."""
@@ -72,12 +63,12 @@ def _validate_and_set_paths(self):
7263
raise ValueError(f"Input image file does not exist: {input_path}")
7364
if not input_path.is_file():
7465
raise ValueError(f"Input image path is not a file: {input_path}")
75-
self.input_image = str(input_path.resolve())
66+
self.input_image = input_path.resolve()
7667

7768
# Set up output directory
7869
output_path = Path(self.output_dir)
7970
try:
80-
self.output_dir = str(ensure_directory(output_path))
71+
self.output_dir = ensure_directory(output_path)
8172
except PermissionError:
8273
raise ValueError(
8374
f"Cannot create output directory (permission denied): {output_path}"
@@ -92,7 +83,7 @@ def _validate_and_set_paths(self):
9283
raise ValueError(f"Model file does not exist: {model_path}")
9384
if not model_path.is_file():
9485
raise ValueError(f"Model path is not a file: {model_path}")
95-
self.model_path = str(model_path.resolve())
86+
self.model_path = model_path.resolve()
9687

9788
def _log_change(self, param: str, old_value: Any, new_value: Any):
9889
"""Log a parameter change with timestamp."""
@@ -124,15 +115,13 @@ def update(self, **kwargs):
124115

125116
old_value = getattr(self, key)
126117
if old_value != new_value:
127-
if key == "method":
128-
new_value = DetectionMethod(new_value)
129118
setattr(self, key, new_value)
130119
self._log_change(key, old_value, new_value)
131120

132121
if needs_validation:
133122
self._validate_and_set_paths()
134123

135-
def get_change_history(self, param: str = None) -> Dict:
124+
def get_change_history(self, param: Optional[str] = None) -> Dict:
136125
"""
137126
Get change history for a specific parameter or all parameters.
138127
@@ -151,11 +140,7 @@ def get_change_history(self, param: str = None) -> Dict:
151140
def to_dict(self) -> dict:
152141
"""Convert configuration to dictionary, excluding internal fields."""
153142
return {
154-
k: (
155-
v.value
156-
if isinstance(v, DetectionMethod)
157-
else str(v) if isinstance(v, Path) else v
158-
)
143+
k: (str(v) if isinstance(v, Path) else v)
159144
for k, v in self.__dict__.items()
160145
if not k.startswith("_")
161146
}
@@ -186,9 +171,7 @@ def configure(**kwargs):
186171
Example:
187172
>>> configure(
188173
... input_image="sample.jpg",
189-
... method="apd_dl",
190174
... model_path="model.pth",
191-
... st_w=7
192175
... )
193176
"""
194177
config.update(**kwargs)

tree-disk-pith/src/treediskpith/detection/detection_method.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)