Teach Pixy an Object 2

Version 24 (Edward Getz, 10/07/2015 08:12 pm)

1 1 Rich LeGrand
h1. Teach Pixy an Object
2 1 Rich LeGrand
3 1 Rich LeGrand
Teaching Pixy an object is super easy, but first let's talk about which objects will work well with Pixy.  Pixy uses a hue-based color filtering algorithm to detect objects.  Since Pixy uses hue (color), the object needs to have a distinct hue.  Here are some objects that are good because they have good, distinct hues.
4 1 Rich LeGrand
5 1 Rich LeGrand
!{width: 10%}http://i74.photobucket.com/albums/i241/cmucam/tennisball_zps93bb0163.jpg!
6 1 Rich LeGrand
!{width: 10%}http://i74.photobucket.com/albums/i241/cmucam/pinkpenguin_zps99f4cb1e.jpg!
7 1 Rich LeGrand
8 1 Rich LeGrand
Here are some bad objects because either there is no hue (black, white or gray) or the hue is not distinct.  
9 1 Rich LeGrand
10 1 Rich LeGrand
!{width: 10%}http://i74.photobucket.com/albums/i241/cmucam/Black_Stapler_zpsc3024407.jpg!
11 1 Rich LeGrand
!{width: 10%}http://i74.photobucket.com/albums/i241/cmucam/stone_zpsd3435436.png!
12 1 Rich LeGrand
13 17 Jesse French
Keeping these guidelines in mind, choose an object to teach Pixy.  (First, apply power to Pixy via battery or USB cable if you haven't already.  When you power up Pixy, it will go through a series of LED flashes.  Wait for the LED to turn off before teaching Pixy an object.)  Now, hold down the button on top of Pixy.  After about 1 second, the LED will turn on - first white, then red, then other colors - but when it turns red, release the button.
14 1 Rich LeGrand
15 15 Jesse French
When you release the button, Pixy will enter what's called "light pipe" mode, where the LED color is the color of the object that Pixy has "locked" onto.  Pixy will lock onto objects in the center of its video frame, so hold the object directly in front of Pixy, between 6 and 20 inches from the lens.  
16 1 Rich LeGrand
17 15 Jesse French
Pixy uses a region growing algorithm to try to determine which pixels are part of your object and which pixels are part of the background.  Using these pixels, Pixy will try to create a statistical model of your object so it can detect it reliably under different lighting conditions.  Use the LED color as feedback to determine if Pixy has a good lock on the object, and use the following guidelines to judge:
18 15 Jesse French
19 1 Rich LeGrand
# When Pixy has locked onto your object, the LED color should match the color of your object.  
20 2 Rich LeGrand
# The brighter the LED, the better the lock.  Move the object a little and see if you can maximize the LED brightness. 
21 22 Edward Getz
# Look at the region-growing grid in the PixyMon window and see how the size of it corresponds to the LED color and brightness.  A grid that covers more of your object is better than a grid that covers only some of your object.  The picture below shows a grid that covers most of the object (good).  
22 22 Edward Getz
!{width: 30%}http://i74.photobucket.com/albums/i241/cmucam/Image%20043_zpsnjzpxikc.png!
23 1 Rich LeGrand
24 24 Edward Getz
Learning when Pixy has a decent lock by looking at the LED might take a little practice, but once you get a feel for it, you can teach Pixy new objects reliably _without needing to use PixyMon_.  A good lock means a good statistical model of your object.  When you are satisfied, press and release the button on Pixy, like you'd click your mouse.  The LED will flash a couple times indicating that Pixy has now "learned" your object.  It will now start tracking your object.
25 1 Rich LeGrand
26 2 Rich LeGrand
The video below is a good short "how-to" regarding teaching Pixy objects.  
27 1 Rich LeGrand
28 13 Rich LeGrand
{{youtube(XdQwZi6l9Ns, 480, 360)}}
29 2 Rich LeGrand
30 7 Rich LeGrand
h3. *Multiple Signatures*
31 3 Rich LeGrand
32 4 Rich LeGrand
Pixy can learn up to seven color signatures. We can teach Pixy the 1st color signature by releasing the button when the LED turns red. If we continue to hold down the button, the LED will turn orange, yellow, etc., indicating the remaining color signatures. Here are the signatures in order:
33 2 Rich LeGrand
34 1 Rich LeGrand
# Red
35 1 Rich LeGrand
# Orange
36 1 Rich LeGrand
# Yellow
37 1 Rich LeGrand
# Green
38 1 Rich LeGrand
# Cyan (light blue)
39 1 Rich LeGrand
# Blue
40 1 Rich LeGrand
# Violet
41 1 Rich LeGrand
42 2 Rich LeGrand
The color signature number is determined by _when_ you release the button.  Release the button when the LED is yellow and you're teaching signature 3.  Release the button when the LED is blue and you're teaching signature 6.  These colors _are not related to the hue of the object_.  The colors are used only to indicate the signature number. So, for example, signature 1 can be a yellow object, even though signature 1 is indicated by a red LED, and signature 2 could be a pink object even though signature 2 is indicated by an orange LED.
43 6 Rich LeGrand
44 1 Rich LeGrand
45 2 Rich LeGrand
After you teach Pixy a signature, it saves the signature in flash, so when you power cycle your Pixy, it will remember the signatures you taught it and continue to track objects that match these signatures.    
46 2 Rich LeGrand
47 9 Rich LeGrand
If you accidentally find yourself teaching signature 2 when you meant to teach signature 1 (i.e. you released the button when it was orange instead of red), for example, just hold down the button until the LED turns off.  This is how you tell Pixy to cancel teach mode.  You can then start over by holding the button down again. 
48 1 Rich LeGrand
49 7 Rich LeGrand
h3. *White Balance*
50 1 Rich LeGrand
51 1 Rich LeGrand
Some types of lighting (such as incandescent) have a reddish hue and others (such as fluorescent) have a bluish hue.  The lighting can affect your color signatures.  For example, if you teach an object under incandescent lighting and move into a room with fluorescent lighting, the color signatures will likely no longer work as well.  You can either re-teach all signatures or you can adjust the white balance.
52 1 Rich LeGrand
53 19 Jesse French
When you first apply power to Pixy, it will spend the first 5 or so seconds determining the correct white balance to use.  It will then disable automatic white balance.  If you wish to readjust the white balance, hold down the button until the LED turns white and release.  It happens quickly, so be prepared!  Pixy is now in automatic white balance mode.  You can hold a white sheet of paper in front of Pixy so Pixy can adjust the white balance.  It only takes 2 or 3 seconds to adjust the white balance, after which you can press and release the button (like a mouse click). The LED will flash, indicating success, and now Pixy is "rebalanced".
54 1 Rich LeGrand
55 17 Jesse French
h3. *Teaching through PixyMon*
56 1 Rich LeGrand
57 17 Jesse French
You can also teach Pixy an object through PixyMon.  This may be useful if the object you want to teach is small, or if you want more control over which pixels are used for teaching. Begin by plugging in the USB cable between Pixy and your computer and running PixyMon.  
58 1 Rich LeGrand
59 1 Rich LeGrand
Now hold the object you want to teach in front of Pixy and select *Action➜Set signature 1* from the pulldown menu.
60 1 Rich LeGrand
61 14 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image%20176_zpsr3k7xfzb.jpg!
62 1 Rich LeGrand
63 19 Jesse French
Using the mouse, click and drag to select the region you want Pixy to use to learn the object.  
64 1 Rich LeGrand
65 14 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image%20177_zpsyweskkhy.jpg!
66 1 Rich LeGrand
67 17 Jesse French
That's it! After you select the region, Pixy will "learn" the object and automatically go into "cooked" video mode so you can verify how well your color signature is working.
68 1 Rich LeGrand
69 14 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image%20178_zpswacbisoj.jpg!
70 1 Rich LeGrand
71 2 Rich LeGrand
h3. *Signature tuning*
72 1 Rich LeGrand
73 10 Rich LeGrand
Sometimes the color signatures you teach Pixy need to be "tweaked" -- that is, you are getting some false positives (Pixy is detecting objects that aren't the objects that you intended) or false negatives (Pixy isn't detecting the object you just taught it, or it's detecting the object intermittently.)  You can tweak things by bringing up the Configure dialog (click on the gear icon or select *File➜Configure*). Select the *Signature Tuning* pane under *Pixy Parameters*.
74 1 Rich LeGrand
75 12 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image149_zps24e932e4.jpg(Signature Tuning pane)!
76 1 Rich LeGrand
77 17 Jesse French
Use the slider for *Signature 1 range* to adjust the inclusiveness of signature 1 (assuming it's signature 1 you're wanting to adjust).  Slide it to the left if you want to be less inclusive (i.e. if you're seeing false positives, like the picture below):
78 1 Rich LeGrand
79 12 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image138_zps864e1453.jpg!
80 1 Rich LeGrand
81 17 Jesse French
or slide it to the right if you want to be more inclusive (i.e. you're seeing false negatives) or the detection is intermittent or sparse (like in the picture below): 
82 12 Rich LeGrand
83 12 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image139_zps6b4f9c11.jpg!
84 12 Rich LeGrand
85 17 Jesse French
Choose a slider value that provides good strong detection, like the picture below:
86 12 Rich LeGrand
87 12 Rich LeGrand
!{width: 40%}http://i74.photobucket.com/albums/i241/cmucam/Image137_zpsa10ad09e.jpg!
88 12 Rich LeGrand
89 12 Rich LeGrand
You can adjust all seven color signatures this way to maximize detection accuracy for all signatures.  Be sure to press *Apply* or *OK* to save the slider ranges!  The adjusted values won't be saved if you press *Cancel* or dismiss the dialog.
90 1 Rich LeGrand
91 19 Jesse French
There are more tips "here":/projects/cmucam5/wiki/Some_Tips_on_Generating_Color_Signatures_2 on how to improve the color signatures.