Image Pipeline Playground
feat(backend/nodes): add blur detect autofocus score node
Mohammad Rajaei6e1e75693e66
diff --git a/backend/app/nodes/__init__.py b/backend/app/nodes/__init__.pyindex e3d511d..34fc0cd 100644--- a/backend/app/nodes/__init__.py+++ b/backend/app/nodes/__init__.py@@ -3,6 +3,7 @@from app.engine.registry import registryfrom app.nodes.analysis import (AdaptiveThresholdNode,+ BlurDetectNode,CompareHistNode,DistanceTransformNode,DrawHistogramNode,@@ -85,6 +86,7 @@ def register_builtin_nodes() -> None:DrawHistogramNode(),NormalizeNode(),CompareHistNode(),+ BlurDetectNode(),KMeansColorsNode(),DominantColorsHistNode(),FindContoursNode(),diff --git a/backend/app/nodes/analysis.py b/backend/app/nodes/analysis.pyindex 867650f..fb3e837 100644--- a/backend/app/nodes/analysis.py+++ b/backend/app/nodes/analysis.py@@ -415,3 +415,97 @@ class CompareHistNode(BaseNode):f"cv2.putText({dst}, f'{params['method']}: {{_score:.4f}}', (20, 90), ""cv2.FONT_HERSHEY_SIMPLEX, 0.8, (220, 220, 220), 2, cv2.LINE_AA)",]+++class BlurDetectNode(BaseNode):+ type = "blur_detect"+ label = "Blur Detect"+ category = "analysis"+ description = "Estimate blur / focus via Laplacian variance (autofocus score)."+ ports = [image_in(), image_out()]+ params = [+ int_param("ksize", "Kernel Size", 3, minimum=1, maximum=31),+ number_param(+ "threshold",+ "Blur Threshold",+ 100.0,+ minimum=0.0,+ maximum=5000.0,+ description="Scores below this are labeled blurry.",+ ),+ select_param("output", "Output", "overlay", ["overlay", "score_card"]),+ ]++ def execute(+ self,+ inputs: dict[str, np.ndarray | list[np.ndarray] | None],+ params: dict[str, Any],+ seed: int = 0,+ ) -> dict[str, np.ndarray | list[np.ndarray]]:+ image = require_image(inputs)+ gray = _as_gray(image)+ ksize = int(params["ksize"])+ if ksize % 2 == 0:+ ksize += 1+ ksize = max(1, ksize)+ lap = cv2.Laplacian(gray, cv2.CV_64F, ksize=ksize)+ score = float(lap.var())+ threshold = float(params["threshold"])+ label = "sharp" if score >= threshold else "blurry"+ text = f"focus={score:.1f} ({label})"+ if str(params["output"]) == "score_card":+ canvas = np.full((120, 420, 3), 28, dtype=np.uint8)+ color = (80, 220, 120) if label == "sharp" else (80, 80, 255)+ cv2.putText(+ canvas,+ text,+ (16, 70),+ cv2.FONT_HERSHEY_SIMPLEX,+ 0.75,+ color,+ 2,+ cv2.LINE_AA,+ )+ return {"image": canvas}+ if len(image.shape) == 2:+ canvas = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)+ elif image.shape[2] == 4:+ canvas = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)+ else:+ canvas = image.copy()+ color = (80, 220, 120) if label == "sharp" else (80, 80, 255)+ cv2.rectangle(canvas, (8, 8), (min(canvas.shape[1] - 8, 360), 42), (0, 0, 0), -1)+ cv2.putText(+ canvas,+ text,+ (14, 34),+ cv2.FONT_HERSHEY_SIMPLEX,+ 0.7,+ color,+ 2,+ cv2.LINE_AA,+ )+ return {"image": canvas}++ def emit_python(+ self,+ node_id: str,+ params: dict[str, Any],+ input_vars: dict[str, str],+ output_vars: dict[str, str],+ ) -> list[str]:+ src = input_vars["image"]+ dst = output_vars["image"]+ ksize = int(params["ksize"])+ if ksize % 2 == 0:+ ksize += 1+ return [+ f"_gray = {src} if len({src}.shape)==2 else cv2.cvtColor({src}, cv2.COLOR_BGR2GRAY)",+ f"_score = float(cv2.Laplacian(_gray, cv2.CV_64F, ksize={ksize}).var())",+ (+ f"{dst} = {src}.copy() if len({src}.shape)==3 else "+ f"cv2.cvtColor({src}, cv2.COLOR_GRAY2BGR)"+ ),+ f"cv2.putText({dst}, f'focus={{_score:.1f}}', (14, 34), "+ "cv2.FONT_HERSHEY_SIMPLEX, 0.7, (80, 220, 120), 2, cv2.LINE_AA)",+ ]diff --git a/backend/tests/test_analysis_blur_detect.py b/backend/tests/test_analysis_blur_detect.pynew file mode 100644index 0000000..e40f974--- /dev/null+++ b/backend/tests/test_analysis_blur_detect.py@@ -0,0 +1,38 @@+"""Tests for blur-detect analysis node."""++from __future__ import annotations++import numpy as np++from app.engine.registry import registry+from app.nodes import register_builtin_nodes+++def test_blur_detect_registered() -> None:+ register_builtin_nodes()+ assert "blur_detect" in {meta.type for meta in registry.list_metadata()}+++def test_blur_detect_sharp_score_card() -> None:+ register_builtin_nodes()+ image = np.zeros((64, 64), dtype=np.uint8)+ image[:, ::4] = 255+ result = registry.get("blur_detect").execute(+ {"image": image},+ {"ksize": 3, "threshold": 50.0, "output": "score_card"},+ seed=0,+ )["image"]+ assert result.shape == (120, 420, 3)+ assert int(result.sum()) > 0+++def test_blur_detect_overlay_on_blurry() -> None:+ register_builtin_nodes()+ image = np.full((64, 64), 128, dtype=np.uint8)+ result = registry.get("blur_detect").execute(+ {"image": image},+ {"ksize": 3, "threshold": 100.0, "output": "overlay"},+ seed=0,+ )["image"]+ assert result.ndim == 3+ assert result.shape[:2] == image.shapediff --git a/backend/tests/test_nodes.py b/backend/tests/test_nodes.pyindex 92eda51..c2ad4bd 100644--- a/backend/tests/test_nodes.py+++ b/backend/tests/test_nodes.py@@ -40,6 +40,7 @@ def test_builtin_nodes_registered() -> None:"draw_histogram","normalize","compare_hist",+ "blur_detect","kmeans_colors","dominant_colors_hist","find_contours",