Programming AI with Leaf/Creating Your Own Leaf

From Wikibooks, open books for an open world
< Programming AI with Leaf
Jump to: navigation, search

Creating Your Own Leaf, a Tutorial[edit]

Adding a new person to Leaf

The following applies to version 01-01-07 only, although it may be a guide to other versions.

There are 2 parts to this process. One part is adding the person to the Leaf "brain" and the other loading up a reference image so Leaf can "See" you.

We'll start with adding you to Leafs brain. Not much point in Leaf being able to see you if Leaf doesn't know who s/he is looking at.

Note in some of the code snippets there is a 'characture in front of the word. This is important and it must be the correct punctuation mark otherwise the lisp compiler will give an error when you save the file.

In this example we will use the name “BOB” if you aren’t BOB then change BOB to your name.

First leaf needs to be able to understand you when you say your name. Go to “speech.xml “ and add

       <P>this is Bob</P>




       <P>this is Bruce</P>

Then save the file.

Open “leaf.lisp” in the lisp editor (save a copy first in case you have problems)

First we need an object for the person.

Locate the line

     ;;;; OBJECTS: "nothing person alex bruce gary robin obstacle wall door recharger"

This is the start of the area to add the new person.

Scroll down to just after

     (make-instance 'robin))

And before

     ;;;  defclass:osla:object:toy  ;;; 

Robin is the last person currently defined

Add a new person with the following code

     ;;;  defclass:osla:object:person:bob  ;;;
     (defclass bob (person) ())
     ;;    defparameter:osla:object:person:robin
     (defparameter bob
      (make-instance 'bob))

Leaf now has an object to store you in.

Now you would like Leaf to play a game with you so locate the line

     ;;;  defclass:osla:activity:play  ;;;

This is the start of where games are configured for each person.

Scroll down to the gap between

     (defparameter playgary
      (make-instance 'play
       :happy '(setf happy (+ happy 10))
       :sad '(setf sad (/ sad 2))))



Gary is the last person to have a game defined. We will set bob to play “mine sweeper” We will also make Leaf more happy and less sad when we play.

Insert the code

     (defmethod play ((x bob)) ;for specific instances
       "(play bob)"
        (sapi-tts '"Would you like to play mine sweeper?")
        (system:call-system '("c:\\WINDOWS\\system32\\winmine.exe") :wait nil))
     (defparameter playbob
      (make-instance 'play
       :happy '(setf happy (+ happy 10))
       :sad '(setf sad (/ sad 2))))

We also want leaf to play music for you. We will use “mission impossible” & we will also make Leaf more happy and less sad

This is the following section so scroll down to between

     (defparameter playmusicbruce
      (make-instance 'playmusic
       :happy '(setf happy (+ happy 10))
       :sad '(setf sad (/ sad 2))))



And add the following

     (defmethod playmusic ((play-bob bob)) ;for specific instances
       "(playmusic bob)"
        (sapi-tts '"Here's mission impossible.")
        (PlayWav mi01))
     (defparameter playmusicbob
      (make-instance 'playmusic
       :happy '(setf happy (+ happy 10))
       :sad '(setf sad (/ sad 2))))

We want leaf to have feelings about us so we add a behaviour script.

Locate the line

     ;;;;  BEHAVIOR SCRIPTS  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

and scroll down to between

     (defun robinscript ()
       (NowFocusOn 'robin)
       (setf lonely 0)
       (sapi-tts '"Hello, Robin.")
       (sapi-tts (concatenate 'string "Now I'm feeling " (updatemood))))


      (defun bebravescript ()

Robin is the last person. Add the following

     (defun bobscript ()
       (NowFocusOn 'bob)
       (setf lonely 0)
       (sapi-tts '"Hello, Bob.")
       (sapi-tts (concatenate 'string "Now I'm feeling " (updatemood))))

We can also update the lonely script so Leaf will ask for Bob.

Locate the line

     (defun lonelyscript ()

and scroll down to

     (sapi-tts (string (car (imagine-if '(ana bruce alex gary)))))

Add Bob to this line so it now reads

     (sapi-tts (string (car (imagine-if '(ana bruce alex gary bob)))))

If we want Leaf to be able to like us best locate

     (defun whodoyoulikebestscript ()

scroll to the line

     (sapi-tts (string (car (imagine-if '(ana bruce alex robin gary)))))

and change it to

     (sapi-tts (string (car (imagine-if '(ana bruce alex robin gary bob)))))

Lastly we want Leaf to act when we say our name so locate the line


and scroll down to between

     ((string-equal *text-phrase*
                    (concatenate 'string robot " this is Ana"))  ;behavior
      (setf *text-phrase* nil)


     ((string-equal *text-phrase*
                    (concatenate 'string robot " who am I"))  ;behavior
      (setf *text-phrase* nil)

and add the following

     ((string-equal *text-phrase*
                    (concatenate 'string robot " this is Bob"))  ;behavior
      (setf *text-phrase* nil)

Save and compile.

Leaf will now know who you are when you say your name.

Try saying

"leaf this is bob"

"leaf play some music"

Leaf should play "mission impossible"

Now we can add our picture so Leaf knows what we look like.

First make sure leaf is working and can see the camera.

Ask leaf “leaf who am I” leaf should respond with “you look like XXX ” where XXX could be Gary, Alex, Ana, or Bruce.

If Leaf responds with “I can’t see you” try moving so you are directly looking at the camera, as this appears to indicate that OpenCV could not locate a face in the image.

For me it works fine from about 18” through to 3 feet on a cheap web cam.

Once you have had leaf respond with “you look like XXX” you are ready to make Leaf recognise you.

Open the folder “C:\program files\leaf\leaffaces” in there you will find a file “image.jpg”. Note this image does not have a number at the end.

Open this file in an image viewer and check it looks something like you. It doesn’t need to be very good.

Rename this file to “image4.jpg” (assuming you already have images 0 to 3 which are Gary, Alex, Ana, & Bruce).

Open the file “names.txt” in the same directory and add your name to the end of the list. Use capital letters and a space between BRUCE and your name. Save the file.

Open the file “distances.txt” in the same directory and add the following

      “Euclidean distance to image 4:

Note that there is a line between the last entry ant this new text and a blank line at the end. Save this file.

Now when you ask Leaf “Leaf who am I?” Leaf should respond with your name. and now the new and great moe!!