wip: Chieri V4 B30 ocr

This commit is contained in:
2023-08-09 18:25:01 +08:00
parent 74060b1412
commit 920ceffb35
8 changed files with 452 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import cv2
import numpy as np
from imutils import grab_contours
from imutils import resize as imutils_resize
from numpy.linalg import norm
from pytesseract import image_to_string
from .template import (
@ -122,6 +123,32 @@ def filter_digit_results(
return final_result
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n * ang / (2 * np.pi))
bin_cells = bin[:10, :10], bin[10:, :10], bin[:10, 10:], bin[10:, 10:]
mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
hists = [
np.bincount(b.ravel(), m.ravel(), bin_n)
for b, m in zip(bin_cells, mag_cells)
]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
def ocr_digits(
img: Mat,
templates: TemplateItem,