benjac8 commited on
Commit
8cea3df
·
verified ·
1 Parent(s): 252a3bf

Read WHOOP dashboard rings with spatial OCR

Browse files

Adds a conservative second OCR pass over the three fixed WHOOP ring positions. The fallback is accepted only when Sleep score, Recovery score and Strain all form a valid row, preventing percentage scores from entering fields with incompatible units.

Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -682,11 +682,59 @@ def read_watch_screenshot(image):
682
  )))
683
  top_inverted = ImageOps.invert(top_panel)
684
  top_thresholded = top_panel.point(lambda pixel: 255 if pixel > 145 else 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685
  extracted = "\n".join([
686
  pytesseract.image_to_string(enlarged, config="--psm 11"),
687
  pytesseract.image_to_string(top_panel, config="--psm 6"),
688
  pytesseract.image_to_string(top_inverted, config="--psm 11"),
689
  pytesseract.image_to_string(top_thresholded, config="--psm 11"),
 
690
  ])
691
  parsed = parse_watch_ocr_text(extracted)
692
  found = [label for label, key in (("Sleep", "sleep"), ("Strain", "strain"), ("HRV", "hrv"))
 
682
  )))
683
  top_inverted = ImageOps.invert(top_panel)
684
  top_thresholded = top_panel.point(lambda pixel: 255 if pixel > 145 else 0)
685
+
686
+ # WHOOP's home screen places the three dashboard values inside large
687
+ # coloured rings. Full-page OCR can miss those isolated white digits
688
+ # even when it reads the labels below them. Read each ring from its
689
+ # stable relative position as a second, tightly-scoped OCR pass. We
690
+ # only trust the layout when all three values form a valid WHOOP row,
691
+ # which keeps this fallback conservative for non-WHOOP screenshots.
692
+ def read_ring_value(x_start, x_end, low, high):
693
+ ring = grayscale.crop((
694
+ int(grayscale.width * x_start),
695
+ int(grayscale.height * 0.20),
696
+ max(int(grayscale.width * x_end), 1),
697
+ max(int(grayscale.height * 0.29), 1),
698
+ ))
699
+ ring = prepare(ring)
700
+ variants = (
701
+ ImageOps.autocontrast(ImageOps.invert(ring)),
702
+ ImageOps.invert(ring).point(lambda pixel: 255 if pixel > 150 else 0),
703
+ )
704
+ readings = []
705
+ for variant in variants:
706
+ readings.append(pytesseract.image_to_string(
707
+ variant,
708
+ config="--psm 7 -c tessedit_char_whitelist=0123456789.%",
709
+ ))
710
+ candidates = []
711
+ for token in re.findall(r"\d{1,3}(?:[.,]\d+)?", " ".join(readings)):
712
+ value = float(token.replace(",", "."))
713
+ if low <= value <= high:
714
+ candidates.append(value)
715
+ if not candidates:
716
+ return None
717
+ # Prefer a decimal for Strain when available; WHOOP Strain is
718
+ # commonly shown to one decimal place (for example 14.7).
719
+ decimal_values = [value for value in candidates if value % 1]
720
+ return decimal_values[0] if decimal_values else candidates[0]
721
+
722
+ ring_sleep = read_ring_value(0.03, 0.31, 0, 100)
723
+ ring_recovery = read_ring_value(0.35, 0.65, 0, 100)
724
+ ring_strain = read_ring_value(0.68, 0.97, 0, 21)
725
+ ring_hint = ""
726
+ if all(value is not None for value in (ring_sleep, ring_recovery, ring_strain)):
727
+ ring_hint = (
728
+ f"SLEEP {ring_sleep:g}% RECOVERY {ring_recovery:g}% "
729
+ f"STRAIN {ring_strain:g}"
730
+ )
731
+
732
  extracted = "\n".join([
733
  pytesseract.image_to_string(enlarged, config="--psm 11"),
734
  pytesseract.image_to_string(top_panel, config="--psm 6"),
735
  pytesseract.image_to_string(top_inverted, config="--psm 11"),
736
  pytesseract.image_to_string(top_thresholded, config="--psm 11"),
737
+ ring_hint,
738
  ])
739
  parsed = parse_watch_ocr_text(extracted)
740
  found = [label for label, key in (("Sleep", "sleep"), ("Strain", "strain"), ("HRV", "hrv"))