End result
1
# The images will be processed using OpenCV and Numpy
2
import cv2
3
import numpy
4
5
# The decode_qrcode function will be used for decoding the found QR Code
6
from pyzbar.pyzbar import decode as decode_qrcode
7
8
# Used for sending the logs to the ':8888/logs' endpoint
9
import logging
10
# For identifying the source of the logs
11
logger = logging.getLogger("lampix.QRCodeDetector")
12
13
from watcher import Watcher
14
# Will be used to specify the used trigger for this Watcher
15
from watcher import TriggerType
16
17
class QRCodeDetector(Watcher):
18
# Calling the base class constructor
19
def __init__(self, id, contour):
20
# self.contour is the property which holds the corners of the registered area
21
Watcher.__init__(self, id, contour)
22
23
def get_vision_trigger(self):
24
return TriggerType.TRIGGER_RGB
25
26
def on_movement(self, depth_frame, grey_frame, color_frame, movement_mask):
27
# Code that will be executed when an object is placed on the projection surface,
28
# exactly where the watcher was registered
29
logger.info('The QRCodeDetector Watcher was triggered')
30
31
x, y, width, height = cv2.boundingRect(self.contour.astype(numpy.int))
32
33
34
# Cropping the color_frame and getting the ROI (Region of Interest) containing the object that triggered the Watcher
35
qr_code_roi = color_frame[y:y + height, x:x + width]
36
37
# Decoding the QR Code and retrieving the data
38
qr_code = decode_qrcode(qr_code_roi)[0]
39
qr_code_data = qr_code.data
40
41
if len(qr_code_data):
42
# The self.report_to_js() method requires a formatted object
43
reported_object = self.create_formatted_object(metadata=qr_code_data)
44
self.report_to_js(reported_object, method='located')
45
else:
46
reported_object = self.create_formatted_object(message="QR Code not detected")
47
self.report_to_js(reported_object, method='located')
48
49
def on_delete(self):
50
"""
51
Code that will be executed on Watcher's removal
52
e.g. closing connections
53
"""
54
55
pass
Copied!
Copy link