Python TCP client library for Raspberry Pi Camera Module 2 NoIR
Project description
rpicam-tcp-client
Librería Python para acceder remotamente a la Raspberry Pi Camera Module 2 NoIR mediante TCP.
Características
- Acceso remoto — conecta desde cualquier PC en la misma red
- Parámetros configurables — brillo, contraste, saturación, exposición, etc.
- Escalado y rotación locales —
cv2.resizeycv2.rotateen el cliente - Reconexión automática — reintentos configurables si falla la conexión
- Plug & play — API simple con context managers
- Configuración global —
config.jsoncentraliza host, resolución y parámetros de cada ejemplo - Tests - 98% pytest con mocks (sin RPI real)
Requisitos
En la Raspberry Pi (servidor):
- Raspberry Pi 4 con Ubuntu 24.04 Server
- Camera Module 2 NoIR conectada
- Python 3.10+
picamera2,opencv-python
En tu PC (cliente):
- Python 3.10+
- Conexión de red a la Raspberry Pi
Instalación
Desde PyPI (recomendado)
pip install rpicam-tcp-client
Desde el código fuente (desarrollo)
Cliente (PC)
git clone https://github.com/PabloTarrio/rpicam-tcp-client.git
cd rpicam-tcp-client
pip install -e .
Servidor (RPi)
cd server && pip install -r requirements.txt
sudo cp camara-tcp.service /etc/systemd/system/
sudo systemctl enable --now camara-tcp.service
Primeros pasos
Los ejemplos pueden ejecutarse directamente con --host, pero también admiten un archivo de configuración global para evitar repetir parámetro en cada comando.
# Copiar plantilla de configuración
cp config.example.json config.json
# Editar con la IP de la raspberry PI y preferencias
nano config.json
Con config.json configurado los ejemplos no necesitan --host ni otros parámetros:
python examples/01_basico/mostrar_video.py
Nota:
config.jsonestá en.gitignorepara no subir datos personales al repositorio.
Uso Básico
from rpicam_tcp_client import CameraClient
import cv2
with CameraClient (
host="TU_RASPBERRY_PI_IP",
jpeg_quality= 60,
saturation= 0.8,
rotation= 180,
width= 640,
height= 480,
) as cam:
while True:
frame = cam.get_frame()
cv2.imshow("Cámara", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
Parámetros disponibles
| Parámetro | Tipo | Rango | Dónde se aplica | Descripción |
|---|---|---|---|---|
| jpeg_quality | int | 0-100 | Servidor | Calidad JPEG |
| brightness | float | -1.0 a 1.0 | Servidor | Brillo |
| contrast | float | 0.0-32.0 | Servidor | Contraste |
| saturation | float | 0.0-32.0 | Servidor | Saturación |
| sharpness | float | 0.0-16.0 | Servidor | Nitidez |
| exposure_time | int | 114-694267 µs | Servidor | Exposición manual |
| analogue_gain | float | 1.0-16.0 | Servidor | Ganancia analógica |
| width | int | 64-1920 | Cliente | Escalado (cv2.resize) |
| height | int | 64-1080 | Cliente | Escalado (cv2.resize) |
| rotation | int | 0,90,180,270 | Cliente | Rotación (cv2.rotate) |
Ejemplos
Consulta README.md para la documentación completa.
# Ejemplo 1: vídeo en tiempo real (valores por defecto)
python examples/01_basico/mostrar_video.py --host <RASPBERRY_PI_IP>
# Ejemplo 2: vídeo con parámetros personalizados
python examples/01_basico/mostrar_video_configurado.py --host <RASPBERRY_PI_IP> --width 640 --height 480 --saturation 0.8 --rotation 180
# Ejemplo 3: guardar un frame
python examples/01_basico/guardar_frame.py --host <RASPBERRY_PI_IP>
# Ejemplo 4: grabar video MP4 con timestamp
python examples/02_intermedio/grabar_video.py --host <RASPBERRY_PI_IP> --duration 10 --fps 15 --output video.mp4
# Ejemplo 5: detección de movimiento en tiempo real
python examples/02_intermedio/detectar_movimiento.py --host <RASPBERRY_PI_IP> --umbral 10 --area_minima 200
# Ejemplo 6: secuencia numerada de frames (time-lapse/dataset)
python examples/02_intermedio/guardar_frames.py --host 172.16.127.78 --frames 300 --prefix experimento
# Ejemplo 7: detector de color en tiempo real
python examples/03_avanzado/detector_color.py --host <RASPBERRY_PI_IP> --color rojo
# Ejemplo 8: calibración de cámara con tablero de ajedrez
python examples/03_avanzado/calibrar_camara.py --host <RASPBERRY_PI_IP> --capturas 15
# Ejemplo 9: medición de distancia visual con cámara calibrada
python examples/03_avanzado/medir_distancia_visual.py --host <RASPBERRY_PI_IP> --ancho-real 10
Estructura del proyecto
rpicam-tcp-client/
|___ src/rpicam_tcp_client/
| |___ __init__.py
| |___ client.py
|___ server/
| |___ servidor_camara_tcp.py
| |___ README.md
|___ examples/
| |___config_loader.py
| |___01_basico/
| | |___ guardar_frame.py
| | |___ mostrar_video.py
| | |___ mostrar_video_configurado.py
| |___02_intermedio/
| | |___ grabar_video.py
| | |___ detectar_movimiento.py
| | |___ guardar_frames.py
| |___03_avanzado/
| | |___ detectar_color.py
| | |___ calibrar_camara.py
| | |___ medir_distancia_visual.py
| |___ images/
| |___ README.md
|___ config.example.json
|___ tests/
| |___test_smoke.py
| |___test_camera_client.py
|___pyproject.toml
Desarrollo
# Instalar dependencias de desarrollo
pip install -e ".[dev]"
# Ejecutar tests con cobertura
pytest
# Ejecutar linting
ruff check .
ruff format .
Documentación adicional
- server/README.md - Instalación del servidor en la Raspberry Pi
- examples/README.md - Guía de ejemplos
- CHANGELOG.md - Historial de cambios
Licencia
Este proyecto esta bajo licencia MIT. Ver LICENSE para más detalles.
Project details
Release history Release notifications | RSS feed
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 rpicam_tcp_client-1.0.0.tar.gz.
File metadata
- Download URL: rpicam_tcp_client-1.0.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d64d3f123336cb1be07fb5baf6a0a20e5f639c4d9cc08e8a170c2ca676ed075
|
|
| MD5 |
33f55439edaf140baef7e1b15bddb4c1
|
|
| BLAKE2b-256 |
74867f335757c280d2401ef2ae774f908a038d974b769798c39f83db7cc6c65a
|
File details
Details for the file rpicam_tcp_client-1.0.0-py3-none-any.whl.
File metadata
- Download URL: rpicam_tcp_client-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0007bf51addaa07609f90dbbc54c7fed1f9a488d562e3190cb23959039301e9
|
|
| MD5 |
caed7ab08e32e3667a29dc704dfd1c4e
|
|
| BLAKE2b-256 |
2ef3211f3bc4c40a2a269297347b4175ef6c3241e76a7dab24955a0baaebfc53
|