Aldebaran documentation What's new in NAOqi 2.4.3?

ALVisionRecognition Tutorial

NAOqi Vision - Overview | API | Tutorial


Introduction

This tutorial explains two ways to access the ALVisionRecognition module from Python: (1) subscribing to AlMemory key that contains the detection result of ALVisionRecognition, or (2) calling ALVisionRecognition APIs via Choregraphe Script box.

Subscribing to ALVisionRecognition’s memory key

The module is designed to notify subscribers when a previously learned image has been recognized. To receive the notification, we need to create a module that exposes a callback method.

Note

You might not have this module depending on your distribution. This module should be loaded on the robot’s NAOqi.

After some initialization steps, we first instantiate a proxy to the ALVisionRecognition module.

Prepare some imports and variables

import os
import sys
import time
import naoqi
from naoqi import *

# A global counter of the number of loops
count = 10

PC_IP = "127.0.0.1"  # Replace this with your computer's IP address
NAO_IP = "nao.local" # Replace this with your NAOqi's IP address

# The name of the event generated by ALVisionRecognition
event_name = "PictureDetected"

We create a python module with a callback method and instantiate it

# The name of our local python module
module_name = "python_module"

class myModule(ALModule):
"""python class myModule test auto documentation"""

  def dataChanged(self, strVarName, value, strMessage):
    """callback when data change"""

    print "datachanged", strVarName, " ", value, " ", strMessage
    global count
    count = count - 1

# Create a local broker, connected to the remote naoqi
broker = ALBroker("pythonBroker", PC_IP, 9999, NAO_IP, 9559)

# Create a python module
pythonModule = myModule(module_name)

We subscribe to event so that our callback can be called

try:
  # Create a proxy to ALMemory
  memoryProxy = ALProxy("ALMemory", ROBOT_IP, PORT)

  # Subscribe to the event, saying where we want to be called back
  memoryProxy.subscribeToEvent(event_name, module_name, "dataChanged")

  # Let the picture recognition run
  while count > 0:
    time.sleep(5)

  # Unsubscribe
  memoryProxy.unsubscribeToEvent(event_name, module_name)

except RuntimeError, e:
  print e
  exit(1)

print 'end'

Calling APIs of ALVisionRecognition from a Choregraphe Script box

Access to ALVisionRecognition API via Choregraphe Script box can be done easily under Python, like below:

class MyClass(GeneratedClass):
    def __init__(self):
  GeneratedClass.__init__(self)

    def onLoad(self):
  #put initialization code here
  pass

    def onUnload(self):
  #put clean-up code here
  pass

    def onInput_onStart(self):
  #Get connected to the ALVisionRecognition module
  vr = ALProxy("ALVisionRecognition")

  #Get the size of the current database
  self.log( "DB size: %s" % vr.getSize())

  self.onStopped() #activate the output of the box
  pass

    def onInput_onStop(self):
  self.onUnload() #it is recommended to reuse the clean-up as the box is stopped