theDavidGuy's picture
import io, re from typing import List, Dict, Tuple, Any from PIL import Image, ImageDraw try: import pytesseract from pytesseract import Output as TessOutput except Exception: pytesseract = None TessOutput = None PRICE_RE = re.compile(r"(\$\s*\d{1,4}(?:\.\d{2})?|\b\d{1,4}\.\d{2}\b)") def ocr_image(image_bytes: bytes): img = Image.open(io.BytesIO(image_bytes)).convert("RGB") if pytesseract is None: return "", [], img.size data = pytesseract.image_to_data(img, output_type=TessOutput.DICT) tokens = [] for i in range(len(data['text'])): txt = data['text'][i] if not txt: continue try: conf = float(data.get('conf', ['-1'])[i]) except: conf = -1.0 x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i] tokens.append({'text': txt, 'conf': conf, 'box': (x,y,w,h)}) full = " ".join([t['text'] for t in tokens]) return full, tokens, img.size def guess_price(tokens: List[Dict[str,Any]]): best = None for t in tokens: m = PRICE_RE.search(t['text'].replace(",", "")) if m: raw = m.group(0).replace("$","").strip() try: val = float(raw) if 0.5 <= val <= 1000: if best is None or val < best[0]: best = (val, t['box']) except: pass return best def annotate_with_box(image_bytes: bytes, box: Tuple[int,int,int,int], label: str = None) -> bytes: img = Image.open(io.BytesIO(image_bytes)).convert("RGB") draw = ImageDraw.Draw(img) x,y,w,h = box draw.rectangle([x,y,x+w,y+h], outline=(255,0,0), width=3) if label: tw = max(50, len(label)*8) y0 = max(0, y-22) draw.rectangle([x, y0, x+tw, y0+22], fill=(255,0,0)) draw.text((x+4, y0+4), label, fill=(255,255,255)) out = io.BytesIO() img.save(out, format="PNG") return out.getvalue()
83cb218 verified