Upload folder using huggingface_hub
Browse files- .gitattributes +2 -0
- README.md +2 -1
- src/fiberpose/paths.py +2 -0
- src/fiberpose/resources/example_fibers.png +3 -0
- src/fiberpose/resources/example_ribbons.png +3 -0
- src/fiberpose/web/__init__.py +10 -5
- src/fiberpose/web/callbacks.py +11 -1
.gitattributes
CHANGED
|
@@ -35,3 +35,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
src/fiberpose/resources/test.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
docs/gui.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
src/fiberpose/resources/test.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
docs/gui.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
src/fiberpose/resources/example_fibers.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
src/fiberpose/resources/example_ribbons.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -293,7 +293,8 @@ Fiberpose/
|
|
| 293 |
└── resources/
|
| 294 |
├── config.json # Default configuration
|
| 295 |
├── fiber_tool.ui # Qt GUI layout
|
| 296 |
-
|
|
|
|
| 297 |
```
|
| 298 |
|
| 299 |
---
|
|
|
|
| 293 |
└── resources/
|
| 294 |
├── config.json # Default configuration
|
| 295 |
├── fiber_tool.ui # Qt GUI layout
|
| 296 |
+
├── test.png # Example image
|
| 297 |
+
└── example_*.png # More examples for the web interface
|
| 298 |
```
|
| 299 |
|
| 300 |
---
|
src/fiberpose/paths.py
CHANGED
|
@@ -18,6 +18,8 @@ DEFAULT_UI_PATH = RESOURCES_DIR / "fiber_tool.ui"
|
|
| 18 |
DARK_THEME_PATH = RESOURCES_DIR / "dark_theme.qss"
|
| 19 |
APP_ICON_PATH = RESOURCES_DIR / "fiberpose.png"
|
| 20 |
EXAMPLE_IMAGE_PATH = RESOURCES_DIR / "test.png"
|
|
|
|
|
|
|
| 21 |
BUNDLED_CONFIG_PATH = RESOURCES_DIR / "config.json"
|
| 22 |
|
| 23 |
USER_CONFIG_PATH = Path(user_config_dir("fiberpose", appauthor=False)) / "config.json"
|
|
|
|
| 18 |
DARK_THEME_PATH = RESOURCES_DIR / "dark_theme.qss"
|
| 19 |
APP_ICON_PATH = RESOURCES_DIR / "fiberpose.png"
|
| 20 |
EXAMPLE_IMAGE_PATH = RESOURCES_DIR / "test.png"
|
| 21 |
+
# Examples offered on the web page.
|
| 22 |
+
EXAMPLE_IMAGE_PATHS = [EXAMPLE_IMAGE_PATH, RESOURCES_DIR / "example_fibers.png", RESOURCES_DIR / "example_ribbons.png"]
|
| 23 |
BUNDLED_CONFIG_PATH = RESOURCES_DIR / "config.json"
|
| 24 |
|
| 25 |
USER_CONFIG_PATH = Path(user_config_dir("fiberpose", appauthor=False)) / "config.json"
|
src/fiberpose/resources/example_fibers.png
ADDED
|
Git LFS Details
|
src/fiberpose/resources/example_ribbons.png
ADDED
|
Git LFS Details
|
src/fiberpose/web/__init__.py
CHANGED
|
@@ -4,7 +4,8 @@
|
|
| 4 |
Analysis settings come from the same config file as the GUI and CLI.
|
| 5 |
Host, port and proxy path come from Gradio's own environment variables
|
| 6 |
(``GRADIO_SERVER_NAME``, ``GRADIO_SERVER_PORT``, ``GRADIO_ROOT_PATH``);
|
| 7 |
-
``FIBERPOSE_MAX_UPLOAD`` caps the upload size (default ``50mb``)
|
|
|
|
| 8 |
``FIBERPOSE_USAGE_REPO`` turns on the usage log (see ``usage.py``).
|
| 9 |
"""
|
| 10 |
|
|
@@ -18,7 +19,7 @@ import gradio as gr
|
|
| 18 |
|
| 19 |
from ..api import FiberposeError
|
| 20 |
from ..domain import AppConfig
|
| 21 |
-
from ..paths import
|
| 22 |
from . import callbacks as cb
|
| 23 |
from . import usage
|
| 24 |
|
|
@@ -37,10 +38,14 @@ def build_demo(config: AppConfig | None = None) -> gr.Blocks:
|
|
| 37 |
if not path:
|
| 38 |
raise gr.Error("Upload an image first.")
|
| 39 |
settings = replace(config, watershed_grad_threshold=edge_threshold)
|
| 40 |
-
example = os.path.basename(path)
|
| 41 |
start = time.perf_counter()
|
| 42 |
try:
|
| 43 |
session = cb.analyze_file(path, settings, pixel_size, unit, invert, previous)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except FiberposeError as exc:
|
| 45 |
usage.log(ok=False, seconds=round(time.perf_counter() - start, 2), example=example)
|
| 46 |
raise gr.Error(str(exc)) from exc
|
|
@@ -60,7 +65,7 @@ def build_demo(config: AppConfig | None = None) -> gr.Blocks:
|
|
| 60 |
with gr.Blocks(title="Fiberpose") as demo:
|
| 61 |
gr.Markdown(
|
| 62 |
"# Fiberpose\nSegmentation and morphometry of fibrous microstructures. Upload a micrograph "
|
| 63 |
-
"(or click
|
| 64 |
)
|
| 65 |
session = gr.State() # this browser tab's analysed image (callbacks.Session)
|
| 66 |
with gr.Row(equal_height=True): # settings, each above the image it applies to
|
|
@@ -82,7 +87,7 @@ def build_demo(config: AppConfig | None = None) -> gr.Blocks:
|
|
| 82 |
with gr.Row():
|
| 83 |
button = gr.Button("Analyze", variant="primary")
|
| 84 |
gr.HTML() # empty half: keeps the button at half width
|
| 85 |
-
gr.Examples([[str(
|
| 86 |
summary = gr.Markdown()
|
| 87 |
with gr.Column(): # under the result
|
| 88 |
with gr.Row():
|
|
|
|
| 4 |
Analysis settings come from the same config file as the GUI and CLI.
|
| 5 |
Host, port and proxy path come from Gradio's own environment variables
|
| 6 |
(``GRADIO_SERVER_NAME``, ``GRADIO_SERVER_PORT``, ``GRADIO_ROOT_PATH``);
|
| 7 |
+
``FIBERPOSE_MAX_UPLOAD`` caps the upload size (default ``50mb``) and
|
| 8 |
+
``FIBERPOSE_MAX_SIDE`` the image width and height (default ``1920`` px);
|
| 9 |
``FIBERPOSE_USAGE_REPO`` turns on the usage log (see ``usage.py``).
|
| 10 |
"""
|
| 11 |
|
|
|
|
| 19 |
|
| 20 |
from ..api import FiberposeError
|
| 21 |
from ..domain import AppConfig
|
| 22 |
+
from ..paths import EXAMPLE_IMAGE_PATHS
|
| 23 |
from . import callbacks as cb
|
| 24 |
from . import usage
|
| 25 |
|
|
|
|
| 38 |
if not path:
|
| 39 |
raise gr.Error("Upload an image first.")
|
| 40 |
settings = replace(config, watershed_grad_threshold=edge_threshold)
|
| 41 |
+
example = os.path.basename(path) in {p.name for p in EXAMPLE_IMAGE_PATHS}
|
| 42 |
start = time.perf_counter()
|
| 43 |
try:
|
| 44 |
session = cb.analyze_file(path, settings, pixel_size, unit, invert, previous)
|
| 45 |
+
except cb.ImageTooLarge as exc: # a warning; whatever was analysed before stays on the page
|
| 46 |
+
usage.log(ok=False, seconds=round(time.perf_counter() - start, 2), example=example)
|
| 47 |
+
gr.Warning(str(exc))
|
| 48 |
+
return (gr.skip(),) * 6
|
| 49 |
except FiberposeError as exc:
|
| 50 |
usage.log(ok=False, seconds=round(time.perf_counter() - start, 2), example=example)
|
| 51 |
raise gr.Error(str(exc)) from exc
|
|
|
|
| 65 |
with gr.Blocks(title="Fiberpose") as demo:
|
| 66 |
gr.Markdown(
|
| 67 |
"# Fiberpose\nSegmentation and morphometry of fibrous microstructures. Upload a micrograph "
|
| 68 |
+
f"up to {cb.MAX_SIDE} x {cb.MAX_SIDE} px (or click an example) and press **Analyze**. [Source code](https://github.com/vikba/Fiberpose)"
|
| 69 |
)
|
| 70 |
session = gr.State() # this browser tab's analysed image (callbacks.Session)
|
| 71 |
with gr.Row(equal_height=True): # settings, each above the image it applies to
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
button = gr.Button("Analyze", variant="primary")
|
| 89 |
gr.HTML() # empty half: keeps the button at half width
|
| 90 |
+
gr.Examples([[str(path)] for path in EXAMPLE_IMAGE_PATHS], inputs=image)
|
| 91 |
summary = gr.Markdown()
|
| 92 |
with gr.Column(): # under the result
|
| 93 |
with gr.Row():
|
src/fiberpose/web/callbacks.py
CHANGED
|
@@ -13,7 +13,7 @@ import matplotlib
|
|
| 13 |
import numpy as np
|
| 14 |
from matplotlib.figure import Figure
|
| 15 |
|
| 16 |
-
from ..api import AnalysisResult, analyze
|
| 17 |
from ..domain import AppConfig
|
| 18 |
from ..io_export import BatchResultExporter
|
| 19 |
from ..render import render
|
|
@@ -29,6 +29,11 @@ LABELS = { # summary key -> name shown on the page
|
|
| 29 |
"fragments_with_centerlines": "Fibers measured",
|
| 30 |
}
|
| 31 |
ACCENT = "#f97316"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
@dataclass
|
|
@@ -49,6 +54,11 @@ def analyze_file(
|
|
| 49 |
"""Run the pipeline on an image file; a `previous` session of the same file skips its unchanged stages."""
|
| 50 |
same = previous.result if previous and previous.source == path else None
|
| 51 |
image = same.frame.orig_img if same else iio.imread(path) # read from disk to keep the bit depth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
result = analyze(image, config, pixel_size=pixel_size or 1.0, unit=unit, invert=invert, previous=same)
|
| 53 |
return Session(result, path, tempfile.TemporaryDirectory(prefix="fiberpose_"))
|
| 54 |
|
|
|
|
| 13 |
import numpy as np
|
| 14 |
from matplotlib.figure import Figure
|
| 15 |
|
| 16 |
+
from ..api import AnalysisResult, FiberposeError, analyze
|
| 17 |
from ..domain import AppConfig
|
| 18 |
from ..io_export import BatchResultExporter
|
| 19 |
from ..render import render
|
|
|
|
| 29 |
"fragments_with_centerlines": "Fibers measured",
|
| 30 |
}
|
| 31 |
ACCENT = "#f97316"
|
| 32 |
+
MAX_SIDE = int(os.environ.get("FIBERPOSE_MAX_SIDE", 1920)) # largest accepted width and height, px
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ImageTooLarge(FiberposeError):
|
| 36 |
+
"""The image exceeds ``MAX_SIDE``; it is not analysed."""
|
| 37 |
|
| 38 |
|
| 39 |
@dataclass
|
|
|
|
| 54 |
"""Run the pipeline on an image file; a `previous` session of the same file skips its unchanged stages."""
|
| 55 |
same = previous.result if previous and previous.source == path else None
|
| 56 |
image = same.frame.orig_img if same else iio.imread(path) # read from disk to keep the bit depth
|
| 57 |
+
height, width = image.shape[:2]
|
| 58 |
+
if max(height, width) > MAX_SIDE:
|
| 59 |
+
raise ImageTooLarge(
|
| 60 |
+
f"Image is {width} x {height} px; the web version accepts up to {MAX_SIDE} x {MAX_SIDE} px."
|
| 61 |
+
)
|
| 62 |
result = analyze(image, config, pixel_size=pixel_size or 1.0, unit=unit, invert=invert, previous=same)
|
| 63 |
return Session(result, path, tempfile.TemporaryDirectory(prefix="fiberpose_"))
|
| 64 |
|