Add services layer, tests, streaming UI, and cleanup legacy code

This commit is contained in:
Viktor Barzin 2026-02-06 20:55:10 +00:00
parent 5514fa6381
commit d205d15c74
62 changed files with 3729 additions and 1024 deletions

View file

@ -1,10 +1,12 @@
import re
from pathlib import Path
from typing import Any
from PIL import Image
import cv2
import numpy as np
def inference(image_path):
def inference(image_path: str | Path) -> tuple[str, Any]:
from transformers import Pix2StructProcessor, Pix2StructForConditionalGeneration
image = Image.open(image_path)
@ -19,7 +21,7 @@ def inference(image_path):
return output, predictions
def extract_total_sqm(input_str: str):
def extract_total_sqm(input_str: str) -> float | None:
sqmregex = r"(\d+\.?\d*) ?(sq ?m|sq. ?m)"
matches = re.findall(sqmregex, input_str.lower())
sqms = [float(m[0]) for m in matches]
@ -29,13 +31,13 @@ def extract_total_sqm(input_str: str):
return max(filtered)
def calculate_model(image_path):
def calculate_model(image_path: str | Path) -> tuple[float | None, str, Any]:
output, predictions_tensor = inference(image_path)
estimated_sqm = extract_total_sqm(output)
return estimated_sqm, output, predictions_tensor
def improve_img_for_ocr(img: Image):
def improve_img_for_ocr(img: Image.Image) -> Image.Image:
img2 = np.array(img.convert("L"))
cv2.resize(img2, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
thresh = cv2.adaptiveThreshold(
@ -44,7 +46,7 @@ def improve_img_for_ocr(img: Image):
return Image.fromarray(thresh)
def calculate_ocr(image_path) -> tuple[float | None, str]:
def calculate_ocr(image_path: str | Path) -> tuple[float | None, str]:
import pytesseract
img = Image.open(image_path)