tarsius 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.
- tarsius-0.1.0/.gitignore +15 -0
- tarsius-0.1.0/LICENSE +202 -0
- tarsius-0.1.0/NOTICE +8 -0
- tarsius-0.1.0/PKG-INFO +106 -0
- tarsius-0.1.0/README.md +78 -0
- tarsius-0.1.0/pyproject.toml +92 -0
- tarsius-0.1.0/scripts/build_model.sh +42 -0
- tarsius-0.1.0/scripts/history_split.py +102 -0
- tarsius-0.1.0/src/tarsius/__init__.py +41 -0
- tarsius-0.1.0/src/tarsius/backends/__init__.py +1 -0
- tarsius-0.1.0/src/tarsius/backends/edges.py +49 -0
- tarsius-0.1.0/src/tarsius/backends/ncnn.py +76 -0
- tarsius-0.1.0/src/tarsius/classifier.py +179 -0
- tarsius-0.1.0/src/tarsius/errors.py +23 -0
- tarsius-0.1.0/src/tarsius/features.py +101 -0
- tarsius-0.1.0/src/tarsius/image.py +33 -0
- tarsius-0.1.0/src/tarsius/models/LICENSE.squeezenet +20 -0
- tarsius-0.1.0/src/tarsius/models/squeezenet_v1.1.bin +0 -0
- tarsius-0.1.0/src/tarsius/models/squeezenet_v1.1.param +50 -0
- tarsius-0.1.0/src/tarsius/py.typed +0 -0
- tarsius-0.1.0/src/tarsius/roi.py +59 -0
- tarsius-0.1.0/src/tarsius/vote.py +27 -0
- tarsius-0.1.0/tests/benchmarks/test_speed.py +66 -0
- tarsius-0.1.0/tests/conftest.py +61 -0
- tarsius-0.1.0/tests/data/golden_squeezenet11.npy +0 -0
- tarsius-0.1.0/tests/test_classifier.py +352 -0
- tarsius-0.1.0/tests/test_ncnn.py +87 -0
- tarsius-0.1.0/tests/test_roi_image.py +87 -0
tarsius-0.1.0/.gitignore
ADDED
tarsius-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
tarsius-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
tarsius
|
|
2
|
+
Copyright 2026 Raphael Hehl
|
|
3
|
+
|
|
4
|
+
This product bundles the SqueezeNet v1.1 model
|
|
5
|
+
(https://github.com/forresti/SqueezeNet), converted to ncnn with fp16
|
|
6
|
+
weights. BSD-2-Clause, see tarsius/models/LICENSE.squeezenet.
|
|
7
|
+
SqueezeNet by Forrest N. Iandola, Song Han, Matthew W. Moskewicz,
|
|
8
|
+
Khalid Ashraf, William J. Dally and Kurt Keutzer.
|
tarsius-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tarsius
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Local, training-free state detection for a region of a camera image
|
|
5
|
+
Project-URL: Repository, https://github.com/RaHehl/tarsius
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/RaHehl/tarsius/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/RaHehl/tarsius/releases
|
|
8
|
+
Author: Raphael Hehl
|
|
9
|
+
License-Expression: Apache-2.0 AND BSD-2-Clause
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
License-File: src/tarsius/models/LICENSE.squeezenet
|
|
13
|
+
Keywords: camera,computer-vision,home-assistant,ncnn,presence
|
|
14
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.14
|
|
23
|
+
Requires-Dist: numpy>=2.0
|
|
24
|
+
Requires-Dist: pillow>=11.0
|
|
25
|
+
Provides-Extra: ncnn
|
|
26
|
+
Requires-Dist: ncnn>=1.0.20260526; extra == 'ncnn'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# tarsius
|
|
30
|
+
|
|
31
|
+
<p align="center">
|
|
32
|
+
<a href="https://github.com/RaHehl/tarsius/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/RaHehl/tarsius/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI"></a>
|
|
33
|
+
<a href="https://codecov.io/gh/RaHehl/tarsius"><img src="https://codecov.io/gh/RaHehl/tarsius/graph/badge.svg" alt="Coverage"></a>
|
|
34
|
+
<a href="https://pypi.org/project/tarsius/"><img src="https://img.shields.io/pypi/v/tarsius" alt="PyPI"></a>
|
|
35
|
+
<a href="https://pypi.org/project/tarsius/"><img src="https://img.shields.io/pypi/pyversions/tarsius" alt="Python"></a>
|
|
36
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-blue" alt="License"></a>
|
|
37
|
+
</p>
|
|
38
|
+
<p align="center">
|
|
39
|
+
<a href="https://github.com/astral-sh/uv"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv"></a>
|
|
40
|
+
<a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff"></a>
|
|
41
|
+
<a href="https://github.com/j178/prek"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json" alt="prek"></a>
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
Local, training-free state detection for a region of a camera image: is the car in the garage, is the mailbox flag up, is the gate open.
|
|
45
|
+
|
|
46
|
+
You confirm a few frames per state, tarsius compares new frames against them and answers with a state or "unsure". Nothing leaves the machine and nothing gets trained.
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from tarsius import Classifier, NcnnBackend, Roi, extract_features, load_image
|
|
50
|
+
|
|
51
|
+
backend = NcnnBackend()
|
|
52
|
+
spot = Roi(0.10, 0.10, 0.60, 0.50)
|
|
53
|
+
classifier = Classifier.for_backend(backend)
|
|
54
|
+
car = extract_features(backend, load_image(car_jpeg), spot)
|
|
55
|
+
empty = extract_features(backend, load_image(empty_jpeg), spot)
|
|
56
|
+
classifier.add("ref-1", car, state="occupied")
|
|
57
|
+
classifier.add("ref-2", empty, state="free")
|
|
58
|
+
|
|
59
|
+
result = classifier.classify(extract_features(backend, load_image(new_jpeg), spot))
|
|
60
|
+
result.status # Status.CONFIDENT, Status.UNSURE or Status.NOT_READY
|
|
61
|
+
result.state # "occupied" or "free" when confident
|
|
62
|
+
result.candidate # best guess, also when unsure
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Store `features.reference().to_bytes()` next to each confirmed frame and restore it with `Reference.from_bytes()`. On `FeaturesError`, extract again from the stored frame.
|
|
66
|
+
|
|
67
|
+
## Backends
|
|
68
|
+
|
|
69
|
+
- `NcnnBackend`: SqueezeNet 1.1 ImageNet features on [ncnn](https://github.com/Tencent/ncnn), install with `tarsius[ncnn]`. Tolerates clutter and lighting changes, a rough region is enough.
|
|
70
|
+
- `EdgeBackend`: numpy only, a baseline. Needs a tight region that still shows some static structure when the object is gone.
|
|
71
|
+
|
|
72
|
+
`NcnnBackend` is the intended default. It stays an optional extra until ncnn ships a release that no longer requires `opencv-python`, which has no musllinux wheels. Without that, tarsius cannot install on Alpine (musl) systems such as Home Assistant OS, whose Core container is Alpine based. ncnn master already made opencv optional ([Tencent/ncnn@0ca5a4b](https://github.com/Tencent/ncnn/commit/0ca5a4b6d)), so the next ncnn release should unblock it.
|
|
73
|
+
|
|
74
|
+
## Results so far
|
|
75
|
+
|
|
76
|
+
One household, two garage cameras, 24 days of recorded history, day and IR night.
|
|
77
|
+
References come from older frames, the test is the newest week.
|
|
78
|
+
The table is the harder camera: a dark car and clutter that gets moved around in front of it (459 frames, 164 in the test week).
|
|
79
|
+
The other camera scores 100 % with both backends.
|
|
80
|
+
|
|
81
|
+
| Backend | Region | 2 confirmed segments per state | 5 per state |
|
|
82
|
+
|---|---|---|---|
|
|
83
|
+
| ncnn | tight | 0 % wrong, 0 % unsure | 0 % wrong, 0 % unsure |
|
|
84
|
+
| ncnn | wide, with clutter | 0 % wrong, 0 % unsure | 0 % wrong, 0 % unsure |
|
|
85
|
+
| edges | tight | 1.8 % wrong, 3.5 % unsure | 0 % wrong, 1.8 % unsure |
|
|
86
|
+
| edges | wide, with clutter | 27 % wrong, 7.6 % unsure | 20 % wrong, 8.5 % unsure |
|
|
87
|
+
|
|
88
|
+
About 20 ms per 4 MP frame including JPEG decoding, for either backend, on an x86 desktop with one thread.
|
|
89
|
+
A small sample, not a guarantee. `scripts/history_split.py` runs the same evaluation on your own recordings.
|
|
90
|
+
|
|
91
|
+
## Development
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
scripts/build_model.sh # once, builds the ncnn model into src/tarsius/models
|
|
95
|
+
uv sync --extra ncnn
|
|
96
|
+
uv run pytest
|
|
97
|
+
uvx prek run --all-files
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The ncnn model is not stored in git. `scripts/build_model.sh` converts the original SqueezeNet 1.1 release with the ncnn tools. Inputs and outputs are checked against pinned sha256 sums, so CI and a local run produce the same files. It needs x86_64 Linux with `curl`, `unzip`, `libgomp1` and `libprotobuf32t64` (Ubuntu 24.04).
|
|
101
|
+
|
|
102
|
+
Releases: every PR gets a label, Release Drafter collects them into a draft, publishing the GitHub release builds and uploads to PyPI.
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
Apache-2.0. The bundled SqueezeNet model is BSD-2-Clause, see NOTICE.
|
tarsius-0.1.0/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# tarsius
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://github.com/RaHehl/tarsius/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/RaHehl/tarsius/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI"></a>
|
|
5
|
+
<a href="https://codecov.io/gh/RaHehl/tarsius"><img src="https://codecov.io/gh/RaHehl/tarsius/graph/badge.svg" alt="Coverage"></a>
|
|
6
|
+
<a href="https://pypi.org/project/tarsius/"><img src="https://img.shields.io/pypi/v/tarsius" alt="PyPI"></a>
|
|
7
|
+
<a href="https://pypi.org/project/tarsius/"><img src="https://img.shields.io/pypi/pyversions/tarsius" alt="Python"></a>
|
|
8
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-blue" alt="License"></a>
|
|
9
|
+
</p>
|
|
10
|
+
<p align="center">
|
|
11
|
+
<a href="https://github.com/astral-sh/uv"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv"></a>
|
|
12
|
+
<a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff"></a>
|
|
13
|
+
<a href="https://github.com/j178/prek"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json" alt="prek"></a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
Local, training-free state detection for a region of a camera image: is the car in the garage, is the mailbox flag up, is the gate open.
|
|
17
|
+
|
|
18
|
+
You confirm a few frames per state, tarsius compares new frames against them and answers with a state or "unsure". Nothing leaves the machine and nothing gets trained.
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from tarsius import Classifier, NcnnBackend, Roi, extract_features, load_image
|
|
22
|
+
|
|
23
|
+
backend = NcnnBackend()
|
|
24
|
+
spot = Roi(0.10, 0.10, 0.60, 0.50)
|
|
25
|
+
classifier = Classifier.for_backend(backend)
|
|
26
|
+
car = extract_features(backend, load_image(car_jpeg), spot)
|
|
27
|
+
empty = extract_features(backend, load_image(empty_jpeg), spot)
|
|
28
|
+
classifier.add("ref-1", car, state="occupied")
|
|
29
|
+
classifier.add("ref-2", empty, state="free")
|
|
30
|
+
|
|
31
|
+
result = classifier.classify(extract_features(backend, load_image(new_jpeg), spot))
|
|
32
|
+
result.status # Status.CONFIDENT, Status.UNSURE or Status.NOT_READY
|
|
33
|
+
result.state # "occupied" or "free" when confident
|
|
34
|
+
result.candidate # best guess, also when unsure
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Store `features.reference().to_bytes()` next to each confirmed frame and restore it with `Reference.from_bytes()`. On `FeaturesError`, extract again from the stored frame.
|
|
38
|
+
|
|
39
|
+
## Backends
|
|
40
|
+
|
|
41
|
+
- `NcnnBackend`: SqueezeNet 1.1 ImageNet features on [ncnn](https://github.com/Tencent/ncnn), install with `tarsius[ncnn]`. Tolerates clutter and lighting changes, a rough region is enough.
|
|
42
|
+
- `EdgeBackend`: numpy only, a baseline. Needs a tight region that still shows some static structure when the object is gone.
|
|
43
|
+
|
|
44
|
+
`NcnnBackend` is the intended default. It stays an optional extra until ncnn ships a release that no longer requires `opencv-python`, which has no musllinux wheels. Without that, tarsius cannot install on Alpine (musl) systems such as Home Assistant OS, whose Core container is Alpine based. ncnn master already made opencv optional ([Tencent/ncnn@0ca5a4b](https://github.com/Tencent/ncnn/commit/0ca5a4b6d)), so the next ncnn release should unblock it.
|
|
45
|
+
|
|
46
|
+
## Results so far
|
|
47
|
+
|
|
48
|
+
One household, two garage cameras, 24 days of recorded history, day and IR night.
|
|
49
|
+
References come from older frames, the test is the newest week.
|
|
50
|
+
The table is the harder camera: a dark car and clutter that gets moved around in front of it (459 frames, 164 in the test week).
|
|
51
|
+
The other camera scores 100 % with both backends.
|
|
52
|
+
|
|
53
|
+
| Backend | Region | 2 confirmed segments per state | 5 per state |
|
|
54
|
+
|---|---|---|---|
|
|
55
|
+
| ncnn | tight | 0 % wrong, 0 % unsure | 0 % wrong, 0 % unsure |
|
|
56
|
+
| ncnn | wide, with clutter | 0 % wrong, 0 % unsure | 0 % wrong, 0 % unsure |
|
|
57
|
+
| edges | tight | 1.8 % wrong, 3.5 % unsure | 0 % wrong, 1.8 % unsure |
|
|
58
|
+
| edges | wide, with clutter | 27 % wrong, 7.6 % unsure | 20 % wrong, 8.5 % unsure |
|
|
59
|
+
|
|
60
|
+
About 20 ms per 4 MP frame including JPEG decoding, for either backend, on an x86 desktop with one thread.
|
|
61
|
+
A small sample, not a guarantee. `scripts/history_split.py` runs the same evaluation on your own recordings.
|
|
62
|
+
|
|
63
|
+
## Development
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
scripts/build_model.sh # once, builds the ncnn model into src/tarsius/models
|
|
67
|
+
uv sync --extra ncnn
|
|
68
|
+
uv run pytest
|
|
69
|
+
uvx prek run --all-files
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The ncnn model is not stored in git. `scripts/build_model.sh` converts the original SqueezeNet 1.1 release with the ncnn tools. Inputs and outputs are checked against pinned sha256 sums, so CI and a local run produce the same files. It needs x86_64 Linux with `curl`, `unzip`, `libgomp1` and `libprotobuf32t64` (Ubuntu 24.04).
|
|
73
|
+
|
|
74
|
+
Releases: every PR gets a label, Release Drafter collects them into a draft, publishing the GitHub release builds and uploads to PyPI.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
Apache-2.0. The bundled SqueezeNet model is BSD-2-Clause, see NOTICE.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "tarsius"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
license = "Apache-2.0 AND BSD-2-Clause"
|
|
5
|
+
license-files = ["LICENSE", "NOTICE", "src/tarsius/models/LICENSE.squeezenet"]
|
|
6
|
+
description = "Local, training-free state detection for a region of a camera image"
|
|
7
|
+
authors = [{ name = "Raphael Hehl" }]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
keywords = ["camera", "computer-vision", "home-assistant", "ncnn", "presence"]
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"numpy>=2.0",
|
|
13
|
+
"pillow>=11.0",
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Topic :: Scientific/Engineering :: Image Recognition",
|
|
22
|
+
"Topic :: Software Development :: Libraries",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
ncnn = ["ncnn>=1.0.20260526"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
"Repository" = "https://github.com/RaHehl/tarsius"
|
|
31
|
+
"Bug Tracker" = "https://github.com/RaHehl/tarsius/issues"
|
|
32
|
+
"Changelog" = "https://github.com/RaHehl/tarsius/releases"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
"mypy>=1.18",
|
|
37
|
+
"pytest>=9",
|
|
38
|
+
"pytest-codspeed>=4",
|
|
39
|
+
"pytest-cov>=7",
|
|
40
|
+
"ruff>=0.17",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[build-system]
|
|
44
|
+
requires = ["hatchling>=1.27"]
|
|
45
|
+
build-backend = "hatchling.build"
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build]
|
|
48
|
+
# The converted model is gitignored (built by scripts/build_model.sh) but ships.
|
|
49
|
+
artifacts = ["src/tarsius/models/*.param", "src/tarsius/models/*.bin"]
|
|
50
|
+
|
|
51
|
+
[tool.hatch.build.targets.wheel]
|
|
52
|
+
packages = ["src/tarsius"]
|
|
53
|
+
|
|
54
|
+
[tool.hatch.build.targets.sdist]
|
|
55
|
+
include = ["src/tarsius", "tests", "scripts", "NOTICE", "README.md"]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
addopts = "-Wdefault --cov=tarsius --cov-report=term-missing:skip-covered"
|
|
59
|
+
pythonpath = ["src"]
|
|
60
|
+
|
|
61
|
+
[tool.coverage.report]
|
|
62
|
+
exclude_lines = [
|
|
63
|
+
"pragma: no cover",
|
|
64
|
+
"if TYPE_CHECKING",
|
|
65
|
+
"raise NotImplementedError",
|
|
66
|
+
"^\\s*\\.\\.\\.$",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[tool.mypy]
|
|
70
|
+
check_untyped_defs = true
|
|
71
|
+
disallow_any_generics = true
|
|
72
|
+
disallow_untyped_defs = true
|
|
73
|
+
warn_unused_ignores = true
|
|
74
|
+
strict_equality = true
|
|
75
|
+
mypy_path = "src/"
|
|
76
|
+
exclude = ["scripts/"]
|
|
77
|
+
|
|
78
|
+
[[tool.mypy.overrides]]
|
|
79
|
+
module = ["ncnn"]
|
|
80
|
+
ignore_missing_imports = true
|
|
81
|
+
|
|
82
|
+
[tool.ruff]
|
|
83
|
+
target-version = "py314"
|
|
84
|
+
line-length = 88
|
|
85
|
+
|
|
86
|
+
[tool.ruff.lint]
|
|
87
|
+
select = ["B", "C4", "D", "E", "F", "I", "N", "PERF", "PL", "RUF", "S", "SIM", "UP", "W"]
|
|
88
|
+
ignore = ["D203", "D212", "PLR2004", "S101"]
|
|
89
|
+
|
|
90
|
+
[tool.ruff.lint.per-file-ignores]
|
|
91
|
+
"tests/**" = ["D", "PLR0913", "S311"]
|
|
92
|
+
"scripts/**" = ["D", "T201", "S"]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Build the bundled SqueezeNet 1.1 ncnn model from the original Caffe release.
|
|
3
|
+
#
|
|
4
|
+
# Sources and outputs are pinned by sha256, so CI and a local run produce the
|
|
5
|
+
# same files. Needs x86_64 Linux with curl, unzip, libgomp1 and, for
|
|
6
|
+
# caffe2ncnn, libprotobuf32t64 (Ubuntu 24.04).
|
|
7
|
+
set -euo pipefail
|
|
8
|
+
|
|
9
|
+
SQUEEZENET_COMMIT=51147dc0681638c28aad593b9e923f3500117125
|
|
10
|
+
SQUEEZENET_URL="https://raw.githubusercontent.com/forresti/SqueezeNet/${SQUEEZENET_COMMIT}/SqueezeNet_v1.1"
|
|
11
|
+
NCNN_TAG=20260526
|
|
12
|
+
NCNN_ZIP="ncnn-${NCNN_TAG}-ubuntu-2404"
|
|
13
|
+
NCNN_URL="https://github.com/Tencent/ncnn/releases/download/${NCNN_TAG}/${NCNN_ZIP}.zip"
|
|
14
|
+
|
|
15
|
+
OUT="$(cd "$(dirname "$0")/.." && pwd)/src/tarsius/models"
|
|
16
|
+
WORK="$(mktemp -d)"
|
|
17
|
+
trap 'rm -rf "$WORK"' EXIT
|
|
18
|
+
|
|
19
|
+
curl -sfL -o "$WORK/deploy.prototxt" "$SQUEEZENET_URL/deploy.prototxt"
|
|
20
|
+
curl -sfL -o "$WORK/squeezenet_v1.1.caffemodel" "$SQUEEZENET_URL/squeezenet_v1.1.caffemodel"
|
|
21
|
+
curl -sfL -o "$WORK/ncnn.zip" "$NCNN_URL"
|
|
22
|
+
(
|
|
23
|
+
cd "$WORK"
|
|
24
|
+
sha256sum -c - <<SUMS
|
|
25
|
+
d041bfb2ab4b32fda4ff6c6966684132f2924e329916aa5bfe9285c6b23e3d1c deploy.prototxt
|
|
26
|
+
72b912ace512e8621f8ff168a7d72af55910d3c7c9445af8dfbff4c2ee960142 squeezenet_v1.1.caffemodel
|
|
27
|
+
c6daf0c8fb2546de420c7b320e2ce62ba764163ace0ba63a2ea0cdfca70d684d ncnn.zip
|
|
28
|
+
SUMS
|
|
29
|
+
unzip -q -j ncnn.zip "${NCNN_ZIP}/bin/caffe2ncnn" "${NCNN_ZIP}/bin/ncnnoptimize" -d bin
|
|
30
|
+
./bin/caffe2ncnn deploy.prototxt squeezenet_v1.1.caffemodel raw.param raw.bin
|
|
31
|
+
# 1 = store weights as fp16, half the size with no measurable accuracy loss.
|
|
32
|
+
./bin/ncnnoptimize raw.param raw.bin squeezenet_v1.1.param squeezenet_v1.1.bin 1
|
|
33
|
+
)
|
|
34
|
+
(
|
|
35
|
+
cd "$WORK"
|
|
36
|
+
sha256sum -c - <<SUMS
|
|
37
|
+
ba925b0bbc9aeaea34baa2a58056990da92ba406250209bce0382f5c66e2725b squeezenet_v1.1.bin
|
|
38
|
+
ca5585520ab1281a44fa27eb06e9851a3dfe882b217978d6cdadddd58356477c squeezenet_v1.1.param
|
|
39
|
+
SUMS
|
|
40
|
+
)
|
|
41
|
+
mkdir -p "$OUT"
|
|
42
|
+
cp "$WORK/squeezenet_v1.1.param" "$WORK/squeezenet_v1.1.bin" "$OUT/"
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Accuracy on recorded history: references from older frames, test on the newest.
|
|
3
|
+
|
|
4
|
+
Data stays outside the repo. TARSIUS_DATA points to a directory with
|
|
5
|
+
raw/<camera>/<YYYYmmddTHHMMSSZ>.jpg, segments.json (frames grouped by closed-door
|
|
6
|
+
segment), labels.json (state per segment, "ignore" to skip one) and rois.json
|
|
7
|
+
(camera -> [l, t, r, b]). Usage: history_split.py <camera> [<camera> ...]
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import pathlib
|
|
13
|
+
import random
|
|
14
|
+
import sys
|
|
15
|
+
import time
|
|
16
|
+
|
|
17
|
+
from tarsius import (
|
|
18
|
+
Classifier,
|
|
19
|
+
EdgeBackend,
|
|
20
|
+
NcnnBackend,
|
|
21
|
+
Roi,
|
|
22
|
+
Status,
|
|
23
|
+
extract_features,
|
|
24
|
+
load_image,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
DATA = pathlib.Path(os.environ["TARSIUS_DATA"])
|
|
28
|
+
SPLIT = os.environ.get("TARSIUS_SPLIT", "20261011")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def load(camera, backend):
|
|
32
|
+
segments = json.loads((DATA / "segments.json").read_text())[camera]
|
|
33
|
+
labels = json.loads((DATA / "labels.json").read_text())[camera]
|
|
34
|
+
roi = Roi(*json.loads((DATA / "rois.json").read_text())[camera])
|
|
35
|
+
rows, spent = [], 0.0
|
|
36
|
+
for index, segment in enumerate(segments):
|
|
37
|
+
state = labels[str(index)]
|
|
38
|
+
if state == "ignore":
|
|
39
|
+
continue
|
|
40
|
+
for name in segment["images"]:
|
|
41
|
+
start = time.perf_counter()
|
|
42
|
+
image = load_image((DATA / "raw" / camera / name).read_bytes())
|
|
43
|
+
features = extract_features(backend, image, roi)
|
|
44
|
+
spent += time.perf_counter() - start
|
|
45
|
+
rows.append((index, name, state, features))
|
|
46
|
+
return rows, spent / len(rows)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def run(rows, backend, budget, seed):
|
|
50
|
+
"""Confirm `budget` older segments per state, then go through the newest frames."""
|
|
51
|
+
older = {}
|
|
52
|
+
for index, name, state, _ in rows:
|
|
53
|
+
if name < SPLIT:
|
|
54
|
+
older.setdefault(state, set()).add(index)
|
|
55
|
+
rng = random.Random(seed)
|
|
56
|
+
chosen = set()
|
|
57
|
+
for ids in older.values():
|
|
58
|
+
chosen |= set(rng.sample(sorted(ids), min(budget, len(ids))))
|
|
59
|
+
classifier = Classifier.for_backend(backend)
|
|
60
|
+
for index, name, state, features in rows:
|
|
61
|
+
if index in chosen:
|
|
62
|
+
classifier.add(name, features, state=state)
|
|
63
|
+
stats = {"n": 0, "wrong": 0, "unsure": 0, "questions": 0}
|
|
64
|
+
asked = set()
|
|
65
|
+
for index, name, state, features in rows:
|
|
66
|
+
if name < SPLIT:
|
|
67
|
+
continue
|
|
68
|
+
result = classifier.classify(features)
|
|
69
|
+
stats["n"] += 1
|
|
70
|
+
if result.status is not Status.CONFIDENT:
|
|
71
|
+
stats["unsure"] += 1
|
|
72
|
+
# Active labelling: one question per segment, the answer is a reference.
|
|
73
|
+
if index not in asked:
|
|
74
|
+
stats["questions"] += 1
|
|
75
|
+
asked.add(index)
|
|
76
|
+
classifier.add(name, features, state=state)
|
|
77
|
+
elif result.state != state:
|
|
78
|
+
stats["wrong"] += 1
|
|
79
|
+
return stats
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def main():
|
|
83
|
+
backends = {"edges": EdgeBackend, "ncnn": NcnnBackend}
|
|
84
|
+
for camera in sys.argv[1:]:
|
|
85
|
+
for name, factory in backends.items():
|
|
86
|
+
backend = factory()
|
|
87
|
+
rows, per_frame = load(camera, backend)
|
|
88
|
+
line = [f"{camera:7} {name:6} {per_frame * 1000:5.0f} ms/frame"]
|
|
89
|
+
for budget in (1, 2, 5):
|
|
90
|
+
total = {"n": 0, "wrong": 0, "unsure": 0, "questions": 0}
|
|
91
|
+
for seed in range(5):
|
|
92
|
+
for key, value in run(rows, backend, budget, seed).items():
|
|
93
|
+
total[key] += value
|
|
94
|
+
line.append(
|
|
95
|
+
f"start {budget}: wrong {total['wrong'] / total['n'] * 100:4.1f} %"
|
|
96
|
+
f" questions {total['questions'] / 5:.1f}"
|
|
97
|
+
)
|
|
98
|
+
print(" | ".join(line), flush=True)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
if __name__ == "__main__":
|
|
102
|
+
main()
|