ALVisionRecognition Tutorial

NAOqi Vision - Overview | API | Tutorial


Introduction

This tutorial explains how to access the ALVisionRecognition module from Python. 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.

Getting a proxy to ALVisionRecognition

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'