-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_captions.py
More file actions
1332 lines (1241 loc) · 52.6 KB
/
face_captions.py
File metadata and controls
1332 lines (1241 loc) · 52.6 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
995
996
997
998
999
1000
"""
Face-following captions: subtitles anchored near your face that follow you.
- Real-time webcam + face detection
- Speech-to-text captions in Minecraft-style font
- Emotion-based emoji and styling
"""
import argparse
import json
import cv2
import numpy as np
import re
import threading
import queue
import time
import os
import sys
from collections import OrderedDict
# Optional WebSocket control API
try:
import asyncio
import websockets
HAS_WEBSOCKETS = True
except ImportError:
HAS_WEBSOCKETS = False
# GPU resize if OpenCV built with CUDA (optional)
try:
HAS_CUDA = hasattr(cv2, "cuda") and cv2.cuda.getCudaEnabledDeviceCount() > 0
except Exception:
HAS_CUDA = False
# Load .env so DEEPGRAM_API_KEY (and others) are set before STT starts
try:
from dotenv import load_dotenv
_env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
load_dotenv(_env_path)
except ImportError:
pass
# Real-time speech: use project library (streaming when possible)
try:
from realtime_stt import StreamingSTT, RESULT_FINAL, RESULT_PARTIAL, get_caption_mode
HAS_STT = True
except ImportError:
HAS_STT = False
try:
from PIL import Image, ImageDraw, ImageFont
HAS_PIL = True
except ImportError:
HAS_PIL = False
# MediaPipe Face Mesh (optional): better tracking + real emotion from blendshapes
try:
from face_mesh import (
create_face_landmarker,
detect_face,
emotion_from_blendshapes,
HAS_MEDIAPIPE as HAS_FACE_MESH_MODULE,
)
HAS_FACE_MESH = HAS_FACE_MESH_MODULE
except ImportError:
HAS_FACE_MESH = False
create_face_landmarker = None
detect_face = None
emotion_from_blendshapes = None
# Optional translation (pip install googletrans==4.0.0-rc1; may conflict with deepgram's httpcore)
try:
from googletrans import Translator
_translator = Translator()
HAS_TRANSLATE = True
except (ImportError, AttributeError):
_translator = None
HAS_TRANSLATE = False
# --- Config (overridden by config.json and CLI) ---
CAMERA_INDEX = 0
CAPTION_FONT_SIZE = 52
CAPTION_MAX_WIDTH = 620
CAPTION_OFFSET_ABOVE_HEAD = 55
MAX_CAPTION_LEN = 220
CAPTION_TIMEOUT_SEC = 4.5
EMOTION_SMOOTH = 0.3
FONT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fonts")
CAMERA_RESOLUTIONS = [(1280, 720), (1920, 1080), (960, 540), (0, 0)]
DISPLAY_SIZE = (1280, 720)
FACE_DETECT_SCALE = 0.28
FACE_DETECT_EVERY_N = 6
FACE_SMOOTH = 0.18
MAX_CAPTION_LINES = 2
REVEAL_CHARS_PER_FRAME = 9999 # Effectively instant: show full caption as soon as STT delivers (smoother, faster)
CAPTION_BG_COLOR = (45, 42, 34, 230)
CAPTION_BG_PADDING = 12
CAPTION_BORDER_LIGHT = (90, 85, 72, 255)
CAPTION_BORDER_DARK = (20, 18, 15, 255)
CAPTION_BORDER_PX = 2
CAPTION_SCALE_MIN = 0.55
CAPTION_SCALE_MAX = 1.85
EMOTIONS = {
"happy": ("😊", "yellow"),
"sad": ("😢", "blue"),
"surprised": ("😲", "orange"),
"angry": ("😠", "red"),
"neutral": ("😐", "white"),
}
EMOTION_COLORS = {
"yellow": (45, 45, 15, 220),
"blue": (15, 25, 45, 220),
"orange": (50, 35, 15, 220),
"red": (50, 20, 20, 220),
"white": (35, 35, 35, 200),
}
SHOW_FACE_BOX = False
SPEECH_BUBBLE_TAIL = True
CAPTION_HISTORY_LINES = 2
FADE_IN_FRAMES = 2
CAPTION_POS_SMOOTH = 0.28
EMOTION_HOLD_FRAMES = 3
TRANSLATION_DEST = "es"
def get_minecraft_font(size: int):
"""Load Minecraft-style font; fallback to default if missing."""
script_dir = os.path.dirname(os.path.abspath(__file__))
search_dirs = [script_dir, FONT_DIR]
names = ["Minecraft.ttf", "Minecraftia.ttf", "MinecraftRegular.ttf"]
for d in search_dirs:
for name in names:
path = os.path.join(d, name)
if os.path.isfile(path):
try:
return ImageFont.truetype(path, size)
except Exception:
pass
try:
return ImageFont.truetype("arial.ttf", size)
except Exception:
return ImageFont.load_default()
def get_cyberpunk_font(size: int, preferred_path: str = None):
"""Load font for Cyberpunk style. Use cyberpunk_font from config.json, or fallback to Rajdhani/Orbitron."""
script_dir = os.path.dirname(os.path.abspath(__file__))
search_dirs = [script_dir, FONT_DIR]
if preferred_path:
candidates = [preferred_path] if os.path.isabs(preferred_path) else [os.path.join(d, preferred_path) for d in search_dirs]
for path in candidates:
if os.path.isfile(path):
try:
return ImageFont.truetype(path, size)
except Exception:
pass
names = ["Rajdhani-Regular.ttf", "Rajdhani-Medium.ttf", "Rajdhani-SemiBold.ttf", "Orbitron-Regular.ttf", "Cyberpunk-Regular.ttf", "ShareTechMono-Regular.ttf", "OCR-A.ttf", "OCRA.ttf"]
for d in search_dirs:
for name in names:
path = os.path.join(d, name)
if os.path.isfile(path):
try:
return ImageFont.truetype(path, size)
except Exception:
pass
font_names = ["Consolas", "Segoe UI", "Arial", "arial.ttf"]
for name in font_names:
try:
return ImageFont.truetype(name, size)
except Exception:
pass
try:
return ImageFont.truetype("arial.ttf", size)
except Exception:
return ImageFont.load_default()
def _wrap_text(text: str, font, max_width: int) -> list:
"""Simple word wrap; returns list of lines."""
words = text.split()
if not words:
return [text] if text else [""]
lines = []
current = ""
for word in words:
candidate = (current + " " + word).strip() if current else word
try:
bbox = font.getbbox(candidate)
w = bbox[2] - bbox[0]
if w <= max_width:
current = candidate
else:
if current:
lines.append(current)
current = word if font.getbbox(word)[2] - font.getbbox(word)[0] <= max_width else candidate
except Exception:
lines.append(candidate)
current = ""
if current:
lines.append(current)
return lines or [text]
def render_caption_pil(
text: str,
font_size: int,
bg_color: tuple = None,
speech_bubble: bool = True,
max_width: int = None,
padding: int = None,
) -> np.ndarray:
"""Render caption text in up to 2 lines with emotion-tinted box and optional tail."""
if not text or not HAS_PIL:
return None
font = get_minecraft_font(font_size)
use_max_width = max_width if max_width is not None else CAPTION_MAX_WIDTH
pad = padding if padding is not None else CAPTION_BG_PADDING
if "\n" in text:
all_lines = []
for phrase in text.split("\n"):
phrase = (phrase or "").strip()
if phrase:
all_lines.extend(_wrap_text(phrase, font, use_max_width))
max_total = 2
lines = all_lines[-max_total:]
else:
lines = _wrap_text(text, font, use_max_width)
if len(lines) > MAX_CAPTION_LINES:
lines = lines[-MAX_CAPTION_LINES:]
line_height = font_size + 4
try:
max_w = max(font.getbbox(line)[2] - font.getbbox(line)[0] for line in lines)
except Exception:
max_w = use_max_width
total_w = max_w + 2 * pad
box_h = line_height * len(lines) + 2 * pad
tail_h = max(6, 10 * font_size // 52) if speech_bubble else 0
total_h = box_h + tail_h
fill_color = bg_color if bg_color is not None else CAPTION_BG_COLOR
img = Image.new("RGBA", (total_w, total_h), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.rectangle([(0, 0), (total_w - 1, box_h - 1)], fill=fill_color, outline=None)
b = CAPTION_BORDER_PX
dark = (0, 0, 0)
for i in range(b):
draw.line([(i, i), (total_w - 1 - i, i)], fill=(255, 255, 255))
draw.line([(i, i), (i, box_h - 1 - i)], fill=(255, 255, 255))
draw.line([(i, box_h - 1 - i), (total_w - 1 - i, box_h - 1 - i)], fill=dark)
draw.line([(total_w - 1 - i, i), (total_w - 1 - i, box_h - 1 - i)], fill=dark)
fc = fill_color if isinstance(fill_color, (tuple, list)) and len(fill_color) >= 3 else (45, 42, 34)
inner_shadow_color = (
max(0, fc[0] - 15),
max(0, fc[1] - 15),
max(0, fc[2] - 15),
)
for i in range(1):
offset = b + i
draw.line([(offset, offset), (total_w - 1 - offset, offset)], fill=inner_shadow_color)
draw.line([(offset, offset), (offset, box_h - 1 - offset)], fill=inner_shadow_color)
if speech_bubble and tail_h:
cx = total_w // 2
tail_w = 12
draw.polygon(
[(cx - tail_w, box_h - 1), (cx + tail_w, box_h - 1), (cx, total_h - 1)],
fill=fill_color,
outline=None,
)
draw.line([(cx - tail_w, total_h - 1), (cx + tail_w, total_h - 1)], fill=dark)
shadow_offsets = [(2, 2), (2, -2), (-2, 2), (-2, -2)]
for i, line in enumerate(lines):
y = pad + i * line_height
for dx, dy in shadow_offsets:
draw.text((pad + dx, y + dy), line, font=font, fill=(0, 0, 0, 200))
draw.text((pad + 1, y + 1), line, font=font, fill=(0, 0, 0, 255))
draw.text((pad, y), line, font=font, fill=(255, 255, 255, 255))
return np.array(img)
# Cyberpunk 2077 style: dark bg, neon cyan/magenta, chamfered corners, scan lines
CYBERPUNK_BG = (10, 10, 10, 153) # #0A0A0A ~60% opacity
CYBERPUNK_BORDER = (0, 215, 242) # #02d7f2 cyan
CYBERPUNK_GLOW = (0, 255, 255) # #00FFFF bright cyan
CYBERPUNK_TEXT = (255, 255, 255)
CYBERPUNK_ACCENT_MAGENTA = (255, 0, 255)
def _draw_chamfered_box(draw, x0, y0, w, h, chamfer: int, fill, outline=None, width=1):
"""Draw a box with chamfered (cut) corners, CP2077 style."""
c = min(chamfer, w // 4, h // 4)
pts = [
(x0 + c, y0), (x0 + w - c, y0),
(x0 + w, y0 + c), (x0 + w, y0 + h - c),
(x0 + w - c, y0 + h), (x0 + c, y0 + h),
(x0, y0 + h - c), (x0, y0 + c),
]
try:
draw.polygon(pts, fill=fill, outline=outline, width=width)
except TypeError:
draw.polygon(pts, fill=fill, outline=outline)
def render_caption_pil_cyberpunk(
text: str,
font_size: int,
bg_color: tuple = None,
speech_bubble: bool = True,
max_width: int = None,
padding: int = None,
font_path: str = None,
) -> np.ndarray:
"""Render caption in authentic Cyberpunk 2077 style: chamfered box, neon cyan border, scan lines, text glow."""
if not text or not HAS_PIL:
return None
font = get_cyberpunk_font(font_size, preferred_path=font_path)
use_max_width = max_width if max_width is not None else CAPTION_MAX_WIDTH
pad = padding if padding is not None else CAPTION_BG_PADDING
if "\n" in text:
all_lines = []
for phrase in text.split("\n"):
phrase = (phrase or "").strip()
if phrase:
all_lines.extend(_wrap_text(phrase, font, use_max_width))
max_total = 2
lines = all_lines[-max_total:]
else:
lines = _wrap_text(text, font, use_max_width)
if len(lines) > MAX_CAPTION_LINES:
lines = lines[-MAX_CAPTION_LINES:]
line_height = font_size + 4
try:
max_w = max(font.getbbox(line)[2] - font.getbbox(line)[0] for line in lines)
except Exception:
max_w = use_max_width
total_w = max_w + 2 * pad
box_h = line_height * len(lines) + 2 * pad
tail_h = max(6, 10 * font_size // 52) if speech_bubble else 0
total_h = box_h + tail_h
chamfer = max(6, font_size // 8)
fill_color = bg_color if bg_color is not None else CYBERPUNK_BG
img = Image.new("RGBA", (total_w, total_h), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
border_rgb = CYBERPUNK_BORDER
_draw_chamfered_box(draw, 0, 0, total_w, box_h, chamfer, fill_color, outline=(*border_rgb, 255), width=2)
accent_y = chamfer + 2
if accent_y < box_h - chamfer - 2:
draw.line([(chamfer + 4, accent_y), (total_w - chamfer - 4, accent_y)], fill=(255, 0, 255, 180), width=1)
if speech_bubble and tail_h:
cx = total_w // 2
tail_w = 12
draw.polygon(
[(cx - tail_w, box_h - 1), (cx + tail_w, box_h - 1), (cx, total_h - 1)],
fill=fill_color,
outline=None,
)
draw.line([(cx - tail_w, box_h - 1), (cx, total_h - 1)], fill=border_rgb, width=2)
draw.line([(cx + tail_w, box_h - 1), (cx, total_h - 1)], fill=border_rgb, width=2)
for i, line in enumerate(lines):
y = pad + i * line_height
for r in [3, 2, 1]:
alpha = 24 - r * 6
draw.text((pad - r, y), line, font=font, fill=(0, 255, 255, alpha))
draw.text((pad + r, y), line, font=font, fill=(0, 255, 255, alpha))
draw.text((pad, y - r), line, font=font, fill=(0, 255, 255, alpha))
draw.text((pad, y + r), line, font=font, fill=(0, 255, 255, alpha))
draw.text((pad + 1, y + 1), line, font=font, fill=(0, 0, 0, 200))
draw.text((pad, y), line, font=font, fill=CYBERPUNK_TEXT)
arr = np.array(img)
scan_alpha = 4
for row in range(0, total_h, 6):
if row < arr.shape[0]:
arr[row, :, 3] = np.minimum(arr[row, :, 3].astype(np.int32) + scan_alpha, 255)
rng = np.random.default_rng(42)
for _ in range(min(1, total_h // 80)):
row = int(rng.integers(2, max(3, total_h - 2)))
shift = int(rng.choice([-1, 1]))
if 0 <= row < total_h:
arr[row, :, :] = np.roll(arr[row, :, :], shift, axis=0)
return arr
def overlay_caption_on_frame(
frame: np.ndarray,
caption_rgba: np.ndarray,
x: int,
y: int,
alpha_mult: float = 1.0,
) -> np.ndarray:
"""Blend caption onto frame at (x, y). Optimized for speed."""
if caption_rgba is None or caption_rgba.size == 0:
return frame
h, w = caption_rgba.shape[:2]
x = max(0, min(x, frame.shape[1] - w))
y = max(0, min(y, frame.shape[0] - h))
if x < 0 or y < 0 or x + w > frame.shape[1] or y + h > frame.shape[0]:
return frame
roi = frame[y : y + h, x : x + w]
if alpha_mult >= 0.99:
alpha = caption_rgba[:, :, 3:4].astype(np.float32) / 255.0
else:
alpha = (caption_rgba[:, :, 3:4].astype(np.float32) / 255.0) * alpha_mult
rgb = caption_rgba[:, :, :3]
roi[:] = (alpha * rgb + (1.0 - alpha) * roi).astype(np.uint8)
return frame
def enhance_frame(frame: np.ndarray) -> np.ndarray:
"""Combined sharpening + brightness/contrast in one pass for speed."""
blurred = cv2.GaussianBlur(frame, (3, 3), 0)
sharpened = cv2.addWeighted(frame, 1.2, blurred, -0.2, 0)
return cv2.convertScaleAbs(sharpened, alpha=1.03, beta=3)
class FaceKalmanTracker:
def __init__(self):
self.kf = cv2.KalmanFilter(4, 2, 0)
self.kf.transitionMatrix = np.array([
[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]
], dtype=np.float32)
self.kf.measurementMatrix = np.array([
[1, 0, 0, 0], [0, 1, 0, 0]
], dtype=np.float32)
self.kf.processNoiseCov = np.eye(4, dtype=np.float32) * 0.002
self.kf.measurementNoiseCov = np.eye(2, dtype=np.float32) * 0.25
self.kf.errorCovPost = np.eye(4, dtype=np.float32) * 0.25
self.initialized = False
self.frames_lost = 0
def update(self, x=None, y=None):
if x is None or y is None:
if not self.initialized:
return None, None
self.frames_lost += 1
if self.frames_lost > 30:
self.kf.errorCovPost *= 1.05
pred = self.kf.predict()
return float(pred[0, 0]), float(pred[1, 0])
self.frames_lost = 0
measurement = np.array([[np.float32(x)], [np.float32(y)]])
if not self.initialized:
self.kf.statePre = np.array([[x], [y], [0], [0]], dtype=np.float32)
self.kf.statePost = np.array([[x], [y], [0], [0]], dtype=np.float32)
self.initialized = True
return float(x), float(y)
self.kf.predict()
estimated = self.kf.correct(measurement)
if self.kf.errorCovPost[0, 0] > 1.0:
self.kf.errorCovPost = np.eye(4, dtype=np.float32) * 0.5
return float(estimated[0, 0]), float(estimated[1, 0])
class EmotionEstimator:
def __init__(self):
self.smoothed = "neutral"
def update(self, _landmarks=None) -> str:
return self.smoothed
class FrameReader:
def __init__(self, cap):
self.cap = cap
self.lock = threading.Lock()
self.latest = None
self.running = True
self.frames_captured = 0
self.frames_dropped = 0
self._thread = threading.Thread(target=self._run, daemon=True)
def _run(self):
while self.running and self.cap.isOpened():
ret, frame = self.cap.read()
if not ret:
time.sleep(0.001)
continue
with self.lock:
if self.latest is not None:
self.frames_dropped += 1
self.latest = frame
self.frames_captured += 1
def start(self):
self._thread.start()
def read(self):
with self.lock:
if self.latest is not None:
try:
return self.latest.copy()
except Exception:
return None
return None
def stop(self):
self.running = False
self._thread.join(timeout=1.0)
def get_stats(self):
with self.lock:
return self.frames_captured, self.frames_dropped
def get_script_dir():
"""Directory containing this script (for config, OBS state, etc.)."""
return os.path.dirname(os.path.abspath(__file__))
def load_obs_window_state():
"""Load OBS window position/size and caption offsets from obs_window.json. Returns dict or None."""
path = os.path.join(get_script_dir(), "obs_window.json")
if not os.path.exists(path):
return None
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return None
def save_obs_window_state(window_name, caption_offset_x, caption_offset_y, window_size):
"""Save OBS window position (if available), size, and caption offsets to obs_window.json."""
out = {
"caption_offset_x": caption_offset_x,
"caption_offset_y": caption_offset_y,
"window_width": window_size[0],
"window_height": window_size[1],
}
try:
WND_X = getattr(cv2, "WND_PROP_POS_X", -1)
WND_Y = getattr(cv2, "WND_PROP_POS_Y", -1)
if WND_X >= 0 and WND_Y >= 0:
x = int(cv2.getWindowProperty(window_name, WND_X))
y = int(cv2.getWindowProperty(window_name, WND_Y))
if x >= 0 and y >= 0:
out["window_x"] = x
out["window_y"] = y
except Exception:
pass
path = os.path.join(get_script_dir(), "obs_window.json")
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(out, f, indent=2)
except Exception:
pass
def draw_perf_overlay(frame, fps, face_detected, stt_active):
"""Draw a small performance panel (FPS, face, STT) on the frame. Modifies frame in place."""
h, w = frame.shape[:2]
pad = 8
line_h = 22
panel_w = 160
panel_h = pad * 2 + line_h * 3
x0, y0 = pad, pad
overlay = frame[y0 : y0 + panel_h, x0 : x0 + panel_w]
if overlay.size == 0:
return
# Semi-transparent dark background
cv2.rectangle(frame, (x0, y0), (x0 + panel_w, y0 + panel_h), (40, 40, 40), -1)
cv2.rectangle(frame, (x0, y0), (x0 + panel_w, y0 + panel_h), (100, 100, 100), 1)
color_fps = (0, 255, 0) if fps >= 24 else (0, 200, 255) if fps >= 18 else (0, 0, 255)
cv2.putText(frame, "FPS: {}".format(fps), (x0 + pad, y0 + pad + 16),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color_fps, 1, cv2.LINE_AA)
face_txt = "Face: yes" if face_detected else "Face: no"
cv2.putText(frame, face_txt, (x0 + pad, y0 + pad + 16 + line_h),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), 1, cv2.LINE_AA)
stt_txt = "STT: on" if stt_active else "STT: off"
cv2.putText(frame, stt_txt, (x0 + pad, y0 + pad + 16 + line_h * 2),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), 1, cv2.LINE_AA)
def _run_ws_server(port, command_queue):
"""Run WebSocket server in a thread; push received JSON commands into command_queue."""
if not HAS_WEBSOCKETS:
return
async def handler(websocket, path):
try:
async for raw in websocket:
try:
msg = json.loads(raw)
command_queue.put(msg)
except (json.JSONDecodeError, TypeError):
pass
except Exception:
pass
async def serve():
async with websockets.serve(handler, "127.0.0.1", port, ping_interval=None):
await asyncio.Future()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(serve())
except Exception:
pass
finally:
loop.close()
def start_control_api(port=8765, command_queue=None):
"""Start WebSocket control API in a daemon thread. Commands are put into command_queue."""
if not HAS_WEBSOCKETS or command_queue is None:
return None
t = threading.Thread(target=_run_ws_server, args=(port, command_queue), daemon=True)
t.start()
return t
def load_config():
"""Load config.json; create with defaults if missing."""
script_dir = get_script_dir()
config_path = os.path.join(script_dir, "config.json")
defaults = {
"camera_index": 0,
"caption_font_size": 52,
"caption_max_width": 620,
"obs_mode_default_size": "1280x720",
"show_face_box": False,
"speech_bubble_tail": True,
"caption_history_lines": 2,
"chroma_color": "green",
"mic_input_device_index": None,
"caption_style": "minecraft",
"cyberpunk_font": None,
}
if os.path.exists(config_path):
try:
with open(config_path, "r", encoding="utf-8") as f:
return {**defaults, **json.load(f)}
except Exception:
pass
try:
with open(config_path, "w", encoding="utf-8") as f:
json.dump(defaults, f, indent=2)
except Exception:
pass
return defaults
def find_camera():
"""Try indices 0-4 and return the first working camera."""
backend = cv2.CAP_DSHOW if sys.platform == "win32" else cv2.CAP_ANY
for idx in range(5):
cap = cv2.VideoCapture(idx, backend)
if cap.isOpened():
ret, frame = cap.read()
cap.release()
if ret and frame is not None:
return idx
return 0
def show_obs_setup_wizard(window_name):
"""Print OBS setup steps once per install."""
print("\n" + "=" * 60)
print("OBS SETUP")
print("=" * 60)
print("\n1. Open OBS Studio")
print("2. Add source: [+] in Sources -> Window Capture")
print(" Name: Face Captions")
print(" Window: " + window_name)
print("\n3. Remove green: Right-click source -> Filters -> + -> Chroma Key")
print(" Key Color: Green | Similarity: 400-500 | Smoothness: 80-100")
print("\n4. Position the source in your scene, then start speaking.")
print("=" * 60)
def main():
global HAS_CUDA
parser = argparse.ArgumentParser(description="Face-following captions (webcam + STT)")
parser.add_argument(
"--obs-mode",
action="store_true",
help="OBS capture mode: green screen background, always-on-top. Use OBS Window Capture + Chroma Key.",
)
parser.add_argument(
"--window-size",
type=str,
default="1280x720",
help="Window size in OBS mode (e.g. 800x600, 1280x720).",
)
parser.add_argument(
"--chroma-color",
type=str,
default=None,
choices=["green", "blue", "magenta"],
help="Background color for chroma keying in OBS mode.",
)
parser.add_argument(
"--camera-index",
type=int,
default=None,
help="Camera device index (overrides config).",
)
parser.add_argument(
"--caption-offset-x",
type=int,
default=0,
help="Horizontal caption offset in pixels.",
)
parser.add_argument(
"--caption-offset-y",
type=int,
default=0,
help="Vertical caption offset in pixels (negative = up).",
)
parser.add_argument(
"--performance-mode",
choices=["auto", "high", "balanced", "low"],
default="balanced",
help="Performance preset (low = smaller resolution, less detection).",
)
parser.add_argument(
"--enable-api",
action="store_true",
help="Start WebSocket control API on 127.0.0.1 (requires: pip install websockets).",
)
parser.add_argument(
"--api-port",
type=int,
default=8765,
help="Port for WebSocket control API (default 8765).",
)
parser.add_argument(
"--show-perf",
action="store_true",
help="Show performance overlay (FPS / face / STT) on the video frame.",
)
args = parser.parse_args()
obs_mode = args.obs_mode
caption_offset_x = args.caption_offset_x
caption_offset_y = args.caption_offset_y
config = load_config()
chroma_color = args.chroma_color or config.get("chroma_color", "green")
if "x" in args.window_size:
try:
ws, hs = args.window_size.strip().lower().split("x")
OBS_WINDOW_SIZE = (int(ws), int(hs))
except ValueError:
OBS_WINDOW_SIZE = DISPLAY_SIZE
else:
OBS_WINDOW_SIZE = DISPLAY_SIZE
CHROMA_BGR = {"green": (0, 255, 0), "blue": (255, 0, 0), "magenta": (255, 0, 255)}
obs_chroma_bgr = CHROMA_BGR.get(chroma_color, (0, 255, 0))
if args.camera_index is not None:
camera_index = args.camera_index
else:
cfg_cam = config.get("camera_index", 0)
camera_index = find_camera() if cfg_cam == "auto" else int(cfg_cam)
PERFORMANCE_PRESETS = {
"high": {"display_size": (1920, 1080), "detect_interval_tracking": 6, "caption_cache_size": 20, "enhance_frame": True},
"balanced": {"display_size": (1280, 720), "detect_interval_tracking": 6, "caption_cache_size": 20, "enhance_frame": True},
"low": {"display_size": (960, 540), "detect_interval_tracking": 10, "caption_cache_size": 10, "enhance_frame": False},
}
perf_mode = args.performance_mode if args.performance_mode != "auto" else "balanced"
perf = PERFORMANCE_PRESETS.get(perf_mode, PERFORMANCE_PRESETS["balanced"])
disp_w_cfg, disp_h_cfg = perf["display_size"]
detect_interval_tracking = perf["detect_interval_tracking"]
caption_cache_size = perf["caption_cache_size"]
enhance_frame_enabled = perf["enhance_frame"]
if not HAS_PIL:
print("Install Pillow: pip install Pillow")
sys.exit(1)
face_cascade_path = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(face_cascade_path)
if face_cascade.empty():
print("Could not load face cascade.")
sys.exit(1)
backend = cv2.CAP_DSHOW if sys.platform == "win32" else cv2.CAP_ANY
cap = cv2.VideoCapture(camera_index, backend)
if not cap.isOpened():
cap = cv2.VideoCapture(camera_index)
if not cap.isOpened():
print("ERROR: Could not open webcam (index {}).".format(camera_index))
print("\nTroubleshooting:")
print(" 1. Close other apps using the camera (Zoom, Teams, etc.)")
print(" 2. Check camera permissions in System Settings")
print(" 3. Try: python face_captions.py --camera-index 0")
print(" 4. Try: python face_captions.py --camera-index 1")
print("\nAvailable camera indices:")
for i in range(5):
test = cv2.VideoCapture(i, backend)
if test.isOpened():
test.release()
print(" - Camera {}: available".format(i))
else:
print(" - Camera {}: not available".format(i))
sys.exit(1)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
cap.set(cv2.CAP_PROP_FPS, 30)
if sys.platform == "win32":
try:
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
except Exception:
pass
for rw, rh in CAMERA_RESOLUTIONS:
if rw and rh:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, rw)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, rh)
actual_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
actual_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if actual_w >= 640 and actual_h >= 480:
print("Camera: {}x{} @ 30fps (index {})".format(actual_w, actual_h, camera_index))
break
frame_reader = FrameReader(cap)
frame_reader.start()
time.sleep(0.3)
text_queue = queue.Queue()
mic_index = config.get("mic_input_device_index")
if mic_index is not None and not isinstance(mic_index, int):
mic_index = None
stt = StreamingSTT(text_queue, input_device_index=mic_index) if HAS_STT else None
if HAS_STT and stt.start():
mode = get_caption_mode()
engine = getattr(stt, "_engine", "batch")
if mode == "streaming":
print("✓ Caption mode: streaming (real-time) — using", engine)
else:
print("✓ Caption mode: fast batch (install faster-whisper or vosk for streaming)")
latency_info = {
"deepgram": "100-200ms",
"faster-whisper": "200-300ms",
"vosk": "150-250ms",
"batch": "600-800ms",
}
print(" Expected latency:", latency_info.get(engine, "varies"))
else:
print("Speech: install speech_recognition and PyAudio (and optionally vosk for real-time)")
live_partial = ""
last_final = ""
caption_history = [] # list of (text, timestamp)
current_caption = ""
reveal_len = 0
emotion_estimator = EmotionEstimator()
emotion = "neutral"
last_caption_render = None
last_caption_text = ""
frames_since_caption_change = 999
show_face_box = SHOW_FACE_BOX
show_speech_tail = SPEECH_BUBBLE_TAIL
caption_history_lines = CAPTION_HISTORY_LINES
caption_style = config.get("caption_style", "minecraft")
if caption_style not in ("minecraft", "cyberpunk"):
caption_style = "minecraft"
fade_in_frames = FADE_IN_FRAMES
apply_color_filter = False
translate_enabled = False
last_translated = ""
last_final_translated = ""
emotion_hold_prev = "neutral"
emotion_hold_frames = 0
window_name = "Face captions (Q=quit B=box T=tail H=history F=fade C=color M=translate +=/-=size)"
caption_cache = OrderedDict()
MAX_CAPTION_CACHE_SIZE = caption_cache_size
last_caption_scale = 1.0
last_caption_x, last_caption_y, last_caption_w, last_caption_h = None, None, None, None
smooth_caption_x, smooth_caption_y = None, None
fps_history = []
last_fps_time = time.time()
face_center_x, face_top_y = 0.0, 0.0
last_face_bbox = None
frame_count = 0
emotion_smooth_prev = "neutral"
face_tracker = FaceKalmanTracker()
caption_scale = 1.0
debug_mode = False
last_debug_time = time.time()
print("Performance: {} ({}x{})".format(perf_mode, disp_w_cfg, disp_h_cfg))
print("Caption style: {} (change in config.json: minecraft | cyberpunk)".format(caption_style))
print("Caption size: + / - keys (0=100% 1=min 9=max, D=debug)")
face_landmarker_mp = None
if HAS_FACE_MESH and create_face_landmarker:
face_landmarker_mp = create_face_landmarker()
if face_landmarker_mp:
print("Face: MediaPipe Face Mesh (emotion from expression)")
else:
print("Face: MediaPipe model not found; run download_face_landmarker_model.py")
if not face_landmarker_mp:
print("Face: OpenCV Haar cascade")
api_command_queue = queue.Queue() if args.enable_api else None
if args.enable_api:
if HAS_WEBSOCKETS:
start_control_api(port=args.api_port, command_queue=api_command_queue)
print("Control API: ws://127.0.0.1:{} (JSON: action, toggle_speech_bubble, set_caption_offset, etc.)".format(args.api_port))
else:
print("Control API: install websockets (pip install websockets) to enable.")
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
try:
if sys.platform == "win32":
cv2.setWindowProperty(window_name, cv2.WND_PROP_OPENGL, cv2.WINDOW_OPENGL)
except Exception:
pass
if obs_mode:
try:
cv2.setWindowProperty(window_name, cv2.WND_PROP_TOPMOST, 1)
except Exception:
pass
obs_state = load_obs_window_state()
if obs_state:
try:
x, y = obs_state.get("window_x"), obs_state.get("window_y")
if x is not None and y is not None and x >= 0 and y >= 0:
cv2.moveWindow(window_name, int(x), int(y))
ww, wh = obs_state.get("window_width"), obs_state.get("window_height")
if ww and wh:
cv2.resizeWindow(window_name, int(ww), int(wh))
if "caption_offset_x" in obs_state:
caption_offset_x = int(obs_state["caption_offset_x"])
if "caption_offset_y" in obs_state:
caption_offset_y = int(obs_state["caption_offset_y"])
except Exception:
pass
print("OBS mode: {}x{} ({} chroma). In OBS: Window Capture -> Chroma Key.".format(
OBS_WINDOW_SIZE[0], OBS_WINDOW_SIZE[1], chroma_color))
disp_w, disp_h = (disp_w_cfg, disp_h_cfg)
if obs_mode:
if not os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".obs_setup_done")):
show_obs_setup_wizard(window_name)
try:
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".obs_setup_done"), "w") as _:
_.write("1")
except Exception:
pass
while True:
if api_command_queue is not None:
while not api_command_queue.empty():
try:
cmd = api_command_queue.get_nowait()
action = (cmd or {}).get("action")
if action == "toggle_speech_bubble":
show_speech_tail = not show_speech_tail
elif action == "set_caption_offset":
if "x" in cmd:
caption_offset_x = int(cmd["x"])
if "y" in cmd:
caption_offset_y = int(cmd["y"])
elif action == "set_theme" and "theme" in cmd:
pass # placeholder for future theme support
except (queue.Empty, TypeError, ValueError, KeyError):
break
frame = frame_reader.read()
if frame is None:
time.sleep(0.001)
continue
frame_time_start = time.time()
frame = cv2.flip(frame, 1)
h, w = frame.shape[:2]
if w > disp_w or h > disp_h:
if HAS_CUDA:
try:
gpu_frame = cv2.cuda_GpuMat()
gpu_frame.upload(frame)
gpu_frame = cv2.cuda.resize(gpu_frame, (disp_w, disp_h))
frame = gpu_frame.download()
except Exception as e:
if frame_count < 10:
print("GPU resize failed: {}, using CPU".format(e))
HAS_CUDA = False
frame = cv2.resize(frame, (disp_w, disp_h), interpolation=cv2.INTER_NEAREST)
else:
frame = cv2.resize(frame, (disp_w, disp_h), interpolation=cv2.INTER_NEAREST)
h, w = disp_h, disp_w
if face_center_x == 0 and face_top_y == 0:
face_center_x, face_top_y = w / 2, h / 3
frame_count += 1
timestamp_ms = int(frame_count * 1000 / 30)
rgb_frame = None
if last_face_bbox is None:
detect_interval = 1
detect_scale = 0.40
else:
detect_interval = detect_interval_tracking
detect_scale = 0.20
if face_landmarker_mp and (frame_count % detect_interval == 0):
if rgb_frame is None:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
bbox_mp, landmarks_mp, blendshapes_mp = detect_face(face_landmarker_mp, rgb_frame, timestamp_ms)
if bbox_mp is not None:
x_f, y_f, bw, bh = bbox_mp
last_face_bbox = (x_f, y_f, bw, bh)
new_cx = x_f + bw / 2
new_ty = float(y_f)
cx, ty = face_tracker.update(new_cx, new_ty)
if cx is not None:
face_center_x, face_top_y = cx, ty