File size: 2,211 Bytes
7e25f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
QR code detection + decoding provider.

Uses OpenCV's built-in cv2.QRCodeDetector — no external deps.
Detects and decodes QR codes, returning the decoded text + bounding box.

Pure OpenCV — no model downloads, no external services.
"""

from __future__ import annotations

import cv2
import numpy as np

from config.settings import Settings, settings as _default_settings
from cores.vision import to_gray
from pipeline.feature_extraction import PipelineOutput
from providers.base import BaseProvider, ProviderCapability


class QRCodeProvider(BaseProvider):
    name = "qr_code"
    capability = ProviderCapability.OBJECT_DETECTION  # reuse OBJECT_DETECTION capability

    def __init__(self, settings: Settings | None = None) -> None:
        super().__init__(settings=settings or _default_settings)
        self._detector = cv2.QRCodeDetector()

    def is_available(self) -> bool:
        return hasattr(cv2, "QRCodeDetector")

    def _run(self, pipeline_output: PipelineOutput) -> tuple[dict, dict]:
        img: np.ndarray = pipeline_output.image

        # QRCodeDetector.detectAndDecode returns:
        # (decoded_text, points, straight_qrcode)
        try:
            data, points, _ = self._detector.detectAndDecode(img)
        except Exception as e:
            return {"error": str(e)}, {"objects": [], "model": "opencv_qr"}

        objects: list[dict] = []
        if points is not None and len(points) > 0:
            # points shape: (4, 2) — four corners of the QR code
            pts = points.reshape(-1, 2)
            x1, y1 = int(pts[:, 0].min()), int(pts[:, 1].min())
            x2, y2 = int(pts[:, 0].max()), int(pts[:, 1].max())
            objects.append({
                "label": "qr_code",
                "confidence": 1.0,
                "box": {"x": x1, "y": y1, "w": x2 - x1, "h": y2 - y1},
                "decoded_text": data if data else "",
                "is_qr_code": True,
            })

        raw = {
            "num_objects": len(objects),
            "model": "opencv_qr",
            "decoded": bool(data),
        }
        normalized = {
            "objects": objects,
            "model": "opencv_qr",
        }
        return raw, normalized