-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (109 loc) Β· 4.06 KB
/
main.py
File metadata and controls
134 lines (109 loc) Β· 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
"""
Bird Watcher Live Stream v3 β Decoupled camera + YOLO architecture.
Camera runs at full native fps. YOLO runs in a separate thread.
Bounding boxes overlay onto the smooth feed without slowing it down.
Usage: python3 main.py [--port 8888] [--model yolo11s.pt] [--confidence 0.15] [--persist 3]
"""
import ipaddress
import os
import signal
import socket
import logging
import sys
import threading
import cv2
from ultralytics import YOLO
from config import get_config, setup_logging, make_stats, stats_lock
from camera import camera_thread
from detector import yolo_thread
from species_id import verify_moondream
from storage import ensure_directories
from stream_server import app, init_server
logger = logging.getLogger("bird-watcher")
def build_shared_state():
"""Create the thread-safe shared state dictionary."""
return {
"current_frame": None,
"current_frame_lock": threading.Lock(),
"current_boxes": [],
"boxes_lock": threading.Lock(),
"boxes_timestamp": 0,
"stream_jpeg": None,
"stream_lock": threading.Lock(),
"stats": make_stats(),
"stats_lock": stats_lock,
}
def get_local_ip():
"""Detect the local network IP address."""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except OSError:
return "localhost"
finally:
s.close()
def main():
setup_logging()
config = get_config(mode="stream")
ensure_directories(config)
# Load YOLO model
logger.info("Loading %s...", config.model)
model = YOLO(config.model)
logger.info("Model loaded!")
# Check Moondream availability
moondream_available = verify_moondream(config.moondream_url)
# Build shared state
shared_state = build_shared_state()
# Initialize Flask server
init_server(config, shared_state)
local_ip = get_local_ip()
# Check if IP is public (non-RFC1918)
try:
ip_obj = ipaddress.ip_address(local_ip)
if not ip_obj.is_private:
print("β οΈ WARNING: Your IP appears to be public. "
"The camera stream may be accessible from the internet.")
except ValueError:
pass
# Startup banner (user-facing HUD β intentionally print, not logger)
print(f"\n{'=' * 60}")
print(f"π¦ Bird Watcher Live Stream v3 β Decoupled Architecture")
print(f" Camera: Full native fps (decoupled from YOLO)")
print(f" YOLO: {config.model} (runs independently)")
print(f" Moondream: Species ID on detections (5s cooldown)")
print(f" Confidence: {config.confidence}")
print(f" Box persistence: {config.persist}s")
print(f"")
print(f" Local: http://localhost:{config.port}")
print(f" Network: http://{local_ip}:{config.port}")
print(f" Moondream: {'Connected β
' if moondream_available else 'Not available (no species ID)'}")
print(f" Max viewers: {config.max_concurrent_viewers}")
print(f" Max saved frames: {config.max_detection_files}")
print(f"")
print(f" π Stream URL (share this):")
print(f" http://{local_ip}:{config.port}?token={config.stream_token}")
print(f"")
print(f" AirPlay: Open the URL on iPhone β AirPlay to TV")
print(f"{'=' * 60}\n")
# Graceful shutdown handler
def _shutdown_handler(signum, frame):
print("\nπ¦ Shutting down Bird Watcher...")
# Release camera if possible
try:
cap = cv2.VideoCapture(0)
cap.release()
except Exception:
pass
print("Camera released. Goodbye!")
sys.exit(0)
signal.signal(signal.SIGINT, _shutdown_handler)
# Start threads
t_cam = threading.Thread(target=camera_thread, args=(config, shared_state), daemon=True)
t_yolo = threading.Thread(target=yolo_thread, args=(model, config, shared_state, moondream_available), daemon=True)
t_cam.start()
t_yolo.start()
app.run(host='0.0.0.0', port=config.port, threaded=True, debug=False)
if __name__ == '__main__':
main()