shopna-point 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shopna_point-0.1.0/LICENSE +21 -0
- shopna_point-0.1.0/PKG-INFO +111 -0
- shopna_point-0.1.0/README.md +87 -0
- shopna_point-0.1.0/pyproject.toml +41 -0
- shopna_point-0.1.0/setup.cfg +4 -0
- shopna_point-0.1.0/src/shopna_point/__init__.py +34 -0
- shopna_point-0.1.0/src/shopna_point/_runtime.py +45 -0
- shopna_point-0.1.0/src/shopna_point/api.py +157 -0
- shopna_point-0.1.0/src/shopna_point/cli.py +111 -0
- shopna_point-0.1.0/src/shopna_point/config.py +450 -0
- shopna_point-0.1.0/src/shopna_point/estimation.py +1068 -0
- shopna_point-0.1.0/src/shopna_point/evaluation.py +548 -0
- shopna_point-0.1.0/src/shopna_point/external.py +688 -0
- shopna_point-0.1.0/src/shopna_point/features.py +997 -0
- shopna_point-0.1.0/src/shopna_point/matching.py +490 -0
- shopna_point-0.1.0/src/shopna_point/preprocess.py +266 -0
- shopna_point-0.1.0/src/shopna_point/synth.py +84 -0
- shopna_point-0.1.0/src/shopna_point.egg-info/PKG-INFO +111 -0
- shopna_point-0.1.0/src/shopna_point.egg-info/SOURCES.txt +22 -0
- shopna_point-0.1.0/src/shopna_point.egg-info/dependency_links.txt +1 -0
- shopna_point-0.1.0/src/shopna_point.egg-info/entry_points.txt +2 -0
- shopna_point-0.1.0/src/shopna_point.egg-info/requires.txt +10 -0
- shopna_point-0.1.0/src/shopna_point.egg-info/top_level.txt +1 -0
- shopna_point-0.1.0/tests/test_package.py +86 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SHOPNA-Point contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shopna-point
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: SHOPNA-Point: robust feature detection, description and homography estimation for restaurant menu-card images
|
|
5
|
+
Author: SHOPNA-Point contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: keypoint,matching,homography,opencv,numba,computer-vision,menu-card
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: numpy<3,>=1.24
|
|
16
|
+
Requires-Dist: opencv-python-headless>=4.8
|
|
17
|
+
Requires-Dist: numba>=0.61
|
|
18
|
+
Requires-Dist: tqdm>=4.60
|
|
19
|
+
Provides-Extra: gpu
|
|
20
|
+
Requires-Dist: numba-cuda>=0.2; extra == "gpu"
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# SHOPNA-Point
|
|
26
|
+
|
|
27
|
+
Robust feature detection, description and homography estimation for
|
|
28
|
+
restaurant menu-card images. SHOPNA-Point is a NumPy/Numba/OpenCV algorithm
|
|
29
|
+
with GPU (CUDA) kernels available as an optional extra. It ships with three
|
|
30
|
+
ready-made presets (**accuracy**, **balanced**, **speed**) plus a
|
|
31
|
+
**device_performance** auto-tier mode, and supports arbitrary custom
|
|
32
|
+
overrides without recompiling anything.
|
|
33
|
+
|
|
34
|
+
The package is extracted line-by-line from the `experimental-shopna-point-gpu`
|
|
35
|
+
Kaggle notebook (see `tools/extract_notebook.py` for the provenance map), so
|
|
36
|
+
a plain run reproduces the pinned benchmark trace (120/120 images, zero
|
|
37
|
+
regressions vs the baseline; gate: `harness/diff_trace.py`).
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install shopna-point # CPU (Numba JIT)
|
|
43
|
+
pip install "shopna-point[gpu]" # + CUDA kernels (numba-cuda)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quick start
|
|
47
|
+
|
|
48
|
+
Match one image pair:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import cv2
|
|
52
|
+
from shopna_point import match
|
|
53
|
+
|
|
54
|
+
ref = cv2.imread("menu_ref.jpg")
|
|
55
|
+
test = cv2.imread("menu_test.jpg")
|
|
56
|
+
|
|
57
|
+
res = match(ref, test, mode="balanced") # accuracy | balanced | speed
|
|
58
|
+
if res["status"] == "success":
|
|
59
|
+
H = res["H"] # 3x3 homography
|
|
60
|
+
print(res["inliers"], "inliers,", round(res["reproj"], 2), "px")
|
|
61
|
+
else:
|
|
62
|
+
print("not matched:", res["status"])
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Run the benchmark harnesses (identical to the notebook cells 8-11):
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from shopna_point import run_external_validation, run_main_validation
|
|
69
|
+
|
|
70
|
+
run_external_validation("path/to/dataset", num_samples=10, start=1, end=10)
|
|
71
|
+
run_main_validation(test_root="...", reference_root="...")
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Command line:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
shopna-point info
|
|
78
|
+
shopna-point match menu_ref.jpg menu_test.jpg --mode accuracy
|
|
79
|
+
shopna-point validate --dataset-dir path/to/dataset --num-samples 7790 --start 1 --end 2600
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Customization contract
|
|
83
|
+
|
|
84
|
+
* `set_mode("accuracy" | "balanced" | "speed" | "device_performance")` switches
|
|
85
|
+
the active preset at runtime.
|
|
86
|
+
* `update_config({...})` merges arbitrary CONFIG keys on top of the active
|
|
87
|
+
preset (partial override).
|
|
88
|
+
* `register_mode("myname", {...})` registers a new full preset usable by
|
|
89
|
+
`set_mode`.
|
|
90
|
+
* `get_config()` returns the active merged config.
|
|
91
|
+
* `seed_all()` applies the reproducibility seeds (random + cv2 USAC; numpy is
|
|
92
|
+
pinned at import like the notebook).
|
|
93
|
+
|
|
94
|
+
> New-API note: `match()` is the package convenience entry point. The
|
|
95
|
+
> byte-identical, gate-verified entry points are `run_external_validation`
|
|
96
|
+
> and `run_main_validation`. `match()` skips the ground-truth-driven FixA
|
|
97
|
+
> retry/rescue branches by design (no GT available per image).
|
|
98
|
+
|
|
99
|
+
## Determinism / reproduction
|
|
100
|
+
|
|
101
|
+
`run_external_validation(..., start=, end=)` supports chunked runs; chunks
|
|
102
|
+
are deterministic and disjoint, so a 7790-image run can be split into
|
|
103
|
+
sequential chunks. Use `SHOPNA_OUTPUT_DIR` (default: current directory) for
|
|
104
|
+
results and `SHOPNA_EXTERNAL_DATASET_DIR` for the dataset root. The CPU
|
|
105
|
+
pipeline is deterministic given the RNG discipline; GPU kernels are
|
|
106
|
+
numerically equivalent but CUDA reductions are not bit-exact, so use
|
|
107
|
+
`gpu_enabled()` to know which path ran.
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT — see `LICENSE`.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# SHOPNA-Point
|
|
2
|
+
|
|
3
|
+
Robust feature detection, description and homography estimation for
|
|
4
|
+
restaurant menu-card images. SHOPNA-Point is a NumPy/Numba/OpenCV algorithm
|
|
5
|
+
with GPU (CUDA) kernels available as an optional extra. It ships with three
|
|
6
|
+
ready-made presets (**accuracy**, **balanced**, **speed**) plus a
|
|
7
|
+
**device_performance** auto-tier mode, and supports arbitrary custom
|
|
8
|
+
overrides without recompiling anything.
|
|
9
|
+
|
|
10
|
+
The package is extracted line-by-line from the `experimental-shopna-point-gpu`
|
|
11
|
+
Kaggle notebook (see `tools/extract_notebook.py` for the provenance map), so
|
|
12
|
+
a plain run reproduces the pinned benchmark trace (120/120 images, zero
|
|
13
|
+
regressions vs the baseline; gate: `harness/diff_trace.py`).
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install shopna-point # CPU (Numba JIT)
|
|
19
|
+
pip install "shopna-point[gpu]" # + CUDA kernels (numba-cuda)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
Match one image pair:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import cv2
|
|
28
|
+
from shopna_point import match
|
|
29
|
+
|
|
30
|
+
ref = cv2.imread("menu_ref.jpg")
|
|
31
|
+
test = cv2.imread("menu_test.jpg")
|
|
32
|
+
|
|
33
|
+
res = match(ref, test, mode="balanced") # accuracy | balanced | speed
|
|
34
|
+
if res["status"] == "success":
|
|
35
|
+
H = res["H"] # 3x3 homography
|
|
36
|
+
print(res["inliers"], "inliers,", round(res["reproj"], 2), "px")
|
|
37
|
+
else:
|
|
38
|
+
print("not matched:", res["status"])
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Run the benchmark harnesses (identical to the notebook cells 8-11):
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from shopna_point import run_external_validation, run_main_validation
|
|
45
|
+
|
|
46
|
+
run_external_validation("path/to/dataset", num_samples=10, start=1, end=10)
|
|
47
|
+
run_main_validation(test_root="...", reference_root="...")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Command line:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
shopna-point info
|
|
54
|
+
shopna-point match menu_ref.jpg menu_test.jpg --mode accuracy
|
|
55
|
+
shopna-point validate --dataset-dir path/to/dataset --num-samples 7790 --start 1 --end 2600
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Customization contract
|
|
59
|
+
|
|
60
|
+
* `set_mode("accuracy" | "balanced" | "speed" | "device_performance")` switches
|
|
61
|
+
the active preset at runtime.
|
|
62
|
+
* `update_config({...})` merges arbitrary CONFIG keys on top of the active
|
|
63
|
+
preset (partial override).
|
|
64
|
+
* `register_mode("myname", {...})` registers a new full preset usable by
|
|
65
|
+
`set_mode`.
|
|
66
|
+
* `get_config()` returns the active merged config.
|
|
67
|
+
* `seed_all()` applies the reproducibility seeds (random + cv2 USAC; numpy is
|
|
68
|
+
pinned at import like the notebook).
|
|
69
|
+
|
|
70
|
+
> New-API note: `match()` is the package convenience entry point. The
|
|
71
|
+
> byte-identical, gate-verified entry points are `run_external_validation`
|
|
72
|
+
> and `run_main_validation`. `match()` skips the ground-truth-driven FixA
|
|
73
|
+
> retry/rescue branches by design (no GT available per image).
|
|
74
|
+
|
|
75
|
+
## Determinism / reproduction
|
|
76
|
+
|
|
77
|
+
`run_external_validation(..., start=, end=)` supports chunked runs; chunks
|
|
78
|
+
are deterministic and disjoint, so a 7790-image run can be split into
|
|
79
|
+
sequential chunks. Use `SHOPNA_OUTPUT_DIR` (default: current directory) for
|
|
80
|
+
results and `SHOPNA_EXTERNAL_DATASET_DIR` for the dataset root. The CPU
|
|
81
|
+
pipeline is deterministic given the RNG discipline; GPU kernels are
|
|
82
|
+
numerically equivalent but CUDA reductions are not bit-exact, so use
|
|
83
|
+
`gpu_enabled()` to know which path ran.
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT — see `LICENSE`.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shopna-point"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "SHOPNA-Point: robust feature detection, description and homography estimation for restaurant menu-card images"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "SHOPNA-Point contributors" }]
|
|
13
|
+
keywords = ["keypoint", "matching", "homography", "opencv", "numba", "computer-vision", "menu-card"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Topic :: Scientific/Engineering :: Image Recognition",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"numpy>=1.24,<3",
|
|
22
|
+
"opencv-python-headless>=4.8",
|
|
23
|
+
"numba>=0.61",
|
|
24
|
+
"tqdm>=4.60",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
gpu = ["numba-cuda>=0.2"]
|
|
29
|
+
dev = ["pytest>=8"]
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
shopna-point = "shopna_point.cli:main"
|
|
33
|
+
|
|
34
|
+
[tool.setuptools]
|
|
35
|
+
package-dir = { "" = "src" }
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
where = ["src"]
|
|
39
|
+
|
|
40
|
+
[tool.pytest.ini_options]
|
|
41
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""SHOPNA-Point: robust feature detection, description and matching.
|
|
3
|
+
|
|
4
|
+
Public API (plan_pip_package.md):
|
|
5
|
+
- match(ref_img, test_img, mode=None, **overrides) -> per-image result dict
|
|
6
|
+
- extract_shopna_point / extract_brightness_dual -> features
|
|
7
|
+
- estimate_homography_shopna -> PHR-GCV estimator
|
|
8
|
+
- run_external_validation / run_main_validation -> benchmark harnesses
|
|
9
|
+
- set_mode / update_config / register_mode / get_config / gpu_enabled
|
|
10
|
+
- seed_all / HAS_GPU / CONFIG / _MODE_PRESETS
|
|
11
|
+
"""
|
|
12
|
+
from ._runtime import HAS_GPU, seed_all, SEED
|
|
13
|
+
from .config import (
|
|
14
|
+
CONFIG, _USE_GPU, _MODE_PRESETS,
|
|
15
|
+
set_mode, update_config, register_mode, get_config, gpu_enabled,
|
|
16
|
+
)
|
|
17
|
+
from .features import extract_shopna_point, extract_brightness_dual
|
|
18
|
+
from .matching import hybrid_attention_match
|
|
19
|
+
from .estimation import estimate_homography_shopna
|
|
20
|
+
from .evaluation import run_main_validation
|
|
21
|
+
from .external import run_external_validation
|
|
22
|
+
from .api import match
|
|
23
|
+
|
|
24
|
+
__version__ = "0.1.0"
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"HAS_GPU", "seed_all", "SEED",
|
|
28
|
+
"CONFIG", "_USE_GPU", "_MODE_PRESETS",
|
|
29
|
+
"set_mode", "update_config", "register_mode", "get_config", "gpu_enabled",
|
|
30
|
+
"extract_shopna_point", "extract_brightness_dual",
|
|
31
|
+
"hybrid_attention_match", "estimate_homography_shopna",
|
|
32
|
+
"run_main_validation", "run_external_validation",
|
|
33
|
+
"match", "__version__",
|
|
34
|
+
]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# SHOPNA-Point runtime: imports, RNG discipline, GPU probe (notebook cell 1).
|
|
3
|
+
import os
|
|
4
|
+
import csv
|
|
5
|
+
import math
|
|
6
|
+
import time
|
|
7
|
+
import json
|
|
8
|
+
import numpy as np
|
|
9
|
+
import cv2
|
|
10
|
+
from tqdm import tqdm
|
|
11
|
+
import concurrent.futures
|
|
12
|
+
from concurrent.futures import ProcessPoolExecutor, as_completed
|
|
13
|
+
import numba as nb
|
|
14
|
+
import random
|
|
15
|
+
|
|
16
|
+
# Determinism discipline: identical RNG seeds as harness/local_runner.py so a
|
|
17
|
+
# plain run-all reproduces the pinned baseline (cv2 USAC + numpy).
|
|
18
|
+
# The package exposes seed_all(); the run_* validation entry points call it
|
|
19
|
+
# before executing (the notebook seeds once in cell 1, before cells 8/11).
|
|
20
|
+
SEED = 20260807
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def seed_all(seed=SEED):
|
|
24
|
+
# Apply the SHOPNA-Point determinism seeds (random, cv2 USAC).
|
|
25
|
+
# NOTE: numpy's global stream is deliberately NOT re-seeded here. The
|
|
26
|
+
# notebook pins it ONCE in cell 5 (`np.random.seed(42)` at module level,
|
|
27
|
+
# before the _SHOPNA_P1/_P2 pattern generation) and every later consumer
|
|
28
|
+
# (warm-up draws, synthetic noise) continues that stream. Re-seeding here
|
|
29
|
+
# would diverge the synthetic-noise realization from the notebook baseline.
|
|
30
|
+
import random
|
|
31
|
+
random.seed(seed)
|
|
32
|
+
cv2.setRNGSeed(seed)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# --- GPU Support ---
|
|
36
|
+
try:
|
|
37
|
+
from numba import cuda
|
|
38
|
+
HAS_GPU = cuda.is_available()
|
|
39
|
+
if HAS_GPU:
|
|
40
|
+
print(f"[GPU] detected: {cuda.get_current_device().name}")
|
|
41
|
+
else:
|
|
42
|
+
print("[GPU] CUDA available but no GPU detected. Falling back to CPU.")
|
|
43
|
+
except Exception:
|
|
44
|
+
HAS_GPU = False
|
|
45
|
+
print("[GPU] Numba CUDA not available. Using CPU-only mode.")
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""SHOPNA-Point per-image public API: match(ref, test).
|
|
3
|
+
|
|
4
|
+
Same single rule as the validation harnesses (external.py / evaluation.py):
|
|
5
|
+
degradation detection -> brightness-dual extraction -> hybrid attention
|
|
6
|
+
matcher -> pyramid augmentation -> degradation-adaptive gates -> PHR-GCV
|
|
7
|
+
estimator -> core sieve.
|
|
8
|
+
|
|
9
|
+
Differences from the harness pipeline (documented):
|
|
10
|
+
* no ground truth, so the FixA-M2 retry/rescue branches (which adopt a
|
|
11
|
+
candidate only when it strictly beats the GT metric) are skipped;
|
|
12
|
+
* no synthetic transform generation; the test image is used as-is.
|
|
13
|
+
|
|
14
|
+
Config discipline: the ACTIVE preset/overrides are applied for the call and
|
|
15
|
+
restored afterwards, so match() is safe to call from any mode.
|
|
16
|
+
"""
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
import cv2
|
|
21
|
+
|
|
22
|
+
from ._runtime import seed_all
|
|
23
|
+
from .config import CONFIG, set_mode, update_config
|
|
24
|
+
from .features import extract_brightness_dual, _make_gamma_lut, _GAMMA_LUTS
|
|
25
|
+
from .matching import hybrid_attention_match
|
|
26
|
+
from .estimation import estimate_homography_shopna
|
|
27
|
+
from .preprocess import (
|
|
28
|
+
resize, detect_image_degradation, normalize_illumination,
|
|
29
|
+
compute_adaptive_gates, _pyramid_augment, _core_sieve,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def match(ref_img, test_img, mode=None, **overrides):
|
|
34
|
+
"""Register reference <-> test image pair and estimate the homography.
|
|
35
|
+
|
|
36
|
+
ref_img / test_img : BGR or grayscale uint8 arrays.
|
|
37
|
+
mode : ready-made preset name (None = keep the ACTIVE mode).
|
|
38
|
+
**overrides : CONFIG keys applied ON TOP of the mode preset.
|
|
39
|
+
|
|
40
|
+
Returns a dict:
|
|
41
|
+
status : "success" | "no_features" | "not_enough_matches"
|
|
42
|
+
| "homography_failed" | "not_enough_inliers"
|
|
43
|
+
H : 3x3 homography (None unless status == "success")
|
|
44
|
+
mask : inlier mask over the match pairs (None unless success)
|
|
45
|
+
inliers, n_matches, reproj(px) : metrics
|
|
46
|
+
ref_keypoints, test_keypoints, matches : for inspection/plotting
|
|
47
|
+
"""
|
|
48
|
+
seed_all()
|
|
49
|
+
_saved_mode = CONFIG.get("EXECUTION_MODE")
|
|
50
|
+
_saved_tier = CONFIG.get("DEVICE_TIER")
|
|
51
|
+
_saved_forced = CONFIG.get("DEVICE_TIER_FORCED")
|
|
52
|
+
try:
|
|
53
|
+
if mode is not None:
|
|
54
|
+
set_mode(mode)
|
|
55
|
+
if overrides:
|
|
56
|
+
update_config(overrides)
|
|
57
|
+
return _run_pair(ref_img, test_img)
|
|
58
|
+
finally:
|
|
59
|
+
CONFIG["EXECUTION_MODE"] = _saved_mode
|
|
60
|
+
CONFIG["DEVICE_TIER"] = _saved_tier
|
|
61
|
+
CONFIG["DEVICE_TIER_FORCED"] = _saved_forced
|
|
62
|
+
if _saved_tier is not None and _saved_tier in _presets_of():
|
|
63
|
+
for _k, _v in _presets_of()[_saved_tier].items():
|
|
64
|
+
CONFIG[_k] = _v
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _presets_of():
|
|
68
|
+
from .config import _MODE_PRESETS
|
|
69
|
+
return _MODE_PRESETS
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _run_pair(ref_img, test_img):
|
|
73
|
+
# Reference is always processed at MAX_DIM (identical to the harness).
|
|
74
|
+
ref_img = cv2.cvtColor(ref_img, cv2.COLOR_BGR2GRAY) if ref_img.ndim == 3 else ref_img
|
|
75
|
+
test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY) if test_img.ndim == 3 else test_img
|
|
76
|
+
if ref_img is None or test_img is None:
|
|
77
|
+
return {"status": "no_image", "H": None, "mask": None,
|
|
78
|
+
"inliers": 0, "n_matches": 0, "reproj": 0.0,
|
|
79
|
+
"ref_keypoints": None, "test_keypoints": None, "matches": []}
|
|
80
|
+
|
|
81
|
+
ref_img = resize(ref_img, CONFIG["MAX_DIM"])
|
|
82
|
+
# Harness semantics: the test frame is the SAME resolution as the resized
|
|
83
|
+
# reference (the synthetic transforms in the harness never change size).
|
|
84
|
+
test_img = resize(test_img, CONFIG["MAX_DIM"])
|
|
85
|
+
|
|
86
|
+
image_is_blurry, image_is_noisy = detect_image_degradation(test_img, ref_img)
|
|
87
|
+
use_relaxed = image_is_blurry or image_is_noisy
|
|
88
|
+
|
|
89
|
+
p1, d1 = extract_brightness_dual(ref_img)
|
|
90
|
+
if CONFIG.get("ILLUMINATION_NORM", True):
|
|
91
|
+
test_img_n = normalize_illumination(test_img, ref_img)
|
|
92
|
+
p2, d2 = extract_brightness_dual(
|
|
93
|
+
test_img_n if CONFIG.get("ILLUMINATION_NORM", True) else test_img,
|
|
94
|
+
denoise=image_is_noisy)
|
|
95
|
+
|
|
96
|
+
if d1 is None or d2 is None:
|
|
97
|
+
return {"status": "no_features", "H": None, "mask": None,
|
|
98
|
+
"inliers": 0, "n_matches": 0, "reproj": 0.0,
|
|
99
|
+
"ref_keypoints": p1, "test_keypoints": p2, "matches": []}
|
|
100
|
+
|
|
101
|
+
img_std = np.std(test_img) / 255.0
|
|
102
|
+
good = hybrid_attention_match(p1, d1, p2, d2, img_std,
|
|
103
|
+
is_blurry=image_is_blurry, is_noisy=image_is_noisy)
|
|
104
|
+
|
|
105
|
+
if CONFIG.get("PYRAMID_MATCH", True):
|
|
106
|
+
_ext, p2, d2 = _pyramid_augment(
|
|
107
|
+
p1, d1, p2, d2,
|
|
108
|
+
test_img_n if CONFIG.get("ILLUMINATION_NORM", True) else test_img,
|
|
109
|
+
CONFIG.get("PYRAMID_FACTORS", [0.7, 1.4]),
|
|
110
|
+
img_std, image_is_blurry, image_is_noisy)
|
|
111
|
+
if _ext:
|
|
112
|
+
good = good + _ext
|
|
113
|
+
|
|
114
|
+
min_matches = CONFIG["MIN_MATCHES_BLUR"] if use_relaxed else CONFIG["MIN_MATCHES"]
|
|
115
|
+
if len(good) < min_matches:
|
|
116
|
+
return {"status": "not_enough_matches", "H": None, "mask": None,
|
|
117
|
+
"inliers": 0, "n_matches": len(good), "reproj": 0.0,
|
|
118
|
+
"ref_keypoints": p1, "test_keypoints": p2, "matches": good}
|
|
119
|
+
|
|
120
|
+
src = np.float32([p1[m[0]] for m in good])
|
|
121
|
+
dst = np.float32([p2[m[1]] for m in good])
|
|
122
|
+
scores = np.float32([m[2] for m in good])
|
|
123
|
+
|
|
124
|
+
if CONFIG.get("ADAPTIVITY_MODE", False):
|
|
125
|
+
min_inl, thresh = compute_adaptive_gates(test_img, ref_img,
|
|
126
|
+
image_is_blurry, image_is_noisy)
|
|
127
|
+
else:
|
|
128
|
+
min_inl = CONFIG["MIN_INLIERS_RELAXED"] if use_relaxed else CONFIG["MIN_INLIERS"]
|
|
129
|
+
thresh = CONFIG["RANSAC_THRESH_RELAXED"] if use_relaxed else CONFIG["RANSAC_THRESH"]
|
|
130
|
+
|
|
131
|
+
H_est, mask, _med = estimate_homography_shopna(src, dst, scores, thresh, min_inl,
|
|
132
|
+
is_relaxed=use_relaxed)
|
|
133
|
+
if H_est is None or mask is None:
|
|
134
|
+
return {"status": "homography_failed", "H": None, "mask": None,
|
|
135
|
+
"inliers": 0, "n_matches": len(good), "reproj": 0.0,
|
|
136
|
+
"ref_keypoints": p1, "test_keypoints": p2, "matches": good}
|
|
137
|
+
|
|
138
|
+
if CONFIG.get("CORE_SIEVE", True):
|
|
139
|
+
H_est, mask = _core_sieve(src, dst, H_est, mask, thresh, min_inl,
|
|
140
|
+
mult=float(CONFIG.get("CORE_SIEVE_MULT", 2.0)))
|
|
141
|
+
|
|
142
|
+
inl = int(mask.sum())
|
|
143
|
+
if inl < min_inl:
|
|
144
|
+
return {"status": "not_enough_inliers", "H": None, "mask": None,
|
|
145
|
+
"inliers": inl, "n_matches": len(good), "reproj": 0.0,
|
|
146
|
+
"ref_keypoints": p1, "test_keypoints": p2, "matches": good}
|
|
147
|
+
|
|
148
|
+
# Reprojection error measured on inliers (identity-GT convention used by
|
|
149
|
+
# the harness: mean |H@src - dst| over inliers).
|
|
150
|
+
src_i = src[mask.ravel() == 1]
|
|
151
|
+
dst_i = dst[mask.ravel() == 1]
|
|
152
|
+
est_warp = cv2.perspectiveTransform(src_i.reshape(-1, 1, 2), H_est).reshape(-1, 2)
|
|
153
|
+
reproj = float(np.mean(np.linalg.norm(est_warp - dst_i, axis=1)))
|
|
154
|
+
|
|
155
|
+
return {"status": "success", "H": H_est, "mask": mask,
|
|
156
|
+
"inliers": inl, "n_matches": len(good), "reproj": reproj,
|
|
157
|
+
"ref_keypoints": p1, "test_keypoints": p2, "matches": good}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""SHOPNA-Point command line interface (console script: shopna-point)."""
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import cv2
|
|
9
|
+
|
|
10
|
+
from ._runtime import HAS_GPU, seed_all
|
|
11
|
+
from .config import (
|
|
12
|
+
CONFIG, _MODE_PRESETS, get_config, gpu_enabled, set_mode, update_config,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _common(p):
|
|
17
|
+
p.add_argument("--mode", default=None,
|
|
18
|
+
choices=sorted(_MODE_PRESETS) + ["device_performance"],
|
|
19
|
+
help="ready-made preset (default: active CONFIG mode)")
|
|
20
|
+
p.add_argument("--config", default=None, metavar="JSON",
|
|
21
|
+
help='CONFIG overrides as JSON, e.g. \'{"LIMIT": 50}\'')
|
|
22
|
+
p.add_argument("--output-dir", default=None,
|
|
23
|
+
help="results dir (default: SHOPNA_OUTPUT_DIR or cwd)")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _apply(overrides_json):
|
|
27
|
+
if overrides_json:
|
|
28
|
+
update_config(json.loads(overrides_json))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def cmd_match(a):
|
|
32
|
+
ref = cv2.imread(a.ref)
|
|
33
|
+
test = cv2.imread(a.test)
|
|
34
|
+
if ref is None or test is None:
|
|
35
|
+
print("error: could not read an input image", file=sys.stderr)
|
|
36
|
+
sys.exit(2)
|
|
37
|
+
from .api import match
|
|
38
|
+
_apply(a.config)
|
|
39
|
+
res = match(ref, test, mode=a.mode)
|
|
40
|
+
out = {"status": res["status"], "H": None if res["H"] is None
|
|
41
|
+
else res["H"].tolist(), "inliers": res["inliers"],
|
|
42
|
+
"n_matches": res["n_matches"], "reproj_px": res["reproj"]}
|
|
43
|
+
print(json.dumps(out, indent=2))
|
|
44
|
+
if out["status"] != "success":
|
|
45
|
+
sys.exit(1)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def cmd_main(a):
|
|
49
|
+
from .evaluation import run_main_validation
|
|
50
|
+
_apply(a.config)
|
|
51
|
+
results, failed, trace = run_main_validation(
|
|
52
|
+
test_root=a.test_root, reference_root=a.reference_root,
|
|
53
|
+
output_dir=a.output_dir, limit=a.limit)
|
|
54
|
+
for row in results:
|
|
55
|
+
print("\t".join(str(c) for c in row))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def cmd_external(a):
|
|
59
|
+
from .external import run_external_validation
|
|
60
|
+
_apply(a.config)
|
|
61
|
+
run_external_validation(a.dataset_dir, num_samples=a.num_samples,
|
|
62
|
+
start=a.start, end=a.end)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def cmd_info(_):
|
|
66
|
+
print("shopna-point %s" % __import__("shopna_point").__version__)
|
|
67
|
+
print("GPU kernels : %s (gpu_enabled=%s)" % (
|
|
68
|
+
"available" if HAS_GPU else "not available", gpu_enabled()))
|
|
69
|
+
print("active mode : %s (tier=%s)" % (
|
|
70
|
+
CONFIG.get("EXECUTION_MODE"), CONFIG.get("DEVICE_TIER")))
|
|
71
|
+
for k, v in get_config().items():
|
|
72
|
+
print(" %-24s %r" % (k, v))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def main(argv=None):
|
|
76
|
+
p = argparse.ArgumentParser(
|
|
77
|
+
prog="shopna-point",
|
|
78
|
+
description="SHOPNA-Point: robust keypoint matching and homography "
|
|
79
|
+
"estimation for menu-card images.")
|
|
80
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
81
|
+
|
|
82
|
+
mp = sub.add_parser("match", help="match one ref/test image pair")
|
|
83
|
+
mp.add_argument("ref")
|
|
84
|
+
mp.add_argument("test")
|
|
85
|
+
_common(mp)
|
|
86
|
+
mp.set_defaults(func=cmd_match)
|
|
87
|
+
|
|
88
|
+
mvp = sub.add_parser("main", help="run the main-track validation loop")
|
|
89
|
+
mvp.add_argument("--test-root", default=None)
|
|
90
|
+
mvp.add_argument("--reference-root", default=None)
|
|
91
|
+
mvp.add_argument("--limit", type=int, default=None)
|
|
92
|
+
_common(mvp)
|
|
93
|
+
mvp.set_defaults(func=cmd_main)
|
|
94
|
+
|
|
95
|
+
evp = sub.add_parser("validate", help="run the external-dataset validation")
|
|
96
|
+
evp.add_argument("--dataset-dir", default=None)
|
|
97
|
+
evp.add_argument("--num-samples", type=int, default=500)
|
|
98
|
+
evp.add_argument("--start", type=int, default=1)
|
|
99
|
+
evp.add_argument("--end", type=int, default=None)
|
|
100
|
+
_common(evp)
|
|
101
|
+
evp.set_defaults(func=cmd_external)
|
|
102
|
+
|
|
103
|
+
ip = sub.add_parser("info", help="print the active CONFIG")
|
|
104
|
+
ip.set_defaults(func=cmd_info)
|
|
105
|
+
|
|
106
|
+
args = p.parse_args(argv)
|
|
107
|
+
args.func(args)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
if __name__ == "__main__":
|
|
111
|
+
main()
|