-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
476 lines (282 loc) · 9.63 KB
/
server.py
File metadata and controls
476 lines (282 loc) · 9.63 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
"""Local web server that exposes mapping controls and conversion helpers."""
import csv
import sys
import threading
import time
from pathlib import Path
from typing import Dict, List, Optional
import cv2
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse, Response
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from scripts.map_leds import (
MappingConfig,
MappingError,
MappingResult,
run_mapping,
)
from scripts.convert_mapping import (
build_grid,
load_mapping,
)
DATA_DIR = Path("data")
DATA_DIR.mkdir(parents=True, exist_ok=True)
MAPPING_CSV = DATA_DIR / "mapping.csv"
CAMERA_SCAN_LIMIT = 8
app = FastAPI(title="WLED Mapper Service")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/ui", StaticFiles(directory="ui", html=True), name="ui")
app.mount("/data", StaticFiles(directory="data"), name="data")
app.mount("/scripts", StaticFiles(directory="scripts"), name="scripts")
@app.get("/")
def root():
return RedirectResponse("/ui/led_viewer.html")
class MapRequest(BaseModel):
host: str
led_count: int
camera_index: int = 0
segment_index: int = 0
transition_ms: int = 0
prelight_delay: float = 0.3
capture_delay: float = 0.6
postlight_delay: float = 0.2
frames_per_led: int = 5
top_frame_ratio: float = 0.4
min_brightness: float = 30.0
skip_dim_leds: bool = False
timeout: float = 3.0
sleep_every: int = 25
cooldown: float = 2.0
class ConvertRequest(BaseModel):
step: Optional[float] = 25.0
width: Optional[int] = None
height: Optional[int] = None
include_meta: bool = True
STATE = {
"status": "idle",
"message": "Ready",
"current": 0,
"total": 0,
"started_at": None,
"finished_at": None,
"points_captured": 0,
}
STATE_LOCK = threading.Lock()
RESULT: Optional[MappingResult] = None
LIVE_POINTS: List[Dict[str, float]] = []
STOP_EVENT = threading.Event()
def _reset_mapping_csv():
with MAPPING_CSV.open("w", newline="", encoding="ascii") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["led", "x", "y", "brightness"])
def _append_mapping_entry(entry: Dict[str, float]):
new_file = not MAPPING_CSV.exists() or MAPPING_CSV.stat().st_size == 0
with MAPPING_CSV.open("a", newline="", encoding="ascii") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["led", "x", "y", "brightness"])
if new_file:
writer.writeheader()
writer.writerow(entry)
def _try_open_camera(camera_index: int) -> Optional[cv2.VideoCapture]:
if sys.platform.startswith("win"):
backends = [cv2.CAP_MSMF, cv2.CAP_DSHOW, cv2.CAP_ANY]
else:
backends = [cv2.CAP_ANY]
for backend in backends:
try:
cap = cv2.VideoCapture(camera_index, backend)
except Exception: # pylint: disable=broad-except
continue
if cap.isOpened():
return cap
cap.release()
return None
def _list_available_cameras(max_index: int) -> list[int]:
cameras: list[int] = []
for idx in range(max(1, max_index)):
cap = _try_open_camera(idx)
if cap is None:
continue
cap.release()
cameras.append(idx)
return cameras
def _update_state(**kwargs):
with STATE_LOCK:
STATE.update(kwargs)
def _progress_hook(led_index: int, stage: str, payload):
if stage == "captured" and payload:
point = {
"led": int(payload.get("led", led_index)),
"x": float(payload.get("x", 0.0)),
"y": float(payload.get("y", 0.0)),
"brightness": float(payload.get("brightness", 0.0)),
}
with STATE_LOCK:
LIVE_POINTS.append(point)
STATE.update(
{
"status": "running",
"message": "captured",
"current": led_index + 1,
"points_captured": len(LIVE_POINTS),
}
)
_append_mapping_entry(point)
return
if stage == "skipped":
message = "skipped"
if payload and payload.get("reason"):
message = payload["reason"]
_update_state(
status="running",
message=message,
current=max(0, led_index + 1),
)
return
if led_index < 0:
return
_update_state(
status="running",
message=stage,
current=led_index + (0 if stage == "highlight" else 0.5),
)
def _mapping_thread(config: MappingConfig, stop_event: threading.Event):
global RESULT
try:
_update_state(
status="running",
message="Capturing",
current=0,
total=config.led_count,
started_at=time.time(),
)
result = run_mapping(config, hook=_progress_hook, stop_event=stop_event)
RESULT = result
status = "completed"
message = "Capture finished"
current = config.led_count
if result.stopped:
status = "stopped"
message = "Capture stopped"
current = len(result.entries)
_update_state(
status=status,
message=message,
finished_at=time.time(),
current=current,
)
except (MappingError, Exception) as exc: # pylint: disable=broad-except
RESULT = None
_update_state(status="error", message=str(exc), finished_at=time.time())
finally:
stop_event.clear()
@app.post("/api/map")
def start_mapping(req: MapRequest):
with STATE_LOCK:
if STATE["status"] == "running":
raise HTTPException(status_code=409, detail="Mapping already in progress")
STATE.update(
{
"status": "starting",
"message": "Launching capture...",
"current": 0,
"total": req.led_count,
"started_at": None,
"finished_at": None,
"points_captured": 0,
}
)
LIVE_POINTS.clear()
_reset_mapping_csv()
STOP_EVENT.clear()
config = MappingConfig(**req.model_dump())
thread = threading.Thread(
target=_mapping_thread, args=(config, STOP_EVENT), daemon=True
)
thread.start()
return {"ok": True}
@app.get("/api/status")
def get_status():
with STATE_LOCK:
data = STATE.copy()
return data
@app.get("/api/cameras")
def get_cameras(max_index: int = CAMERA_SCAN_LIMIT):
limit = max(1, min(max_index, 24))
cameras = _list_available_cameras(limit)
return {"cameras": [{"index": idx} for idx in cameras], "scanned": limit}
@app.get("/api/camera_preview/{camera_index}")
def camera_preview(camera_index: int, width: int = 640):
cap = _try_open_camera(camera_index)
if cap is None:
raise HTTPException(status_code=404, detail="Camera not available")
success, frame = cap.read()
cap.release()
if not success or frame is None:
raise HTTPException(status_code=500, detail="Failed to capture frame")
target_width = max(160, min(width, 1280))
height, width_px = frame.shape[:2]
if width_px != target_width:
scale = target_width / max(1, width_px)
new_height = max(1, int(height * scale))
frame = cv2.resize(frame, (target_width, new_height))
ok, encoded = cv2.imencode(".jpg", frame)
if not ok:
raise HTTPException(status_code=500, detail="Failed to encode frame")
return Response(content=encoded.tobytes(), media_type="image/jpeg")
@app.get("/api/live_points")
def get_live_points(after: int = -1):
with STATE_LOCK:
total = len(LIVE_POINTS)
if after >= 0:
points = [pt for pt in LIVE_POINTS if pt["led"] > after]
else:
points = list(LIVE_POINTS)
return {"points": points, "total": total}
@app.post("/api/stop")
def stop_mapping():
with STATE_LOCK:
if STATE["status"] not in {"running", "starting"}:
raise HTTPException(status_code=409, detail="No mapping in progress")
STOP_EVENT.set()
return {"ok": True}
@app.get("/api/result")
def get_result():
if RESULT is None:
raise HTTPException(status_code=404, detail="No capture result available")
return {"entries": RESULT.entries}
@app.post("/api/convert")
def convert(req: ConvertRequest):
if not MAPPING_CSV.exists():
raise HTTPException(
status_code=404, detail="mapping.csv not found, run capture first"
)
rows = load_mapping(MAPPING_CSV)
if req.step is None and req.width is None and req.height is None:
raise HTTPException(status_code=400, detail="Provide step or width/height")
effective_step = req.step if req.step is not None else 10.0
grid = build_grid(rows, effective_step, req.width, req.height)
# Do not write ledmap.json on server; UI handles download formatting
payload = {
"map": grid["map"],
"width": grid["width"],
"height": grid["height"],
"meta": grid["meta"],
}
return payload
@app.post("/api/gaps")
def generate_gaps(req: ConvertRequest):
data = convert(req)
gap_map = [1 if value != -1 else -1 for value in data["map"]]
# Do not write 2d-gaps.json on server; UI handles download formatting
return gap_map
if __name__ == "__main__":
import uvicorn
uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=False)