api.services.label_ocr ====================== .. py:module:: api.services.label_ocr .. autoapi-nested-parse:: Label OCR for the receiving workflow (product-creation spec §6A "Identify and group"): read a label photo with the OCR sidecar (ocr/, RapidOCR) and extract candidate brand, model and serial number. Candidates only: the app shows them next to the photo and a person applies them. Runs in a Celery task (api/tasks.py ``run_label_ocr``) so capture never waits for it. Attributes ---------- .. autoapisummary:: api.services.label_ocr.OCR_TIMEOUT_SECONDS api.services.label_ocr.SERIAL_LABEL api.services.label_ocr.MODEL_LABEL api.services.label_ocr.PART_LABEL api.services.label_ocr.TRUNCATED_SERIAL api.services.label_ocr.VALUE api.services.label_ocr.SERIAL_VALUE api.services.label_ocr.SEPARATOR api.services.label_ocr.SERIAL_KEYWORDS api.services.label_ocr.FUZZY_LABEL_RATIO Functions --------- .. autoapisummary:: api.services.label_ocr.call_ocr api.services.label_ocr.reading_order api.services.label_ocr.extract_fields api.services.label_ocr.serial_matches Module Contents --------------- .. py:data:: OCR_TIMEOUT_SECONDS :value: 60 .. py:data:: SERIAL_LABEL :value: '(?:s\\s*/\\s*n|[s5][e3]r[il1|!][a@4][l1|i][\\s.\\-]*(?:no|n[o°º]|number|nr)?|n[o°º]\\s*(?:de\\s*... .. py:data:: MODEL_LABEL :value: '(?:(?:model(?:(?:e|l)\\b)?|mod\\.|type|typ|device|r[ée]f(?:[ée]rence)?\\.?)(?:\\s*(?:no|nr|n[o°º... .. py:data:: PART_LABEL :value: '(?:cat\\.?\\s*(?:no|nr)\\.?|p\\s*/\\s*n|part\\s*(?:no|number)|art\\.?\\s*-?\\s*(?:no|nr)\\.?)' .. py:data:: TRUNCATED_SERIAL .. py:data:: VALUE :value: '(?!(?:no|nr|n[o°º]|number|num)\\b)([A-Za-z0-9][A-Za-z0-9\\-/.]{2,}(?:\\s[A-Za-z0-9\\-/.]{1,})?)' .. py:data:: SERIAL_VALUE :value: '(?!(?:no|nr|n[o°º]|number|num)\\b)(?=\\S*\\d)([A-Za-z0-9][A-Za-z0-9\\-/.]{2,})' .. py:data:: SEPARATOR :value: '\\s*[:#.\\-]{0,3}\\s*' .. py:function:: call_ocr(image: bytes, content_type: str = 'image/jpeg') -> list[dict[str, Any]] .. py:function:: reading_order(lines: list[dict[str, Any]]) -> list[dict[str, Any]] Top to bottom, then left to right (rows within ~12 px). .. py:data:: SERIAL_KEYWORDS :value: ('serial', 'serialno', 'serialnr', 'serialnumber', 'seriennr', 'seriennummer', 'seriesno',... .. py:data:: FUZZY_LABEL_RATIO :value: 0.8 .. py:function:: extract_fields(lines: list[dict[str, Any]], brands: list[str]) -> dict[str, Any] Brand, model and serial candidates. The serial is searched by its label, then by a label that only looks like one (OCR errors, unusual spelling), then by its shape alone: the guesses carry ``guess: true`` and a lower confidence. .. py:function:: serial_matches(expected: str | None, found: str | None) -> bool Same serial up to separators/case, or one containing the other.