cap_inspector
cap_inspector is a Python package for inspecting whether bottle caps are open or closed using camera images. It combines OpenCV-based inspection, local operation, gRPC remote control and image streaming, and a GUI viewer.
Its principal goal is stable, reproducible inspection through explicit geometric criteria, not changing the color of the cap. Candidate regions are assessed by shape, tilt, and position; the cap's lift relative to a bottle reference line is then compared with defined acceptance intervals. This approach aims to reduce failures caused by lighting variation. The project targets applications involving red and black caps, but the color-segmentation implementation currently verified in the code uses HSV thresholds for red caps; black-cap segmentation should not be assumed to be implemented.
Features
- Image processing: HSV segmentation, morphological filtering, contours, and rotated-rectangle geometry.
- Open/closed inspection: lift measured in millimeters against separate open and closed acceptance intervals.
- Cached results:
inspect()performs acquisition and classification;is_open()/is_closed()read the latest flags without acquiring another frame. - Configuration: runtime parameter updates and JSON import/export.
- Remote operation: inspection, configuration, JPEG streaming via
grpc-frame, and a PySide6 viewer.
Installation
From the project root:
pip install .
For development, use pip install -e .. The distribution names grpc-frame and xlogger-py differ from their import names grpc_frame and x_logger.
Quick start: local inspection
from cap_inspector import CapInspector
inspector = CapInspector(
camera_id=0,
BOTTLE_RIM_Y=275,
MM_TO_PIXEL=4.0,
current_mode="closed",
)
try:
passed = inspector.inspect("closed")
print("Closed inspection:", passed)
print("Closed:", inspector.is_closed())
print("Opened:", inspector.is_open())
print("Status:", inspector.get_status())
print("Lift (mm):", inspector.get_lift_mm())
finally:
inspector.release()
Use inspect("closed") to test the closed condition and inspect("opened") for the open condition. Both update the open and closed flags using the same frame. is_open() and is_closed() only read the latest inspection result. Use get_status() to distinguish unavailable inspection information from a failed acceptance test.
Recognition and classification algorithm
The current red-cap detection pipeline is:
- Convert BGR to HSV and combine two red hue ranges (H: 0–10 and 170–180; S: 120–255; V: 70–255).
- Apply opening followed by closing with a 5×5 kernel to clean the mask.
- Extract external contours and evaluate those with area above 500 pixels².
- Fit
minAreaRectto each candidate and require tilt below 3° and width/height ratio between 2.3 and 2.8. - Determine the cap bottom from the two lower rectangle vertices and compute lift from the bottle reference line:
lift_mm = (BOTTLE_RIM_Y - cap_bottom_y) / MM_TO_PIXEL
| Parameter | Default | Purpose |
|---|---|---|
BOTTLE_RIM_Y |
275 px | Bottle reference line in image coordinates |
MM_TO_PIXEL |
4.0 px/mm | Image-to-distance conversion |
TARGET_CLOSE_MM |
0.5 mm | Closed-cap target lift |
TARGET_OPEN_MM |
3.5 mm | Open-cap target lift |
TOLERANCE_MM |
1.0 mm | Acceptance tolerance around each target |
The calculated lift must fall within the respective target ± tolerance. Classification is therefore not based solely on whether a colored region exists. Lighting invariance is not guaranteed: thresholds, camera geometry, and the reference line must be appropriate for the installation. The current implementation processes multiple candidates and retains the last candidate as the representative result.
Runtime configuration
inspector.set_parameters({
"BOTTLE_RIM_Y": 275,
"MM_TO_PIXEL": 4.0,
"TARGET_CLOSE_MM": 0.5,
"TARGET_OPEN_MM": 3.5,
"TOLERANCE_MM": 1.0,
"mode": "closed",
})
print(inspector.get_parameters())
inspector.save_parameters("cap_inspector_parameters.json")
inspector.load_parameters("cap_inspector_parameters.json")
Recheck BOTTLE_RIM_Y and MM_TO_PIXEL after changing the camera position or field of view. Changing the inspection mode does not cause status getters to acquire new images.
gRPC server and remote GUI
Run the server and GUI client in separate terminals, adjusting HOST and PORT in the example scripts:
python examples/server_example.py
python examples/gui_client.py
The API is also accessible from a Python client:
from cap_inspector import CapInspectorClient
client = CapInspectorClient("127.0.0.1", 50051)
try:
client.start_camera()
print(client.inspect("closed"))
print(client.get_status())
finally:
client.stop_camera()
client.close()
examples/client_example.py demonstrates remote inspection; examples/parameter_client.py demonstrates JSON synchronization; examples/set_parameters_client.py shows individual setters and bulk updates; and examples/gui_client.py launches the remote viewer. The .pyi type stub is generated and distributed by the package maintainer, not by end users.
API quick reference
| API | Purpose |
|---|---|
inspect(mode) |
Acquire and classify one frame; return the selected-mode result |
process_frame(frame, mode) |
Classify an existing BGR frame |
is_open() / is_closed() |
Read the latest open/closed flags |
is_safe() / get_status() |
Read the selected-mode result and detailed status |
get_lift_mm() / get_detections() |
Read measured lift and candidate data |
get_parameters() / set_parameters(...) |
Inspect and update configuration |
save_parameters(path) / load_parameters(path) |
Export and import JSON configuration |
get_image() / image_stream() |
Obtain JPEG frames or a continuous stream |
start_camera() / stop_camera() / release() |
Manage the camera lifecycle |
License
Apache License 2.0. See LICENSE.
Metadata
Release files for cap-inspector 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cap_inspector-0.1.0.tar.gz | 21.3 kB | With provenance | Details for cap_inspector-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cap_inspector-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for cap_inspector-0.1.0-py3-none-any.whl |
Total release size: 41.4 kB
Release files / cap_inspector-0.1.0.tar.gz
| Download URL | cap_inspector-0.1.0.tar.gz |
|---|---|
| Size | 21.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d24eabb8b86c4f21d99f7f54845db9b49f432e6696ad7080663a0425880d7d81
|
|
BLAKE2b-256 checksum How to use checksums |
debd4240bc909067eabbedbb0022f93742a9c381073e57de70fca3b5ed36ad3b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.
Transparency logRelease files / cap_inspector-0.1.0-py3-none-any.whl
| Download URL | cap_inspector-0.1.0-py3-none-any.whl |
|---|---|
| Size | 20.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3208e55e481d908203c1496d66c12a723c1bc59e4476e4d9996ff7c7a2e21473
|
|
BLAKE2b-256 checksum How to use checksums |
9c519dd81f37166fca3be14adb612bb0231d3585751a10589933d58a7acc9f06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.
Transparency logSecurity for cap-inspector 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for cap_inspector-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.