-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognizer_pi.py
More file actions
30 lines (26 loc) · 1.01 KB
/
Copy pathrecognizer_pi.py
File metadata and controls
30 lines (26 loc) · 1.01 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
import os
import cv2
import numpy as np
from PIL import Image, ImageOps
from spellchecker import SpellChecker
from itertools import product
CHAR_LIST = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
EMNIST_CORRECTIONS = {'0': 'o', '8': 'r', '5': 's', '1': 'l', '2': 'z', '6': 'b', '9': 'g'}
confusions = {'b':'d','d':'b','p':'q','q':'p','i':'l','l':'i','1':'l','0':'o'}
try:
spell = SpellChecker()
except ImportError:
spell = None
def apply_emnist_correction(chars_str):
if not chars_str or not any(c.isalpha() for c in chars_str):
return chars_str
return "".join([EMNIST_CORRECTIONS.get(c, c) for c in chars_str])
def dyslexia_correction(word):
if not word:
return ""
if not spell:
return word
variants = [[c, confusions[c]] if c in confusions else [c] for c in word.lower()]
variants = [''.join(p) for p in product(*variants)]
valid = [w for w in variants if spell.correction(w) == w]
return valid[0] if valid else (spell.correction(word) or word)