Skip to content

Commit af52307

Browse files
committed
refactor: migrate camera_code_detection
1 parent dcccd8e commit af52307

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

src/arduino/app_bricks/camera_code_detection/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This Brick enables real-time barcode and QR code scanning from a camera video st
66

77
The Camera Code Detection Brick allows you to:
88

9-
- Capture frames from a USB camera.
10-
- Configure camera settings (resolution and frame rate).
9+
- Capture frames from a Camera (see Camera peripheral for supported cameras).
10+
- Configure Camera settings (resolution and frame rate).
1111
- Define the type of code to detect: barcodes and/or QR codes.
1212
- Process detections with customizable callbacks.
1313

@@ -22,7 +22,7 @@ The Camera Code Detection Brick allows you to:
2222

2323
## Prerequisites
2424

25-
To use this Brick you should have a USB camera connected to your board.
25+
To use this Brick you can choose to plug a camera to your board or use a network-connected camera.
2626

2727
**Tip**: Use a USB-C® Hub with USB-A connectors to support commercial web cameras.
2828

@@ -37,9 +37,25 @@ def render_frame(frame):
3737
def handle_detected_code(frame, detection):
3838
...
3939

40-
# Select the camera you want to use, its resolution and the max fps
41-
detection = CameraCodeDetection(camera=0, resolution=(640, 360), fps=10)
40+
detection = CameraCodeDetection()
4241
detection.on_frame(render_frame)
4342
detection.on_detection(handle_detected_code)
44-
detection.start()
43+
44+
App.run()
4545
```
46+
47+
You can also select a specific camera to use:
48+
49+
```python
50+
from arduino.app_bricks.camera_code_detection import CameraCodeDetection
51+
52+
def handle_detected_code(frame, detection):
53+
...
54+
55+
# Select the camera you want to use, its resolution and the max fps
56+
camera = Camera(camera="rtsp://...", resolution=(640, 360), fps=10)
57+
detection = CameraCodeDetection(camera)
58+
detection.on_detection(handle_detected_code)
59+
60+
App.run()
61+
```

src/arduino/app_bricks/camera_code_detection/detection.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import threading
77
from typing import Callable
88

9-
import cv2
109
from pyzbar.pyzbar import decode, ZBarSymbol, PyZbarError
1110
import numpy as np
12-
from PIL.Image import Image
11+
from PIL.Image import Image, fromarray
1312

1413
from arduino.app_peripherals.camera import Camera
14+
from arduino.app_utils.image import greyscale
1515
from arduino.app_utils import brick, Logger
1616

1717
logger = 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 (Camera): 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
@@ -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(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

src/arduino/app_bricks/camera_code_detection/examples/2_detection_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def on_codes_detected(frame: Image, detections: list[Detection]):
1919
detector = CameraCodeDetection()
2020
detector.on_detect(on_codes_detected)
2121

22-
App.run() # This will block until the app is stopped
22+
App.run()

src/arduino/app_bricks/camera_code_detection/examples/3_detection_with_overrides.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# EXAMPLE_REQUIRES = "Requires an USB webcam connected to the Arduino board."
77
from PIL.Image import Image
88
from arduino.app_utils.app import App
9-
from arduino.app_peripherals.usb_camera import USBCamera
9+
from arduino.app_peripherals.usb_camera import Camera
1010
from arduino.app_bricks.camera_code_detection import CameraCodeDetection, Detection
1111

1212

@@ -17,7 +17,7 @@ def on_code_detected(frame: Image, detection: Detection):
1717
# e.g., draw a bounding box, save it to a database or log it.
1818

1919

20-
camera = USBCamera(camera=0, resolution=(640, 360), fps=10)
20+
camera = Camera(camera=2, resolution=(640, 360), fps=10)
2121
detector = CameraCodeDetection(camera)
2222
detector.on_detect(on_code_detected)
2323

0 commit comments

Comments
 (0)