66import threading
77from typing import Callable
88
9- import cv2
109from pyzbar .pyzbar import decode , ZBarSymbol , PyZbarError
1110import numpy as np
12- from PIL .Image import Image
11+ from PIL .Image import Image , fromarray
1312
14- from arduino .app_peripherals .usb_camera import USBCamera
13+ from arduino .app_peripherals .camera import Camera , BaseCamera
14+ from arduino .app_utils .image import greyscale
1515from arduino .app_utils import brick , Logger
1616
1717logger = Logger ("CameraCodeDetection" )
@@ -44,7 +44,7 @@ class CameraCodeDetection:
4444 """Scans a camera video feed for QR codes and/or barcodes.
4545
4646 Args:
47- camera (USBCamera ): The USB camera instance. If None, a default camera will be initialized.
47+ camera (BaseCamera ): The camera instance to use for capturing video . If None, a default camera will be initialized.
4848 detect_qr (bool): Whether to detect QR codes. Defaults to True.
4949 detect_barcode (bool): Whether to detect barcodes. Defaults to True.
5050
@@ -55,18 +55,20 @@ class CameraCodeDetection:
5555
5656 def __init__ (
5757 self ,
58- camera : USBCamera = None ,
58+ camera : BaseCamera = None ,
5959 detect_qr : bool = True ,
6060 detect_barcode : bool = True ,
6161 ):
6262 """Initialize the CameraCodeDetection brick."""
6363 if detect_qr is False and detect_barcode is False :
6464 raise ValueError ("At least one of 'detect_qr' or 'detect_barcode' must be True." )
6565
66+ self ._camera = camera if camera else Camera ()
67+
6668 self ._detect_qr = detect_qr
6769 self ._detect_barcode = detect_barcode
6870
69- # These callbacks do not require locks as long as we're running on CPython
71+ # These callbacks don't require locking as long as we're running on CPython
7072 self ._on_frame_cb = None
7173 self ._on_error_cb = None
7274
@@ -76,8 +78,6 @@ def __init__(
7678
7779 self .already_seen_codes = set ()
7880
79- self ._camera = camera if camera else USBCamera ()
80-
8181 def start (self ):
8282 """Start the detector and begin scanning for codes."""
8383 self ._camera .start ()
@@ -154,13 +154,13 @@ def loop(self):
154154 self ._on_error (e )
155155 return
156156
157- # Use grayscale for barcode/QR code detection
158- gs_frame = cv2 .cvtColor (np .asarray (frame ), cv2 .COLOR_RGB2GRAY )
159-
160- self ._on_frame (frame )
157+ pil_frame = fromarray (frame )
158+ self ._on_frame (pil_frame )
161159
160+ # Use grayscale for barcode/QR code detection
161+ gs_frame = greyscale (frame )
162162 detections = self ._scan_frame (gs_frame )
163- self ._on_detect (frame , detections )
163+ self ._on_detect (pil_frame , detections )
164164
165165 def _on_frame (self , frame : Image ):
166166 if self ._on_frame_cb :
@@ -170,7 +170,7 @@ def _on_frame(self, frame: Image):
170170 logger .error (f"Failed to run on_frame callback: { e } " )
171171 self ._on_error (e )
172172
173- def _scan_frame (self , frame : cv2 . typing . MatLike ) -> list [Detection ]:
173+ def _scan_frame (self , frame : np . ndarray ) -> list [Detection ]:
174174 """Scan the frame for a single barcode or QR code."""
175175 detections = []
176176
0 commit comments