A Python library for object distance calculations in images using pinhole projection.
Project description
pinhole-distance
A Python library for pinhole distance calculations.
Installation
pip install pinhole-distance
Usage
# Calculate the distance to an object known to be 21" wide that is 70px wide in the image
# Assumes you're using the USB pinhole camera defined in usb_pinhole.py
from pinhole_distance import usb_pinhole
distance = usb_pinhole.distance_to_object(
dimension='x',
actual_dimension=0.5334,
observed_dimension_px=70
)
print(f"Distance to object: {distance:.4f} meters")
API Reference
class Lens
Represents a camera lens.
Attributes:
focal_length(float): Focal length of the lens in millimeters.pixel_width_um(float): Width of a single pixel in micrometers (μm).distortion_table(Optional[dict[float, float]]): Optional mapping of observed radius (pixels or mm) to distortion correction factor (for fisheye lenses).
Example:
lens = Lens(focal_length=4.0, pixel_width_um=1.4, distortion_table={0.0: 1.0, 100.0: 0.98})
class Sensor
Represents a camera sensor.
Attributes:
pixel_height_um(float): Height of a single pixel in micrometers (μm).resolution(tuple[int, int]): Sensor resolution as (width_px, height_px).
Example:
sensor = Sensor(pixel_height_um=1.4, resolution=(1920, 1080))
class Package
Combines a Lens and a Sensor for distance and dimension calculations.
Attributes:
lens(Lens): The lens used.sensor(Sensor): The sensor used.
Methods:
distance_to_object(actual_dimension: float, observed_dimension_px: float) -> float
Returns the distance (in meters) to an object given its actual dimension (height or width, in meters) and observed dimension in the image.object_dimension_at_distance(distance: float, observed_dimension_px: float) -> float
Returns the actual dimension (in meters) of an object at a given distance (in meters), given its observed dimension in the image.
Example:
pkg = Package(lens, sensor)
dist = pkg.distance_to_object(actual_dimension=0.2, observed_dimension_px=150)
dimension = pkg.object_dimension_at_distance(distance=1.0, observed_dimension_px=150)
Example: Using the OV5647 Package
from pinhole_distance import ov5647
distance = ov5647.distance_to_object(
dimension='y',
actual_dimension=0.1e-3, # 0.1 mm in meters
observed_dimension_px=160
)
print(f"Distance to object: {distance:.4f} meters")
Output: Distance to object: 0.0011 meters, or 1.1mm.
Example: USB Pinhole with manually checked distances
The following example computes the distance to a person with:
- Known width of 21" (0.5334m)
- Observed width of 70 pixels, using YOLO bounding box
I know from measuring that the person was 5m away.
from pinhole_distance import usb_pinhole
distance = usb_pinhole.distance_to_object(
dimension='x',
actual_dimension=0.5334,
observed_dimension_px=70
)
print(f"Distance to object: {distance:.4f} meters")
The output is a distance of 4.8986 meters, or roughly a 2% error.
Publishing
- Build:
python -m build - Publish:
twine upload dist/*
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pinhole_distance-0.1.3.tar.gz.
File metadata
- Download URL: pinhole_distance-0.1.3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cce5a1637ac7bcd16e0dbb1e1a673b149d6a43ec9b8afcbb8d97a97bc089b87a
|
|
| MD5 |
d34ed9879cc403728c7ef6a9e0eeafc8
|
|
| BLAKE2b-256 |
e4b37bc18f5e14a2d7fc9a409ec8e54b213cf31ab565b726eea61536107d5ae3
|
Provenance
The following attestation bundles were made for pinhole_distance-0.1.3.tar.gz:
Publisher:
python-publish.yml on micrictor/pinhole-distance
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pinhole_distance-0.1.3.tar.gz -
Subject digest:
cce5a1637ac7bcd16e0dbb1e1a673b149d6a43ec9b8afcbb8d97a97bc089b87a - Sigstore transparency entry: 238589136
- Sigstore integration time:
-
Permalink:
micrictor/pinhole-distance@5f4cd45d67960023841187a477e0a2105fb2c2f0 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/micrictor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@5f4cd45d67960023841187a477e0a2105fb2c2f0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pinhole_distance-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pinhole_distance-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1408cccc2bcced532f4bb85f512288a63ec3cf02c92e3da76f9a434ca2587735
|
|
| MD5 |
45a373f30a6bab97da503758e235eb56
|
|
| BLAKE2b-256 |
c25b8b8c9947a62ca71a93b1448260a7a982d1027f47b9bd902c98eebecd40b7
|
Provenance
The following attestation bundles were made for pinhole_distance-0.1.3-py3-none-any.whl:
Publisher:
python-publish.yml on micrictor/pinhole-distance
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pinhole_distance-0.1.3-py3-none-any.whl -
Subject digest:
1408cccc2bcced532f4bb85f512288a63ec3cf02c92e3da76f9a434ca2587735 - Sigstore transparency entry: 238589140
- Sigstore integration time:
-
Permalink:
micrictor/pinhole-distance@5f4cd45d67960023841187a477e0a2105fb2c2f0 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/micrictor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@5f4cd45d67960023841187a477e0a2105fb2c2f0 -
Trigger Event:
release
-
Statement type: