-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_detection.py
More file actions
994 lines (849 loc) · 39.2 KB
/
Copy pathweb_detection.py
File metadata and controls
994 lines (849 loc) · 39.2 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
import os
import sys
import cv2
import argparse
import time
import queue
import subprocess
import numpy as np
import threading
from fastapi import FastAPI, Response, UploadFile, File, Form, HTTPException
from fastapi.responses import StreamingResponse, FileResponse
import uvicorn
import shutil
from typing import Optional, List
from py_utils.coco_utils import COCO_test_helper
stop_event = threading.Event()
try:
from py_utils.hailo_executor import HailoInfer
HAILO_AVAILABLE = True
except ImportError as e:
HAILO_AVAILABLE = False
print(f"Warning: HailoRT not available ({e}), inference will fail")
OBJ_THRESH = 0.25
NMS_THRESH = 0.45
IMG_SIZE = (640, 640) # (width, height) — must match the .hef input shape
DEFAULT_CLASSES = ("person", "bicycle", "car","motorbike ","aeroplane ","bus ","train","truck ","boat","traffic light",
"fire hydrant","stop sign ","parking meter","bench","bird","cat","dog ","horse ","sheep","cow","elephant",
"bear","zebra ","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite",
"baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife ",
"spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza ","donut","cake","chair","sofa",
"pottedplant","bed","diningtable","toilet ","tvmonitor","laptop ","mouse ","remote ","keyboard ","cell phone","microwave ",
"oven ","toaster","sink","refrigerator ","book","clock","vase","scissors ","teddy bear ","hair drier", "toothbrush ")
CLASSES = DEFAULT_CLASSES
def load_classes(path):
global CLASSES
if not path or not os.path.exists(path):
CLASSES = DEFAULT_CLASSES
return
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read().strip()
import re
items = re.findall(r'"([^"]*)"', content)
if items:
CLASSES = tuple(items)
print(f"Successfully loaded {len(CLASSES)} classes from {path}")
else:
items = [item.strip().strip('"') for item in content.split(',') if item.strip()]
if items:
CLASSES = tuple(items)
print(f"Loaded {len(CLASSES)} classes from {path} (fallback parsing)")
else:
print(f"Warning: No classes found in {path}, using default COCO classes")
CLASSES = DEFAULT_CLASSES
except Exception as e:
print(f"Error loading classes from {path}: {e}. Using default COCO classes")
CLASSES = DEFAULT_CLASSES
class DetectionConfig:
def __init__(self):
self.obj_thresh = 0.25
self.nms_thresh = 0.45
self.lock = threading.Lock()
def update(self, obj_thresh, nms_thresh):
with self.lock:
self.obj_thresh = obj_thresh
self.nms_thresh = nms_thresh
def get(self):
with self.lock:
return self.obj_thresh, self.nms_thresh
det_config = DetectionConfig()
UPLOAD_DIR = "workspace/uploads"
OUTPUT_DIR = "workspace/outputs"
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
class VideoAnalyzer:
def __init__(self, model=None, co_helper=None):
self.model = model
self.co_helper = co_helper
self.is_processing = False
self.progress = 0
self.current_video = ""
self.error_msg = ""
self._stop_event = threading.Event()
self._thread = None
def set_engine(self, model, co_helper):
self.model = model
self.co_helper = co_helper
def start_analysis(self, input_path, output_path):
if self.is_processing:
return False
self._stop_event.clear()
self._thread = threading.Thread(target=self._process_video, args=(input_path, output_path))
self._thread.daemon = True
self._thread.start()
return True
@staticmethod
def _open_writer(output_path, width, height, fps):
"""Try ffmpeg subprocess with libx264 ultrafast first — on Pi 5 this
is ~5x faster than cv2's mp4v at 4K (40ms vs 150ms per frame).
Falls back to cv2 mp4v if ffmpeg isn't installed (e.g., non-Docker run).
Returns (writer, kind) where kind is 'ffmpeg' or 'mp4v'. The caller
uses kind to decide whether to write via proc.stdin.write or out.write."""
cmd = [
'ffmpeg', '-y', '-loglevel', 'error',
'-f', 'rawvideo', '-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', f'{width}x{height}',
'-r', f'{fps}',
'-i', '-',
# ultrafast = simplest motion search; threads=0 lets x264 use all 4 cores.
# NOT using -tune zerolatency: it forces single-thread sliced threading,
# which on Pi 5 cuts throughput in half. For offline encoding we want
# frame-level threading instead.
'-c:v', 'libx264', '-preset', 'ultrafast', '-threads', '0',
'-pix_fmt', 'yuv420p',
'-movflags', '+faststart',
output_path,
]
try:
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE)
print(f"[VideoAnalyzer] Using ffmpeg libx264 ultrafast", flush=True)
return proc, 'ffmpeg'
except FileNotFoundError:
pass
# Fallback: cv2 mp4v (works without ffmpeg binary, but slow at 4K)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
if out.isOpened():
print(f"[VideoAnalyzer] Using cv2 mp4v (slower; install ffmpeg for 5x speedup)", flush=True)
return out, 'mp4v'
out.release()
return None, None
def _process_video(self, input_path, output_path):
self.is_processing = True
self.progress = 0
self.error_msg = ""
self.current_video = os.path.basename(input_path)
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
self.error_msg = f"Error: Cannot open video {input_path}"
self.is_processing = False
return
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
if total_frames <= 0:
self.error_msg = "Error: Invalid total frames"
self.is_processing = False
cap.release()
return
out, kind = self._open_writer(output_path, width, height, fps)
if out is None:
self.error_msg = "Error: No usable video writer (ffmpeg + cv2 mp4v both failed)"
self.is_processing = False
cap.release()
return
# Single-threaded loop — empirical: on Pi 5 with 4K frames, the
# producer/consumer pipeline experiment was 2-3x SLOWER than the
# straight loop. Likely because 4K BGR frames (~24MB each) in a
# Python queue trigger heavy GC, and libavcodec's mp4v encoder
# doesn't play nicely with concurrent libav decoders on the same
# process. The straight loop is the boring-but-fast choice here.
frame_idx = 0
try:
while not self._stop_event.is_set():
ret, frame = cap.read()
if not ret:
break
if self.model and self.co_helper:
processed_img = preprocess_frame(frame, self.co_helper)
outputs = self.model.run(processed_img)
if outputs is not None:
obj, nms = det_config.get()
boxes, classes, scores = post_process_hailo(
outputs, obj, nms, IMG_SIZE[1], IMG_SIZE[0]
)
if boxes is not None:
draw(frame, self.co_helper.get_real_box(boxes), scores, classes)
if kind == 'ffmpeg':
out.stdin.write(frame.tobytes())
else:
out.write(frame)
frame_idx += 1
self.progress = int((frame_idx / total_frames) * 100)
except Exception as e:
self.error_msg = f"Process error: {str(e)}"
finally:
cap.release()
if kind == 'ffmpeg':
try:
out.stdin.close()
except Exception:
pass
try:
out.wait(timeout=30)
except Exception:
out.kill()
else:
out.release()
self.is_processing = False
if not self.error_msg:
self.progress = 100
def stop(self):
self._stop_event.set()
video_analyzer = VideoAnalyzer()
app = FastAPI(title="reComputer Hailo-CV Web Preview (RPi5 + Hailo-8)")
@app.get("/api/config")
async def get_config():
obj, nms = det_config.get()
return {"obj_thresh": obj, "nms_thresh": nms}
@app.post("/api/config")
async def update_config(config: dict):
det_config.update(config.get("obj_thresh", 0.25), config.get("nms_thresh", 0.45))
return {"status": "success"}
@app.post("/api/video/upload")
async def upload_video(file: UploadFile = File(...)):
file_path = os.path.join(UPLOAD_DIR, file.filename)
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return {"filename": file.filename, "status": "uploaded"}
@app.get("/api/video/list")
async def list_videos():
uploads = os.listdir(UPLOAD_DIR)
outputs = os.listdir(OUTPUT_DIR)
return {"uploads": uploads, "outputs": outputs}
@app.post("/api/video/analyze")
async def analyze_video(filename: str = Form(...)):
input_path = os.path.join(UPLOAD_DIR, filename)
if not os.path.exists(input_path):
raise HTTPException(status_code=404, detail="Video not found")
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
raise HTTPException(status_code=400, detail="Cannot open video file")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()
name_base = os.path.splitext(filename)[0]
output_filename = f"{name_base}_{width}x{height}_results.mp4"
output_path = os.path.join(OUTPUT_DIR, output_filename)
success = video_analyzer.start_analysis(input_path, output_path)
if success:
return {"status": "started", "output": output_filename}
else:
return {"status": "error", "message": "Already processing another video"}
@app.get("/api/video/status")
async def get_analysis_status():
return {
"is_processing": video_analyzer.is_processing,
"progress": video_analyzer.progress,
"current_video": video_analyzer.current_video,
"error": video_analyzer.error_msg
}
@app.get("/api/video/download/{filename}")
async def download_video(filename: str):
file_path = os.path.join(OUTPUT_DIR, filename)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(file_path, media_type='video/mp4', filename=filename)
_global_model = None
_global_co_helper = None
@app.post("/api/models/yolov11/predict")
async def predict(
file: Optional[UploadFile] = File(None),
video: Optional[UploadFile] = File(None),
timestamp: Optional[float] = Form(None),
realtime: Optional[bool] = Form(False),
conf: Optional[float] = Form(None),
iou: Optional[float] = Form(None)
):
if _global_model is None or _global_co_helper is None:
return {"success": False, "message": "Model not initialized"}
try:
img = None
source_info = ""
if file:
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
source_info = "uploaded image"
elif video:
import tempfile
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
tmp.write(await video.read())
tmp_path = tmp.name
cap = cv2.VideoCapture(tmp_path)
if cap.isOpened():
if timestamp is not None:
cap.set(cv2.CAP_PROP_POS_MSEC, timestamp * 1000)
ret, frame = cap.read()
if ret:
img = frame
source_info = f"video frame at {timestamp if timestamp else 0}s"
cap.release()
os.unlink(tmp_path)
if img is None:
img = frame_buffer.get_raw_frame()
source_info = "realtime camera frame"
if img is None:
return {"success": False, "message": "No valid input source found (image, video, or camera)"}
h, w = img.shape[:2]
input_img = preprocess_frame(img, _global_co_helper)
outputs = _global_model.run(input_img)
current_obj_thresh, current_nms_thresh = det_config.get()
target_conf = conf if conf is not None else current_obj_thresh
target_iou = iou if iou is not None else current_nms_thresh
boxes, classes, scores = post_process_hailo(
outputs, target_conf, target_iou, IMG_SIZE[1], IMG_SIZE[0]
)
predictions = []
if boxes is not None and len(boxes) > 0:
real_boxes = _global_co_helper.get_real_box(boxes)
for box, score, cl in zip(real_boxes, scores, classes):
predictions.append({
"class": CLASSES[cl],
"confidence": float(score),
"box": {
"x1": int(box[0]),
"y1": int(box[1]),
"x2": int(box[2]),
"y2": int(box[3])
}
})
return {
"success": True,
"source": source_info,
"predictions": predictions,
"image": {
"width": w,
"height": h
}
}
except Exception as e:
return {"success": False, "message": str(e)}
class FrameBuffer:
"""Latest-frame buffer with version tracking.
Two stages separated by version counters:
- annotated: 4K BGR frame from the inference thread, waiting to be encoded.
Encode thread blocks on wait_annotated().
- jpeg: downscaled preview JPEG bytes, ready to ship to browsers.
MJPEG stream consumers block on wait_jpeg().
`raw` mirrors the latest annotated frame for the realtime predict API.
Consumers wake on a condvar instead of polling, so a slow network can't
cause stale-frame pileup.
"""
def __init__(self):
self.raw = None
self.annotated = None
self.annotated_version = 0
self.jpeg = None
self.jpeg_version = 0
self.cond = threading.Condition()
def push_annotated(self, frame):
with self.cond:
self.raw = frame
self.annotated = frame
self.annotated_version += 1
self.cond.notify_all()
def wait_annotated(self, last_version, timeout=1.0):
with self.cond:
self.cond.wait_for(lambda: self.annotated_version > last_version, timeout=timeout)
return self.annotated, self.annotated_version
def push_jpeg(self, jpeg_bytes):
with self.cond:
self.jpeg = jpeg_bytes
self.jpeg_version += 1
self.cond.notify_all()
def wait_jpeg(self, last_version, timeout=1.0):
with self.cond:
self.cond.wait_for(lambda: self.jpeg_version > last_version, timeout=timeout)
return self.jpeg, self.jpeg_version
def get_raw_frame(self):
with self.cond:
return self.raw.copy() if self.raw is not None else None
frame_buffer = FrameBuffer()
class LatestFrameReader:
"""Continuously read a live camera and expose only the newest frame.
USB/V4L2 camera buffers can queue old frames when inference is slower than
capture. A single-threaded read/infer loop then shows stale frames even if
the web stream itself has no queue. This reader drains the camera in the
background and lets inference always consume the latest available frame.
"""
def __init__(self, cap):
self.cap = cap
self.frame = None
self.version = 0
self._last_read_version = 0
self._stopped = False
self._cond = threading.Condition()
self._thread = threading.Thread(target=self._loop, daemon=True)
def start(self):
self._thread.start()
return self
def _loop(self):
while not stop_event.is_set():
ret, frame = self.cap.read()
if not ret:
time.sleep(0.01)
continue
with self._cond:
self.frame = frame
self.version += 1
self._cond.notify_all()
with self._cond:
self._stopped = True
self._cond.notify_all()
def read(self, timeout=1.0):
with self._cond:
self._cond.wait_for(
lambda: self.version > self._last_read_version or self._stopped,
timeout=timeout
)
if self.frame is None:
return False, None
self._last_read_version = self.version
return True, self.frame.copy()
def stop(self):
with self._cond:
self._stopped = True
self._cond.notify_all()
self._thread.join(timeout=2)
@app.get("/api/video_feed")
async def video_feed():
def generate():
last_v = -1
while True:
jpeg, last_v = frame_buffer.wait_jpeg(last_v, timeout=1.0)
if jpeg is not None:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + jpeg + b'\r\n')
return StreamingResponse(generate(), media_type="multipart/x-mixed-replace; boundary=frame")
@app.get("/")
async def index():
return Response(content="""
<html>
<head>
<title>reComputer Hailo-CV Web Preview</title>
<style>
body { background-color: #1a1a1a; color: white; text-align: center; font-family: sans-serif; margin: 0; padding: 20px; }
.container { max-width: 1200px; margin: 0 auto; }
.video-box { margin: 20px auto; display: inline-block; border: 5px solid #333; border-radius: 10px; overflow: hidden; background: #000; width: 100%; max-width: 800px; }
.controls { background: #2a2a2a; padding: 20px; border-radius: 10px; display: inline-block; text-align: left; min-width: 400px; vertical-align: top; margin: 10px; }
.control-group { margin-bottom: 15px; }
.control-group label { display: block; margin-bottom: 5px; font-weight: bold; }
.slider-container { display: flex; align-items: center; gap: 15px; }
input[type=range] { flex-grow: 1; cursor: pointer; }
.value-display { min-width: 50px; font-family: monospace; background: #444; padding: 2px 8px; border-radius: 4px; text-align: center; }
h1 { color: #00e676; }
.tab-container { margin-top: 30px; }
.tabs { display: flex; justify-content: center; margin-bottom: 20px; border-bottom: 2px solid #333; }
.tab { padding: 10px 30px; cursor: pointer; border-bottom: 3px solid transparent; transition: 0.3s; font-weight: bold; }
.tab.active { border-bottom-color: #00e676; color: #00e676; }
.tab-content { display: none; }
.tab-content.active { display: block; }
.video-analysis { text-align: left; background: #2a2a2a; padding: 20px; border-radius: 10px; margin: 10px; }
.btn { background: #00e676; color: #000; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer; font-weight: bold; margin: 5px; }
.btn:hover { background: #00c853; }
.btn:disabled { background: #555; cursor: not-allowed; }
.progress-container { width: 100%; background: #444; border-radius: 10px; margin: 15px 0; height: 20px; position: relative; overflow: hidden; }
.progress-bar { height: 100%; background: #00e676; width: 0%; transition: 0.3s; }
.progress-text { position: absolute; width: 100%; text-align: center; top: 0; left: 0; line-height: 20px; font-size: 12px; font-weight: bold; color: #fff; text-shadow: 1px 1px 2px #000; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { text-align: left; padding: 10px; border-bottom: 1px solid #444; }
th { color: #888; }
</style>
</head>
<body>
<div class="container">
<h1>RPi5 + Hailo-8 Real-time Detection</h1>
<div class="tabs">
<div class="tab active" onclick="showTab('realtime')">Real-time Detection</div>
<div class="tab" onclick="showTab('analysis')">Local Video Analysis</div>
</div>
<div id="realtime" class="tab-content active">
<div class="video-box">
<img id="streamImg" src="/api/video_feed" style="max-width: 100%; height: auto;">
</div>
<div class="controls">
<div class="control-group">
<label>Confidence Threshold</label>
<div class="slider-container">
<input type="range" id="confSlider" min="0.01" max="1.0" step="0.01" value="0.25">
<span id="confValue" class="value-display">0.25</span>
</div>
</div>
<div class="control-group">
<label>IOU Threshold (applied on top of built-in NMS)</label>
<div class="slider-container">
<input type="range" id="iouSlider" min="0.01" max="1.0" step="0.01" value="0.45">
<span id="iouValue" class="value-display">0.45</span>
</div>
</div>
</div>
</div>
<div id="analysis" class="tab-content">
<div class="video-analysis">
<h3>Analyze Local Video</h3>
<div class="control-group">
<label>Upload New Video (.mp4)</label>
<input type="file" id="videoUpload" accept=".mp4">
<button class="btn" onclick="uploadVideo()">Upload</button>
</div>
<div id="processingArea" style="display: none;">
<p id="statusText">Processing: <span id="currentFileName">-</span></p>
<div class="progress-container">
<div id="progressBar" class="progress-bar"></div>
<div id="progressText" class="progress-text">0%</div>
</div>
<p id="errorText" style="color: #ff5252;"></p>
</div>
<div class="control-group">
<label>File Management</label>
<button class="btn" onclick="refreshFileList()">Refresh List</button>
<table>
<thead>
<tr><th>File Name</th><th>Action</th></tr>
</thead>
<tbody id="fileTableBody">
</tbody>
</table>
</div>
</div>
</div>
<p style="color: #888; margin-top: 20px;">Streaming via FastAPI + MJPEG | Port: 8000</p>
</div>
<script>
function showTab(tabId) {
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.getElementById(tabId).classList.add('active');
event.currentTarget.classList.add('active');
if (tabId === 'realtime') {
document.getElementById('streamImg').src = '/api/video_feed';
} else {
document.getElementById('streamImg').src = '';
refreshFileList();
}
}
const confSlider = document.getElementById('confSlider');
const iouSlider = document.getElementById('iouSlider');
const confValue = document.getElementById('confValue');
const iouValue = document.getElementById('iouValue');
function updateConfig() {
const obj_thresh = parseFloat(confSlider.value);
const nms_thresh = parseFloat(iouSlider.value);
confValue.innerText = obj_thresh.toFixed(2);
iouValue.innerText = nms_thresh.toFixed(2);
fetch('/api/config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ obj_thresh, nms_thresh })
});
}
confSlider.oninput = updateConfig;
iouSlider.oninput = updateConfig;
fetch('/api/config').then(res => res.json()).then(data => {
confSlider.value = data.obj_thresh;
iouSlider.value = data.nms_thresh;
confValue.innerText = data.obj_thresh.toFixed(2);
iouValue.innerText = data.nms_thresh.toFixed(2);
});
async function uploadVideo() {
const fileInput = document.getElementById('videoUpload');
if (!fileInput.files[0]) return alert('Please select a file');
const formData = new FormData();
formData.append('file', fileInput.files[0]);
const btn = event.currentTarget;
btn.disabled = true;
btn.innerText = 'Uploading...';
try {
await fetch('/api/video/upload', { method: 'POST', body: formData });
alert('Upload successful');
refreshFileList();
} catch (e) {
alert('Upload failed');
} finally {
btn.disabled = false;
btn.innerText = 'Upload';
}
}
async function refreshFileList() {
const res = await fetch('/api/video/list');
const data = await res.json();
const tbody = document.getElementById('fileTableBody');
tbody.innerHTML = '';
data.uploads.forEach(f => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${f} (Original)</td>
<td><button class="btn" onclick="analyzeVideo('${f}')">Analyze</button></td>
`;
tbody.appendChild(tr);
});
data.outputs.forEach(f => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${f} (Analyzed)</td>
<td><button class="btn" onclick="window.open('/api/video/download/${f}')">Download</button></td>
`;
tbody.appendChild(tr);
});
}
async function analyzeVideo(filename) {
const formData = new FormData();
formData.append('filename', filename);
const res = await fetch('/api/video/analyze', { method: 'POST', body: formData });
const data = await res.json();
if (data.status === 'started') {
startStatusPolling();
} else {
alert(data.message || 'Error starting analysis');
}
}
let pollInterval;
function startStatusPolling() {
document.getElementById('processingArea').style.display = 'block';
if (pollInterval) clearInterval(pollInterval);
pollInterval = setInterval(async () => {
const res = await fetch('/api/video/status');
const data = await res.json();
document.getElementById('currentFileName').innerText = data.current_video;
document.getElementById('progressBar').style.width = data.progress + '%';
document.getElementById('progressText').innerText = data.progress + '%';
document.getElementById('errorText').innerText = data.error || '';
if (!data.is_processing && data.progress === 100) {
clearInterval(pollInterval);
alert('Analysis completed!');
refreshFileList();
} else if (!data.is_processing && data.error) {
clearInterval(pollInterval);
}
}, 1000);
}
fetch('/api/video/status').then(res => res.json()).then(data => {
if (data.is_processing) startStatusPolling();
});
</script>
</body>
</html>
""", media_type="text/html")
def run_fastapi(host, port):
print("\n" + "="*50, flush=True)
print("Registered Routes:", flush=True)
for route in app.routes:
if hasattr(route, "methods"):
print(f"Path: {route.path:35} | Methods: {route.methods}", flush=True)
print("="*50 + "\n", flush=True)
sys.stdout.flush()
uvicorn.run(app, host=host, port=port, log_level="info", log_config=None)
def post_process_hailo(hailo_output, obj_thresh, nms_thresh, input_h, input_w):
"""
Decode the Hailo Model Zoo yolov11 .hef output (built-in NMS post-process layer).
Three output layouts are supported (HailoRT varies by version/build):
1. Python list: output[batch][class_id] -> (N, 5) per-class detections
(HailoRT >= 4.20 default for HAILO_NMS_BY_SCORE format).
2. Object ndarray: same indexing, dtype=object.
3. Dense float32: shape (batch, num_classes, max_dets, 5), zero-padded.
All three reduce to `per_class = output[0]` then iterate by class index.
Each detection row is [ymin, xmin, ymax, xmax, score], normalized to [0, 1]
relative to the network input (e.g. 640x640 after letterboxing).
Returns (boxes_xyxy, classes, scores) in input-image pixel coords. The caller
is expected to run get_real_box() to unletter back to the original frame.
nms_thresh is accepted for API parity with the original RKNN post-process,
but is ignored here because NMS is already baked into the .hef.
"""
if hailo_output is None:
return None, None, None
output = list(hailo_output.values())[0]
boxes, classes, scores = [], [], []
per_class = output[0]
for cls_id, dets in enumerate(per_class):
if dets is None or len(dets) == 0:
continue
for det in dets:
ymin, xmin, ymax, xmax, score = float(det[0]), float(det[1]), float(det[2]), float(det[3]), float(det[4])
if score < obj_thresh:
continue
boxes.append([xmin * input_w, ymin * input_h, xmax * input_w, ymax * input_h])
classes.append(cls_id)
scores.append(score)
if not boxes:
return None, None, None
return np.array(boxes), np.array(classes), np.array(scores)
def draw(image, boxes, scores, classes):
for box, score, cl in zip(boxes, scores, classes):
top, left, right, bottom = [int(_b) for _b in box]
cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
(top, left - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
def preprocess_frame(frame, co_helper):
if getattr(co_helper, "letter_box_info_list", None) is not None:
co_helper.letter_box_info_list.clear()
img = co_helper.letter_box(im=frame.copy(), new_shape=(IMG_SIZE[1], IMG_SIZE[0]), pad_color=(0,0,0))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img
def inference_loop(cap, model, co_helper, is_video_file, target_fps):
"""Capture + preprocess + Hailo inference + draw on full-res frame.
Pushes annotated frames into frame_buffer; the encode thread takes over from there.
`target_fps` caps the loop rate so the preview path doesn't starve other
Hailo/CPU consumers (notably VideoAnalyzer). Hailo can run 100+ fps but
no human needs a 100 fps preview, and the leftover budget keeps offline
video analysis healthy. Pass 0 to disable the cap.
When VideoAnalyzer is running, the loop drops to 1 fps automatically —
a frozen preview is fine for a few seconds, and the freed CPU/Hailo
cycles roughly halve the analyze wall time."""
fps_counter = 0
target_period = 1.0 / target_fps if target_fps > 0 else 0
next_time = time.time()
try:
while not stop_event.is_set():
# Yield to VideoAnalyzer when it's running.
if video_analyzer.is_processing:
time.sleep(1.0)
next_time = time.time()
continue
ret, frame = cap.read()
if not ret:
if is_video_file:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
break
processed_img = preprocess_frame(frame, co_helper)
start_time = time.time()
outputs = model.run(processed_img)
inference_time = time.time() - start_time
if outputs is not None:
obj, nms = det_config.get()
boxes, classes, scores = post_process_hailo(
outputs, obj, nms, IMG_SIZE[1], IMG_SIZE[0]
)
if boxes is not None:
draw(frame, co_helper.get_real_box(boxes), scores, classes)
inf_fps = 1.0 / inference_time if inference_time > 0 else 0
fps_counter = 0.9 * fps_counter + 0.1 * inf_fps if fps_counter > 0 else inf_fps
cv2.putText(frame, f'Hailo FPS: {fps_counter:.1f}', (20, 40),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
frame_buffer.push_annotated(frame)
if target_period > 0:
now = time.time()
next_time += target_period
sleep_for = next_time - now
if sleep_for > 0:
time.sleep(sleep_for)
elif sleep_for < -target_period:
next_time = now + target_period
finally:
stop_event.set()
def encode_loop(preview_w, preview_h, jpeg_quality):
"""Take the latest annotated frame, resize for preview, JPEG-encode, publish.
Runs at its own pace so a slow encode never back-pressures inference."""
last_v = -1
while not stop_event.is_set():
frame, last_v = frame_buffer.wait_annotated(last_v, timeout=1.0)
if frame is None:
continue
h, w = frame.shape[:2]
if preview_w > 0 and preview_h > 0 and (w, h) != (preview_w, preview_h):
preview = cv2.resize(frame, (preview_w, preview_h), interpolation=cv2.INTER_AREA)
else:
preview = frame
ok, buf = cv2.imencode('.jpg', preview, [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_quality])
if ok:
frame_buffer.push_jpeg(buf.tobytes())
def main():
parser = argparse.ArgumentParser(description='YOLOv11 object detection on RPi5 + Hailo-8 (Web Preview Mode)')
parser.add_argument('--model_path', type=str, required=True, help='Path to .hef model (Hailo Executable Format)')
parser.add_argument('--camera_id', type=int, default=0, help='Camera device ID (default: 0 for /dev/video0). Use -1 to disable camera and run web-only mode.')
parser.add_argument('--video_path', type=str, help='Path to video file (overrides camera_id)')
parser.add_argument('--class_path', type=str, help='Path to class_config.txt file for dynamic category loading')
parser.add_argument('--host', type=str, default='0.0.0.0', help='Web server host')
parser.add_argument('--port', type=int, default=8000, help='Web server port')
parser.add_argument('--preview_width', type=int, default=1280, help='MJPEG preview width (0 to disable resize). Default 1280.')
parser.add_argument('--preview_height', type=int, default=720, help='MJPEG preview height (0 to disable resize). Default 720.')
parser.add_argument('--jpeg_quality', type=int, default=80, help='MJPEG preview JPEG quality 1-100. Default 80.')
parser.add_argument('--cam_width', type=int, default=1280, help='Requested USB camera width. Default 1280.')
parser.add_argument('--cam_height', type=int, default=720, help='Requested USB camera height. Default 720.')
parser.add_argument('--target_fps', type=float, default=30.0, help='Cap live preview inference rate (fps). 0 = uncapped. Default 30.')
args = parser.parse_args()
if not HAILO_AVAILABLE:
print("Error: HailoRT is not available. Install the hailort wheel matching your driver version.")
return
if args.class_path:
load_classes(args.class_path)
global _global_model, _global_co_helper
model = HailoInfer(args.model_path)
co_helper = COCO_test_helper(enable_letter_box=True)
_global_model = model
_global_co_helper = co_helper
video_analyzer.set_engine(model, co_helper)
web_thread = threading.Thread(target=run_fastapi, args=(args.host, args.port), daemon=True)
web_thread.start()
print(f"Web Preview started at http://{args.host}:{args.port}", flush=True)
print(f"Preview: {args.preview_width}x{args.preview_height} JPEG q={args.jpeg_quality} | target_fps={args.target_fps}", flush=True)
sys.stdout.flush()
if args.camera_id == -1 and not args.video_path:
print("Running in Video Analysis Mode. Access Web UI to process local videos.", flush=True)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Interrupted by user")
finally:
model.release()
return
if args.video_path:
cap = cv2.VideoCapture(args.video_path)
capture_source = cap
is_video_file = True
else:
cap = cv2.VideoCapture(args.camera_id)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
# Ask the camera to deliver MJPG so USB bandwidth isn't wasted on raw
# YUYV. Most modern USB webcams do MJPG natively; if not, V4L2 quietly
# falls back to YUYV and we just lose this win — no error.
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, args.cam_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, args.cam_height)
capture_source = None
is_video_file = False
if not cap.isOpened():
print(f"Error: Cannot open video source (ID: {args.camera_id if not args.video_path else args.video_path})")
return
if not is_video_file:
capture_source = LatestFrameReader(cap).start()
inf_thread = threading.Thread(target=inference_loop,
args=(capture_source, model, co_helper, is_video_file, args.target_fps),
daemon=True)
enc_thread = threading.Thread(target=encode_loop,
args=(args.preview_width, args.preview_height, args.jpeg_quality),
daemon=True)
inf_thread.start()
enc_thread.start()
try:
while inf_thread.is_alive():
time.sleep(0.5)
except KeyboardInterrupt:
print("Interrupted by user")
finally:
stop_event.set()
if not is_video_file:
capture_source.stop()
inf_thread.join(timeout=2)
enc_thread.join(timeout=2)
cap.release()
model.release()
if __name__ == '__main__':
main()