Lampix Apps API
  • Introduction
  • Application Development
    • Getting Started
      • Up and Running
      • Boilerplate
    • Step by step app
      • What We'll Build
      • Environment Setup
      • Styling
      • HTML Structure
      • NeuralNetworkClassifier
      • MovementBasedSegmenter
      • Final Step
      • Extras
    • LampixJS
      • API Reference
        • Watcher
        • RegisteredWatcher
        • .watchers.add
        • .watchers.remove
        • .watchers.pauseAll
        • .watchers.resumeAll
        • .presets.button
        • .helpers.rectangle
        • getLampixInfo
        • switchToApp
        • exit
        • getApps
        • getAppConfig
        • getAppMetadata
        • writeJsonToFile
        • readJsonFromFile
        • transformRectCoords
        • constants
      • Examples
        • NeuralNetworkClassifier: Buttons
        • MovementBasedSegmenter
        • Counter App
      • Migrating from v0.x.x to v1.0.0-beta.x
      • Ecosystem
    • Deploying
      • Application Structure (production)
      • Local Deploy
    • Standard Watchers
    • Custom Watchers
      • Description
      • Environment Setup
      • Directory Structure
      • End result
      • QRCodeDetector implementation
    • Community
  • Lampix Simulator
    • Installation
    • Usage
      • Basics
Powered by GitBook
On this page

Was this helpful?

  1. Application Development
  2. Custom Watchers

End result

# The images will be processed using OpenCV and Numpy
import cv2
import numpy

# The decode_qrcode function will be used for decoding the found QR Code
from pyzbar.pyzbar import decode as decode_qrcode

# Used for sending the logs to the ':8888/logs' endpoint
import logging
# For identifying the source of the logs
logger = logging.getLogger("lampix.QRCodeDetector")

from watcher import Watcher
# Will be used to specify the used trigger for this Watcher
from watcher import TriggerType

class QRCodeDetector(Watcher):
    # Calling the base class constructor
    def __init__(self, id, contour):
        # self.contour is the property which holds the corners of the registered area
        Watcher.__init__(self, id, contour)

    def get_vision_trigger(self):
        return TriggerType.TRIGGER_RGB

    def on_movement(self, depth_frame, grey_frame, color_frame, movement_mask):
        # Code that will be executed when an object is placed on the projection surface, 
        # exactly where the watcher was registered
        logger.info('The QRCodeDetector Watcher was triggered')

        x, y, width, height = cv2.boundingRect(self.contour.astype(numpy.int))


        # Cropping the color_frame and getting the ROI (Region of Interest) containing the object that triggered the Watcher
        qr_code_roi = color_frame[y:y + height, x:x + width]

        # Decoding the QR Code and retrieving the data
        qr_code = decode_qrcode(qr_code_roi)[0]
        qr_code_data = qr_code.data

        if len(qr_code_data):
            # The self.report_to_js() method requires a formatted object
            reported_object = self.create_formatted_object(metadata=qr_code_data)
            self.report_to_js(reported_object, method='located')
        else:
            reported_object = self.create_formatted_object(message="QR Code not detected")
            self.report_to_js(reported_object, method='located')

    def on_delete(self):
        """
        Code that will be executed on Watcher's removal
        e.g. closing connections
        """

        pass
PreviousDirectory StructureNextQRCodeDetector implementation

Last updated 6 years ago

Was this helpful?