-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpose_webcam.py
More file actions
1062 lines (912 loc) · 46.5 KB
/
pose_webcam.py
File metadata and controls
1062 lines (912 loc) · 46.5 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
import cv2
import mediapipe as mp
# import pyttsx3 # Removed for performance - using macOS say command instead
import numpy as np
import time
import subprocess
import re
import sys
import argparse
import json
import os
# Configuration
CONFIG_FILE = "config.json"
DEFAULT_CONFIG = {
"auto_start_enabled": False,
"monitor_detection_enabled": False,
"camera_index": 0,
"sitting_duration_threshold": 1800,
"bad_posture_duration_threshold": 60,
"announcement_interval": 5,
"camera_width": 640, # Lower resolution for better performance
"camera_height": 480, # Lower resolution for better performance
"processing_fps": 10 # Process every Nth frame instead of every frame
}
def is_user_active():
"""Check if the user is currently active (not idle)"""
try:
# Use IOHIDSystem to get user idle time on macOS
result = subprocess.run(['ioreg', '-c', 'IOHIDSystem'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
for line in result.stdout.splitlines():
if 'HIDIdleTime' in line:
# Extract the idle time value (in nanoseconds)
import re
match = re.search(r'HIDIdleTime\s*=\s*(\d+)', line)
if match:
idle_time_ns = int(match.group(1))
idle_time_seconds = idle_time_ns / 1_000_000_000 # Convert to seconds
# Consider user inactive if idle for more than 2 minutes (120 seconds)
return idle_time_seconds < 120
return True # Default to True if we can't determine
except Exception as e:
print(f"User activity detection error: {e}")
return True # Default to True on error
def load_config():
"""Load configuration from file"""
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
# Merge with defaults to ensure all keys exist
for key, value in DEFAULT_CONFIG.items():
if key not in config:
config[key] = value
return config
except Exception as e:
print(f"Error loading config: {e}")
return DEFAULT_CONFIG.copy()
else:
return DEFAULT_CONFIG.copy()
# Helper to list camera device names on macOS using ffmpeg
def list_mac_cameras():
try:
result = subprocess.run(
['ffmpeg', '-f', 'avfoundation', '-list_devices', 'true', '-i', '""'],
stderr=subprocess.PIPE, stdout=subprocess.PIPE, text=True
)
devices = []
for line in result.stderr.splitlines():
m = re.search(r'\[(\d+)\] (.+)', line)
if m:
idx, name = m.groups()
devices.append((int(idx), name.strip()))
return devices
except Exception as e:
print(f"Could not list camera devices: {e}")
return []
# Helper to play a ding sound
def play_ding():
"""Play a system ding sound for posture alerts"""
try:
# macOS system sound
os.system('afplay /System/Library/Sounds/Glass.aiff')
except Exception as e:
# Fallback: try different system sounds
try:
os.system('afplay /System/Library/Sounds/Ping.aiff')
except Exception as e2:
print(f"Could not play ding sound: {e}, {e2}")
def play_stand_up_sound():
"""Play Hero sound three times for stand up alerts"""
try:
# Play Hero sound three times with short pauses
for i in range(3):
os.system('afplay /System/Library/Sounds/Hero.aiff')
if i < 2: # Don't sleep after the last one
time.sleep(0.3) # Short pause between sounds
except Exception as e:
# Fallback: try different system sounds
try:
for i in range(3):
os.system('afplay /System/Library/Sounds/Basso.aiff')
if i < 2:
time.sleep(0.3)
except Exception as e2:
try:
for i in range(3):
os.system('afplay /System/Library/Sounds/Sosumi.aiff')
if i < 2:
time.sleep(0.3)
except Exception as e3:
print(f"Could not play stand up sound: {e}, {e2}, {e3}")
# Final fallback to regular ding
play_ding()
def toggle_camera_window(window_should_be_visible, window_created):
"""Toggle camera window visibility"""
try:
# Toggle the visibility flag
window_should_be_visible = not window_should_be_visible
if window_should_be_visible:
# Create window if it doesn't exist
if not window_created:
cv2.namedWindow('Pose Detection', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('Pose Detection', cv2.WND_PROP_TOPMOST, 1)
cv2.resizeWindow('Pose Detection', 800, 600)
# Set window title with more descriptive name (if supported)
try:
cv2.setWindowProperty('Pose Detection', cv2.WND_PROP_TITLE, 'SitStraight - Posture Detection')
except AttributeError:
# WND_PROP_TITLE not available in this OpenCV version, skip it
pass
# Additional macOS-specific dock hiding when window is created
if sys.platform == 'darwin':
try:
import AppKit
# Re-hide dock icon after window creation
app = AppKit.NSApplication.sharedApplication()
app.setActivationPolicy_(AppKit.NSApplicationActivationPolicyAccessory)
except ImportError:
pass
window_created = True
# Show window
cv2.setWindowProperty('Pose Detection', cv2.WND_PROP_VISIBLE, 1)
if sys.platform == 'darwin':
cv2.moveWindow('Pose Detection', 100, 100)
# Additional dock hiding after showing window
try:
import AppKit
app = AppKit.NSApplication.sharedApplication()
app.setActivationPolicy_(AppKit.NSApplicationActivationPolicyAccessory)
except ImportError:
pass
else:
# Hide window
if window_created:
cv2.setWindowProperty('Pose Detection', cv2.WND_PROP_VISIBLE, 0)
if sys.platform == 'darwin':
cv2.moveWindow('Pose Detection', 3000, 3000)
return window_should_be_visible, window_created
except Exception as e:
print(f"DEBUG: Error toggling window: {e}")
return window_should_be_visible, window_created
def update_status_file(status_file, sitting_start_time, sitting_elapsed, window_visible=True):
"""Update status file with current posture detection state"""
try:
status = {
"timestamp": time.time(),
"sitting_start_time": sitting_start_time,
"sitting_elapsed": sitting_elapsed,
"window_visible": window_visible,
"is_sitting": sitting_start_time is not None
}
with open(status_file, 'w') as f:
json.dump(status, f)
except Exception as e:
print(f"Error updating status file: {e}")
def safe_speak(message, voice_busy, last_voice_time, sound_type="posture"):
"""Safely speak a message using efficient macOS say command"""
current_time = time.time()
# If voice is busy or too soon after last announcement, skip this one
if voice_busy or (current_time - last_voice_time) < 2.0:
debug_msg = f"DEBUG: Voice alert skipped - busy: {voice_busy}, time since last: {current_time - last_voice_time:.1f}s, message: '{message}'"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
return voice_busy, last_voice_time
try:
voice_busy = True
debug_msg = f"DEBUG: Playing voice alert - type: {sound_type}, message: '{message}'"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
# Play different sounds based on alert type
if sound_type == "stand_up":
play_stand_up_sound() # Play stand up sound
else:
play_ding() # Play regular ding sound for posture alerts
# Use efficient macOS say command (much faster and more reliable than pyttsx3)
try:
import subprocess
# Use macOS say command with optimized settings
subprocess.run(['say', '-v', 'Alex', '-r', '150', message],
timeout=3, capture_output=True, check=True)
debug_msg = f"DEBUG: Successfully used system 'say' command for: '{message}'"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
except subprocess.TimeoutExpired:
debug_msg = f"DEBUG: System say command timed out for: '{message}'"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
except subprocess.CalledProcessError as e:
debug_msg = f"DEBUG: System say command failed: {e}"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
except Exception as e:
debug_msg = f"DEBUG: System say command error: {e}"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
time.sleep(0.2) # Reduced pause for better performance
last_voice_time = current_time
voice_busy = False
return voice_busy, last_voice_time
except Exception as e:
print(f"Voice alert error: {e}")
voice_busy = False
return voice_busy, last_voice_time
# Calibration system
class PostureCalibrator:
def __init__(self, calibration_file="posture_calibration.json"):
self.calibration_file = calibration_file
self.good_examples = []
self.bad_examples = []
self.personalized_thresholds = {
'head_height_threshold': 0.1,
'nose_height_threshold': 0.7 # Nose should be in upper 30% of frame for good posture
}
self.load_calibration()
def load_calibration(self):
"""Load existing calibration data"""
if os.path.exists(self.calibration_file):
try:
with open(self.calibration_file, 'r') as f:
data = json.load(f)
self.good_examples = data.get('good_examples', [])
self.bad_examples = data.get('bad_examples', [])
loaded_thresholds = data.get('thresholds', {})
# Merge with defaults to ensure all required thresholds exist
for key, value in self.personalized_thresholds.items():
if key not in loaded_thresholds:
loaded_thresholds[key] = value
self.personalized_thresholds = loaded_thresholds
print(f"Loaded calibration data: {len(self.good_examples)} good, {len(self.bad_examples)} bad examples")
except Exception as e:
print(f"Error loading calibration: {e}")
def save_calibration(self):
"""Save calibration data"""
data = {
'good_examples': self.good_examples,
'bad_examples': self.bad_examples,
'thresholds': self.personalized_thresholds
}
try:
with open(self.calibration_file, 'w') as f:
json.dump(data, f, indent=2)
print(f"Calibration saved to {self.calibration_file}")
except Exception as e:
print(f"Error saving calibration: {e}")
def calculate_measurements(self, landmarks, mp_pose):
"""Calculate posture measurements from landmarks"""
left_shoulder = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
right_shoulder = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value]
nose = landmarks[mp_pose.PoseLandmark.NOSE.value]
avg_shoulder_y = (left_shoulder.y + right_shoulder.y) / 2
# Head height: vertical distance from shoulders to nose
head_height_measurement = avg_shoulder_y - nose.y # Positive when head is above shoulders
# Nose height in frame: relative position from top of frame (0 = top, 1 = bottom)
nose_height_in_frame = nose.y # MediaPipe y-coordinate is 0 at top, 1 at bottom
return {
'head_height': head_height_measurement,
'nose_height_in_frame': nose_height_in_frame
}
def add_example(self, landmarks, is_good, mp_pose):
"""Add a posture example"""
measurements = self.calculate_measurements(landmarks, mp_pose)
example = {
'measurements': measurements,
'timestamp': time.time()
}
if is_good:
self.good_examples.append(example)
print(f"Added good example (head_height: {measurements['head_height']:.3f}, nose_height: {measurements['nose_height_in_frame']:.3f})")
else:
self.bad_examples.append(example)
print(f"Added bad example (head_height: {measurements['head_height']:.3f}, nose_height: {measurements['nose_height_in_frame']:.3f})")
def calculate_personalized_thresholds(self):
"""Calculate personalized thresholds from collected examples"""
if len(self.good_examples) < 3 or len(self.bad_examples) < 3:
print("Need at least 3 good and 3 bad examples for calibration")
return False
# Calculate statistics for good examples
good_head_height = [ex['measurements']['head_height'] for ex in self.good_examples]
good_nose_height = [ex['measurements']['nose_height_in_frame'] for ex in self.good_examples]
# Calculate statistics for bad examples
bad_head_height = [ex['measurements']['head_height'] for ex in self.bad_examples]
bad_nose_height = [ex['measurements']['nose_height_in_frame'] for ex in self.bad_examples]
# Set thresholds as midpoint between good and bad distributions
self.personalized_thresholds['head_height_threshold'] = (
np.mean(good_head_height) + np.mean(bad_head_height)
) / 2
self.personalized_thresholds['nose_height_threshold'] = (
np.mean(good_nose_height) + np.mean(bad_nose_height)
) / 2
print(f"Personalized thresholds calculated:")
print(f" Head height: {self.personalized_thresholds['head_height_threshold']:.3f}")
print(f" Nose height: {self.personalized_thresholds['nose_height_threshold']:.3f}")
return True
def is_bad_pose(self, landmarks, mp_pose):
"""Check if pose is bad using personalized thresholds"""
measurements = self.calculate_measurements(landmarks, mp_pose)
head_height_bad = measurements['head_height'] < self.personalized_thresholds['head_height_threshold']
nose_height_bad = measurements['nose_height_in_frame'] > self.personalized_thresholds['nose_height_threshold']
return head_height_bad or nose_height_bad
def run_calibration_mode(cam_index):
"""Run the calibration mode to collect posture examples"""
print("=== POSTURE CALIBRATION MODE ===")
print("Instructions:")
print("1. Press 'g' to add a GOOD posture example")
print("2. Press 'b' to add a BAD posture example")
print("3. Press 'c' to calculate personalized thresholds")
print("4. Press 's' to save calibration")
print("5. Press 'q' to quit")
print()
# List available cameras on macOS
print("Available cameras:")
cameras = list_mac_cameras()
if cameras:
for idx, name in cameras:
print(f" [{idx}] {name}")
else:
print(" Could not detect cameras automatically")
print()
print(f"Using camera index {cam_index}")
# Initialize MediaPipe pose and drawing utilities
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
# Initialize calibrator
calibrator = PostureCalibrator()
# Debug: Print loaded thresholds
print(f"DEBUG: Loaded thresholds - Head height: {calibrator.personalized_thresholds['head_height_threshold']:.3f}, Nose height: {calibrator.personalized_thresholds['nose_height_threshold']:.3f}")
# Open selected webcam
print(f"Attempting to open camera at index {cam_index}...")
cap = cv2.VideoCapture(cam_index)
# Verify camera opened successfully
if not cap.isOpened():
print(f"Failed to open camera at index {cam_index}")
print("Trying to open camera at index 0...")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Failed to open any camera. Exiting.")
return
# Verify the camera is actually working by reading a test frame
ret, test_frame = cap.read()
if not ret or test_frame is None:
print(f"Camera at index {cam_index} opened but cannot read frames")
print("Trying to open camera at index 0...")
cap.release()
cap = cv2.VideoCapture(0)
ret, test_frame = cap.read()
if not ret or test_frame is None:
print("Failed to open any working camera. Exiting.")
return
# Get camera info to verify
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"Camera opened successfully: {width}x{height}")
# Set lower resolution for better performance
config = load_config()
target_width = config.get('camera_width', 640)
target_height = config.get('camera_height', 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, target_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, target_height)
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Flip the frame horizontally (mirror effect)
frame = cv2.flip(frame, 1)
# Convert the BGR image to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make pose detection
results = pose.process(image)
# Draw pose landmarks on the image
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.pose_landmarks:
# Draw pose landmarks and connections
mp_drawing.draw_landmarks(
image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Annotate each skeleton line with body part names
landmark_points = results.pose_landmarks.landmark
h, w = image.shape[:2]
for connection in mp_pose.POSE_CONNECTIONS:
start_idx, end_idx = connection
start_lm = landmark_points[start_idx]
end_lm = landmark_points[end_idx]
# Get pixel coordinates
start_xy = (int(start_lm.x * w), int(start_lm.y * h))
end_xy = (int(end_lm.x * w), int(end_lm.y * h))
# Get landmark names
start_name = mp_pose.PoseLandmark(start_idx).name.replace("_", " ").title()
end_name = mp_pose.PoseLandmark(end_idx).name.replace("_", " ").title()
# Draw text near each endpoint
cv2.putText(image, start_name, (start_xy[0]+5, start_xy[1]-5),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255), 1, cv2.LINE_AA)
cv2.putText(image, end_name, (end_xy[0]+5, end_xy[1]-5),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255), 1, cv2.LINE_AA)
# Show current measurements
measurements = calibrator.calculate_measurements(results.pose_landmarks.landmark, mp_pose)
cv2.putText(image, f"Head Height: {measurements['head_height']:.3f}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.putText(image, f"Nose Height: {measurements['nose_height_in_frame']:.3f}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Show current thresholds (always from calibrator.personalized_thresholds)
cv2.putText(
image,
f"Thresholds: H={calibrator.personalized_thresholds['head_height_threshold']:.3f}, N={calibrator.personalized_thresholds['nose_height_threshold']:.3f}",
(10, 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
# Show example counts
cv2.putText(image, f"Examples - Good: {len(calibrator.good_examples)}, Bad: {len(calibrator.bad_examples)}",
(10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1)
# Show the image (landscape)
cv2.imshow('Posture Calibration', image)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('g') and results.pose_landmarks:
calibrator.add_example(results.pose_landmarks.landmark, True, mp_pose)
elif key == ord('b') and results.pose_landmarks:
calibrator.add_example(results.pose_landmarks.landmark, False, mp_pose)
elif key == ord('c'):
if calibrator.calculate_personalized_thresholds():
print("Personalized thresholds calculated successfully!")
else:
print("Not enough examples for calibration")
elif key == ord('s'):
calibrator.save_calibration()
cap.release()
cv2.destroyAllWindows()
def draw_sitting_timer(image, elapsed_time, alerted):
"""Draw a visual timelapse-style sitting timer on the image"""
# Convert elapsed time to hours, minutes, seconds
hours = int(elapsed_time // 3600)
minutes = int((elapsed_time % 3600) // 60)
seconds = int(elapsed_time % 60)
# Format time string
if hours > 0:
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
else:
time_str = f"{minutes:02d}:{seconds:02d}"
# Get image dimensions
h, w = image.shape[:2]
# Timer position (top-right corner)
timer_x = w - 200
timer_y = 50
# Background rectangle for timer
bg_color = (0, 0, 0) if not alerted else (0, 0, 255) # Red background if alerted
cv2.rectangle(image, (timer_x - 10, timer_y - 30), (timer_x + 190, timer_y + 10), bg_color, -1)
cv2.rectangle(image, (timer_x - 10, timer_y - 30), (timer_x + 190, timer_y + 10), (255, 255, 255), 2)
# Timer text
text_color = (255, 255, 255) if not alerted else (255, 255, 255)
if elapsed_time == 0:
cv2.putText(image, "Sitting: PAUSED", (timer_x, timer_y),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (128, 128, 128), 2)
else:
cv2.putText(image, f"Sitting: {time_str}", (timer_x, timer_y),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, text_color, 2)
# Progress bar (timelapse style)
bar_width = 180
bar_height = 8
bar_x = timer_x
bar_y = timer_y + 20
# Calculate progress (20 minutes = 100%)
progress = min(elapsed_time / (20 * 60), 1.0) # 20 minutes max
filled_width = int(bar_width * progress)
# Draw progress bar background
cv2.rectangle(image, (bar_x, bar_y), (bar_x + bar_width, bar_y + bar_height), (100, 100, 100), -1)
# Draw filled progress
if progress > 0:
# Color gradient: green -> yellow -> red
if progress < 0.5:
# Green to yellow
color_ratio = progress / 0.5
color = (0, int(255 * (1 - color_ratio)), int(255 * color_ratio))
else:
# Yellow to red
color_ratio = (progress - 0.5) / 0.5
color = (0, int(255 * (1 - color_ratio)), 255)
cv2.rectangle(image, (bar_x, bar_y), (bar_x + filled_width, bar_y + bar_height), color, -1)
# Progress bar border
cv2.rectangle(image, (bar_x, bar_y), (bar_x + bar_width, bar_y + bar_height), (255, 255, 255), 1)
# Add percentage text
percentage = int(progress * 100)
cv2.putText(image, f"{percentage}%", (bar_x + bar_width + 10, bar_y + bar_height + 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Add warning text if sitting too long
if alerted:
cv2.putText(image, "TAKE A BREAK!", (timer_x, timer_y + 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
def draw_posture_metrics(image, measurements, thresholds, bad_reasons=None):
"""Draw posture metrics on the image"""
# Get image dimensions
h, w = image.shape[:2]
# Metrics position (bottom-left corner, above quit instruction)
metrics_x = 10
metrics_y = h - 120 # Moved back up since we removed one metric
# Calculate text size to determine background rectangle size
font_scale = 0.4
font_thickness = 1
line_height = 18
# Draw metrics without background rectangle for less visual interference
y_offset = 0
# Head height measurement
head_height_status = "❌" if measurements['head_height'] < thresholds['head_height_threshold'] else "✅"
head_height_color = (0, 0, 255) if measurements['head_height'] < thresholds['head_height_threshold'] else (0, 255, 0)
cv2.putText(image, f"{head_height_status} Height: {measurements['head_height']:.3f} (>{thresholds['head_height_threshold']:.3f})",
(metrics_x, metrics_y + y_offset), cv2.FONT_HERSHEY_SIMPLEX, font_scale, head_height_color, font_thickness)
y_offset += line_height
# Nose height in frame measurement (now used for alerts)
nose_height_status = "❌" if measurements['nose_height_in_frame'] > thresholds['nose_height_threshold'] else "✅"
nose_height_color = (0, 0, 255) if measurements['nose_height_in_frame'] > thresholds['nose_height_threshold'] else (0, 255, 0)
cv2.putText(image, f"{nose_height_status} Nose Y: {measurements['nose_height_in_frame']:.3f} (<{thresholds['nose_height_threshold']:.3f})",
(metrics_x, metrics_y + y_offset), cv2.FONT_HERSHEY_SIMPLEX, font_scale, nose_height_color, font_thickness)
y_offset += line_height
# Show thresholds below the metrics
cv2.putText(
image,
f"Thresholds: H={thresholds['head_height_threshold']:.3f}, N={thresholds['nose_height_threshold']:.3f}",
(metrics_x, metrics_y + y_offset),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1)
# Draw bad posture warning if any (smaller and more subtle)
if bad_reasons:
y_offset += line_height + 5
cv2.putText(image, f"⚠️ BAD POSTURE", (metrics_x, metrics_y + y_offset),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
def run_normal_mode(cam_index):
"""Run the normal pose detection mode with personalized thresholds"""
# Load configuration
config = load_config()
# List available cameras on macOS
print("Available cameras:")
cameras = list_mac_cameras()
if cameras:
for idx, name in cameras:
print(f" [{idx}] {name}")
else:
print(" Could not detect cameras automatically")
print()
print(f"Using camera index {cam_index}")
print(f"Configuration loaded:")
print(f" Monitor detection: {'Enabled' if config['monitor_detection_enabled'] else 'Disabled'}")
print(f" Sitting threshold: {config['sitting_duration_threshold']} seconds")
print(f" Bad posture threshold: {config['bad_posture_duration_threshold']} seconds")
print(f" Announcement interval: {config['announcement_interval']} seconds")
# Initialize MediaPipe pose and drawing utilities
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
# Voice engine is no longer needed - using efficient macOS say command
# engine = pyttsx3.init() # Removed for performance
# engine.setProperty('volume', 1.0) # Removed for performance
# engine.setProperty('rate', 150) # Removed for performance
# Voice queue to prevent conflicts
voice_busy = False
last_voice_time = 0
# Initialize calibrator for personalized thresholds
calibrator = PostureCalibrator()
# Debug: Print loaded thresholds
print(f"DEBUG: Loaded thresholds - Head height: {calibrator.personalized_thresholds['head_height_threshold']:.3f}, Nose height: {calibrator.personalized_thresholds['nose_height_threshold']:.3f}")
# Open selected webcam
print(f"Attempting to open camera at index {cam_index}...")
cap = cv2.VideoCapture(cam_index)
# Verify camera opened successfully
if not cap.isOpened():
print(f"Failed to open camera at index {cam_index}")
print("Trying to open camera at index 0...")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Failed to open any camera. Exiting.")
return
# Get camera info to verify
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"Camera opened successfully: {width}x{height}")
# Set lower resolution for better performance
target_width = config.get('camera_width', 640)
target_height = config.get('camera_height', 480)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, target_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, target_height)
# Get actual frame rate
fps = cap.get(cv2.CAP_PROP_FPS)
if fps <= 0:
# If FPS is not available, estimate it
print("FPS not available from camera, estimating...")
start_time = time.time()
for _ in range(30): # Sample 30 frames
ret, _ = cap.read()
if not ret:
break
end_time = time.time()
fps = 30 / (end_time - start_time) if (end_time - start_time) > 0 else 30
print(f"Camera frame rate: {fps:.1f} FPS")
# Alert logic
bad_posture_frames = 0
bad_posture_start_time = None
bad_posture_alerted = False
BAD_POSTURE_DURATION_THRESHOLD = config['bad_posture_duration_threshold'] # seconds
last_announcement_time = 0
ANNOUNCEMENT_INTERVAL = config['announcement_interval'] # Announce every N seconds when bad posture continues
# Sitting timer logic
sitting_start_time = None # Will be set when pose is first detected
sitting_alerted = False
SITTING_DURATION_THRESHOLD = config['sitting_duration_threshold'] # seconds
total_sitting_time = 0 # Track total actual sitting time
last_pose_time = None # Track when pose was last detected
pose_detection_threshold = 3 # Seconds without pose detection to consider "not sitting"
# Status file for communication with menu bar
status_file = "posture_status.json"
# Window visibility control
window_should_be_visible = False
window_created = False
# Frame counter for periodic tasks
frame_count = 0
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
# Don't create the window initially - create it only when needed
# Additional macOS-specific dock hiding for OpenCV window
if sys.platform == 'darwin':
try:
import AppKit
# Hide dock icon for the OpenCV window
AppKit.NSApplication.sharedApplication()
AppKit.NSApp.setActivationPolicy_(AppKit.NSApplicationActivationPolicyAccessory)
except ImportError:
# AppKit not available, use alternative method
pass
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# Check user activity if monitor detection is enabled
if config['monitor_detection_enabled']:
user_active = is_user_active()
if not user_active:
# User is idle - pause detection
time.sleep(1) # Sleep for 1 second before checking again
continue # Skip this frame and continue loop
# Flip the frame horizontally (mirror effect)
frame = cv2.flip(frame, 1)
# Convert the BGR image to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make pose detection
results = pose.process(image)
# Update sitting timer based on pose detection
current_time = time.time()
if results.pose_landmarks:
# Pose detected - update last pose time
last_pose_time = current_time
# If this is the first pose detection after being away, resume timer
if sitting_start_time is None:
sitting_start_time = current_time
debug_msg = f"DEBUG: Sitting timer started at {time.strftime('%H:%M:%S')}"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
else:
# No pose detected - check if we should pause timer
if last_pose_time and (current_time - last_pose_time) > pose_detection_threshold:
# Been away for more than threshold - pause timer
if sitting_start_time is not None:
sitting_start_time = None # Pause timer
# Draw pose landmarks on the image
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.pose_landmarks:
# Calculate measurements for posture detection
measurements = calibrator.calculate_measurements(results.pose_landmarks.landmark, mp_pose)
# Draw only the key points used for posture calculation
landmark_points = results.pose_landmarks.landmark
h, w = image.shape[:2]
# Draw key landmarks: shoulders, nose
key_landmarks = [
mp_pose.PoseLandmark.LEFT_SHOULDER,
mp_pose.PoseLandmark.RIGHT_SHOULDER,
mp_pose.PoseLandmark.NOSE
]
for landmark in key_landmarks:
lm = landmark_points[landmark.value]
x, y = int(lm.x * w), int(lm.y * h)
# Draw circle for each key point
cv2.circle(image, (x, y), 8, (0, 255, 0), -1) # Green circles
# Draw landmark name
name = landmark.name.replace("_", " ").title()
cv2.putText(image, name, (x+10, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
# Draw line connecting shoulders for visual reference
left_shoulder = landmark_points[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
right_shoulder = landmark_points[mp_pose.PoseLandmark.RIGHT_SHOULDER.value]
# Shoulder line
cv2.line(image,
(int(left_shoulder.x * w), int(left_shoulder.y * h)),
(int(right_shoulder.x * w), int(right_shoulder.y * h)),
(255, 255, 0), 2) # Yellow line
# Check for bad posture using personalized thresholds
measurements = calibrator.calculate_measurements(results.pose_landmarks.landmark, mp_pose)
bad_reasons = []
if measurements['head_height'] < calibrator.personalized_thresholds['head_height_threshold']:
bad_reasons.append("head height")
if measurements['nose_height_in_frame'] > calibrator.personalized_thresholds['nose_height_threshold']:
bad_reasons.append("nose too low")
if bad_reasons:
if bad_posture_start_time is None:
bad_posture_start_time = time.time()
bad_posture_alerted = False
last_announcement_time = 0
debug_msg = f"DEBUG: Bad posture detected: {bad_reasons}"
print(debug_msg)
# Also write to log file
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
bad_posture_frames += 1
cv2.putText(image, f"Bad posture: {', '.join(bad_reasons)}", (30, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
current_time = time.time()
elapsed = current_time - bad_posture_start_time
# Initial alert after bad posture duration threshold
if elapsed >= BAD_POSTURE_DURATION_THRESHOLD and last_announcement_time == 0:
debug_msg = f"DEBUG: Bad posture alert triggered after {elapsed:.1f} seconds (threshold: {BAD_POSTURE_DURATION_THRESHOLD})"
print(debug_msg)
# Also write to log file
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
voice_busy, last_voice_time = safe_speak("Please sit up straight!", voice_busy, last_voice_time)
last_announcement_time = current_time
# Continuous announcements every ANNOUNCEMENT_INTERVAL seconds (only after initial alert)
elif last_announcement_time > 0 and (current_time - last_announcement_time) >= ANNOUNCEMENT_INTERVAL:
debug_msg = f"DEBUG: Continuous alert triggered after {current_time - last_announcement_time:.1f} seconds"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
voice_busy, last_voice_time = safe_speak("Please sit up straight!", voice_busy, last_voice_time)
last_announcement_time = current_time
# Note: "stand up" announcement moved to sitting timer section
else:
bad_posture_frames = 0
bad_posture_start_time = None
bad_posture_alerted = False
else:
# No pose detected - reset all bad posture tracking
bad_posture_frames = 0
bad_posture_start_time = None
bad_posture_alerted = False
last_announcement_time = 0 # Reset announcement timer as well
# Only reset sitting alert if person has been away for a long time (more than 30 seconds)
# This prevents resetting the alert due to brief pose detection failures
if last_pose_time and (current_time - last_pose_time) > 30:
sitting_alerted = False
debug_msg = f"DEBUG: Sitting alert reset after being away for {current_time - last_pose_time:.1f} seconds"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
# Check sitting timer (only when actually sitting and pose is detected)
if sitting_start_time is not None:
sitting_elapsed = time.time() - sitting_start_time
# Only trigger stand up alert if pose is currently detected (person is actually sitting)
if (sitting_elapsed >= SITTING_DURATION_THRESHOLD and
not sitting_alerted and
results.pose_landmarks is not None):
debug_msg = f"DEBUG: Stand up alert triggered after {sitting_elapsed:.1f} seconds (threshold: {SITTING_DURATION_THRESHOLD})"
print(debug_msg)
try:
with open('simple_posture.log', 'a') as f:
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {debug_msg}\n")
except:
pass
voice_busy, last_voice_time = safe_speak("stand up", voice_busy, last_voice_time, "stand_up")
sitting_alerted = True
else:
sitting_elapsed = 0 # Timer is paused
# Check for window toggle request from menu bar
try:
if os.path.exists("toggle_window.txt"):
window_should_be_visible, window_created = toggle_camera_window(window_should_be_visible, window_created)
os.remove("toggle_window.txt")
except:
pass
# Update status file for menu bar communication
try:
update_status_file(status_file, sitting_start_time, sitting_elapsed, window_should_be_visible)
except Exception as e:
print(f"DEBUG: Error updating status: {e}")
# Periodic dock icon hiding check (every 30 frames, roughly every second)
if frame_count % 30 == 0 and sys.platform == 'darwin':
try:
import AppKit
app = AppKit.NSApplication.sharedApplication()
app.setActivationPolicy_(AppKit.NSApplicationActivationPolicyAccessory)
except ImportError:
pass
# Draw posture metrics if pose is detected
if results.pose_landmarks:
draw_posture_metrics(image, measurements, calibrator.personalized_thresholds, bad_reasons if 'bad_reasons' in locals() else None)
# Draw sitting timer display
draw_sitting_timer(image, sitting_elapsed, sitting_alerted)
# Add keyboard controls info
h, w = image.shape[:2]
cv2.putText(image, "Press 'q' to quit, 'h' to hide/show", (10, h - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# Show the image (landscape) - only if window should be visible
if window_should_be_visible and window_created:
cv2.imshow('Pose Detection', image)
else: