Fix: .env wird automatisch geladen (MQTT-Events kamen nicht an)
Ohne 'source .env' liefen Broker/Topic auf Defaults (127.0.0.1 statt 192.168.10.7, vehiclecounter/cam1 statt counter/cam1) -> keine Events. Jetzt laedt app.py die .env per python-dotenv beim Start. - load_dotenv() vor dem Auslesen von os.environ - README: .env-Konfig + supervisord-Hinweis (directory=), python-dotenv Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
21
README.md
21
README.md
@@ -28,7 +28,7 @@ Eine webbasierte Anwendung zur Echtzeit-Objekterkennung und -Verfolgung mittels
|
||||
|
||||
2. Erforderliche Python-Pakete installieren:
|
||||
```bash
|
||||
pip3 install flask opencv-python numpy ultralytics requests paho-mqtt
|
||||
pip3 install flask opencv-python numpy ultralytics requests paho-mqtt python-dotenv
|
||||
```
|
||||
|
||||
3. **CUDA-fähiges PyTorch installieren** (für GPU-Beschleunigung). Die passende
|
||||
@@ -58,6 +58,25 @@ python3 app.py
|
||||
|
||||
Die Anwendung ist dann unter `http://localhost:8080` erreichbar.
|
||||
|
||||
### Konfiguration (.env)
|
||||
|
||||
Kamera-, MQTT- und Inferenz-Einstellungen werden über Umgebungsvariablen
|
||||
gesteuert. Lege dazu eine `.env`-Datei im Projektverzeichnis an – sie wird
|
||||
beim Start **automatisch geladen** (`python-dotenv`):
|
||||
|
||||
```bash
|
||||
export CAMERA_URL="http://CAMERA-IP:81/stream"
|
||||
export MQTT_HOST="MQTT-HOST"
|
||||
export MQTT_PORT=1883
|
||||
export MQTT_TOPIC="counter/cam1"
|
||||
export CAMERA_ID="cam1"
|
||||
```
|
||||
|
||||
> ⚠️ Beim Betrieb über einen Prozess-Manager (z. B. **supervisord**) muss das
|
||||
> Arbeitsverzeichnis auf das Projekt zeigen (`directory=/pfad/zum/projekt`),
|
||||
> sonst wird die `.env` nicht gefunden und es greifen die Defaults.
|
||||
> Die `.env` ist per `.gitignore` ausgeschlossen und gehört nicht ins Repo.
|
||||
|
||||
### Webcam-Erkennung
|
||||
|
||||
1. Öffnen Sie `http://localhost:8080` im Browser
|
||||
|
||||
8
app.py
8
app.py
@@ -26,6 +26,14 @@ from flask import (
|
||||
from werkzeug.utils import secure_filename
|
||||
from ultralytics import YOLO
|
||||
|
||||
# .env laden, BEVOR os.environ ausgelesen wird (Broker-/Kamera-/MQTT-Konfig).
|
||||
# load_dotenv versteht auch "export VAR=..."-Zeilen und Inline-Kommentare.
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
except ImportError:
|
||||
print("[config] python-dotenv nicht installiert - .env wird ignoriert", flush=True)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Konfiguration
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user