Remove ring borders before WHOOP digit recognition
Browse filesUses border-connected-component cleanup on the numeric ring crops so the coloured circle cannot hide the second digit in Recovery 93%. Central digits are preserved and the existing confidence gate remains unchanged.
app.py
CHANGED
|
@@ -26,7 +26,7 @@ import numpy as np
|
|
| 26 |
import pandas as pd
|
| 27 |
import torch
|
| 28 |
from datasets import load_dataset
|
| 29 |
-
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
|
| 30 |
from sentence_transformers import SentenceTransformer
|
| 31 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 32 |
|
|
@@ -698,9 +698,24 @@ def read_watch_screenshot(image):
|
|
| 698 |
))
|
| 699 |
ring = prepare(ring)
|
| 700 |
inverted = ImageOps.autocontrast(ImageOps.invert(ring))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
variants = (
|
|
|
|
| 702 |
inverted,
|
| 703 |
-
inverted.point(lambda pixel: 255 if pixel > 128 else 0),
|
| 704 |
)
|
| 705 |
readings = []
|
| 706 |
for variant in variants:
|
|
|
|
| 26 |
import pandas as pd
|
| 27 |
import torch
|
| 28 |
from datasets import load_dataset
|
| 29 |
+
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageOps
|
| 30 |
from sentence_transformers import SentenceTransformer
|
| 31 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 32 |
|
|
|
|
| 698 |
))
|
| 699 |
ring = prepare(ring)
|
| 700 |
inverted = ImageOps.autocontrast(ImageOps.invert(ring))
|
| 701 |
+
binary = inverted.point(lambda pixel: 255 if pixel > 128 else 0)
|
| 702 |
+
|
| 703 |
+
# The coloured recovery ring becomes a dark arc after inversion
|
| 704 |
+
# and can make OCR treat "93" as "9". Remove only black connected
|
| 705 |
+
# components that touch the crop border; the central digits never
|
| 706 |
+
# touch that border, so their shapes remain unchanged.
|
| 707 |
+
border_clean = binary.copy()
|
| 708 |
+
for x in range(border_clean.width):
|
| 709 |
+
for y in (0, border_clean.height - 1):
|
| 710 |
+
if border_clean.getpixel((x, y)) == 0:
|
| 711 |
+
ImageDraw.floodfill(border_clean, (x, y), 255)
|
| 712 |
+
for y in range(border_clean.height):
|
| 713 |
+
for x in (0, border_clean.width - 1):
|
| 714 |
+
if border_clean.getpixel((x, y)) == 0:
|
| 715 |
+
ImageDraw.floodfill(border_clean, (x, y), 255)
|
| 716 |
variants = (
|
| 717 |
+
border_clean,
|
| 718 |
inverted,
|
|
|
|
| 719 |
)
|
| 720 |
readings = []
|
| 721 |
for variant in variants:
|