Phission Control Functions
Phission is a new high-performance image processing system for Pyro. It is written in C++, but has methods in Python to access the underlying code. This system is very fast, but has a bit of a learning curve due to its sophisticated functions.
Control Functions are in charge of Phission utilities, such as starting and stopping Phission. They are usually used in class constructors and destructors, as well as from the command line.
setup(capturechannel, capturewidth = 320, captureheight = 240)
-
This function starts up Phission, takes control of the capture device (framegrabber), and opens a display window.
-
Channel number of your capture device: In our lab, the desktop computers use channel 1 and the pioneer robots use channel 0.
-
Width: the width of the image that is captured from the capture device (defaults to 320)
-
Height the height of the image that is captured from the capture device (defaults to 240)
Parameters
destroy()
-
This function shuts down Phission and releases the capture device.
If destroy() is not called before you press the reload button in pyro, you will get a phission error that says it could not open the capture device. If you have this problem you must exit and restart pyro.
pause()
-
This function can be used to pause phission. This can be helpful if you want to freeze the output display so that you can take a better look at filtered frame.
play()
-
This function will make phission resume running the filters after pause has been called.
Filter Functions
Filter Functions do the actual image manipulations. These functions can be called to apply edge detection, blurring, histograming, and motion filtering to the video stream itself. Calling any of these functions will remove all previous filters that have been applied. Any of these filters will continue to run until a new filter (or no_Filter) is inserted.
no_Filter()
-
This function does no filtering. It basically directly connects the capture device to the display window.
canny_Filter()
-
This function will run a Canny edge detection.
sobel_Filter()
-
This function will run a Sobel edge detection.
gaussianBlur_Filter()
-
This function will run a Gaussian blur filter.
medianBlur_Filter()
-
This function will run a median blur filter.
histogramRGB_Filter(x, y, size)
-
This function will run a histogram filter on part of the image. The color space used will be RGB. Basically the histogram will be run on a cube with coordinates (x - size, y - size) to (x + size, y + size). Use the function getHistData() to retrieve the results of the histogram.
-
X location: location on X coordinate for histogram to begin
-
Y location: location on Y coordinate for histogram to begin
-
Size: Sets width and height of the area to histogram
Parameters
histogramHSV_Filter(x, y, size)
-
This function does the same thing as histogramRGB_Filter, however, the HSV color space is used and the function getHistData() will return its result in terms of HSV values.
-
X location: location on X coordinate for histogram to begin
-
Y location: location on Y coordinate for histogram to begin
-
Size: Sets width and height of the area to histogram
Parameters
motion_Filter()
-
This filter will blob on motion in an image. Use the Blob utilities, described below, to get useful information about the largest blob.
ColorTrackRGB_Filter(color, tol)
-
This will blob on a color in the RGB color space. Use the Blob utilities to get useful information about the largest blob.
-
Color: a 3 value tuple in the form of (R, G, B) -- specifies the color to blob on
-
Tolerance: a 3 value tuple in the form of (Rt, Gt, Bt) -- specifies the tolerance of colors the blob will accept (+Rt,+-Gt,+-Bt)
Parameters
ColorTrackHSV_Filter(color, tol)
-
This does the same thing as the last filter but in the HSV color space. The parameters are 3 value tuples in the form of (H, S, V).
-
Color: a 3 value tuple in the form of (H, S, V) -- specifies the color to blob on
-
Tolerance: a 3 value tuple in the form of (H, S, V) -- specifies the tolerance of colors the blob will accept (+Ht,+-St,+-Vt)
Parameters
SpotColorTrackRGB_Filter(x, y, size, tol)
-
This is similar to the color track filters except that insted of specifying a color to track, you specify a location in the image and a histogram filter is run once to detirmine the color at that location. Then blobing is done on that color with the specified tolerance.
-
X location: location on X coordinate for histogram to begin
-
Y location: location on Y coordinate for histogram to begin
-
Size: Sets width and height of the area to histogram
-
Tolerance: a 3 value tuple in the form of (R, G, B) -- specifies the tolerance of colors the blob will accept
Parameters
SpotColorTrackHSV_Filter(x, y, size, tol)
-
This is the same as the last function except it is done in the HSV color space.
-
X location: location on X coordinate for histogram to begin
-
Y location: location on Y coordinate for histogram to begin
-
Size: Sets width and height of the area to histogram
-
Tolerance: a 3 value tuple in the form of (H, S, V) -- specifies the tolerance of colors the blob will accept
Parameters
Data Collecting Functions
Data Collecting Functions are used to get information back from your video stream. This information can be used in programming to allow your robot (or simulated robot) to act differently depending on what it "sees."
getHistData()
-
This function will retrieve the results of the last histogram run in the form of a 3 value tuple. if the color space the histogram ran in is RGB, then the tuples values will be (R, G, B). And if the color space used was HSV, then the tuple values will be (H, S, V). This can also be used just after the SpotColorTrack filters to find out the value of the color that is being blobbed.
getMaxBlob()
-
This function will return a blob data object. This is how to get all the different fields stored in the blob object:
-
Get all the attributes of the blob
-
mass = max_blob.mass
-
center_x = max_blob.cx
-
center_y = max_blob.cy
-
top_left_x = max_blob.x1
-
top_left_y = max_blob.y1
-
bottom_right_x = max_blob.x2
-
bottom_right_y = max_blob.y2
-
blob_width = max_blob.w
-
blob_height = max_blob.h
max_blob = camera.getMaxBlob()
getMaxBlob_cx()
-
This function will return the x coordinate of the center of the largest blob.
getMaxBlob_cy()
-
This function will return the y coordinate of the center of the largest blob.
getMaxBlob_mass()
-
This function will return the mass of the largest blob.
Examples
Apply Random Filter
The following example will continuously loop, changing the current filter every 50 iterations. The new filter will be randomly chosen from seven of the currently supported filters. Each time the filter is changed, the name of the new filter (as well as its parameters) will be displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from pyrobot.brain import Brain
from time import *
from random import *
from phissioncamera import *
class SimpleBrain(Brain):
def setup(self):
self.counter = 0 # initialize counter to 0
def step(self):
self.counter += 1 # increment counter
if ((self.counter % 50) == 0): # run randomFilter() every 50 iterations
self.randomFilter()
def randomFilter(self):
x = randrange(0,7,1) # get a random number
if(x == 0):
self.camera.canny_Filter()
print "canny_Filter()"
if(x == 1):
self.camera.sobel_Filter()
print "sobel_Filter()"
if(x == 2):
self.camera.gaussianBlur_Filter()
print "gaussianBlur_Filter()"
if(x == 3):
self.camera.medianBlur_Filter()
print "medianBlur_Filter()"
if(x == 4):
self.camera.motion_Filter()
print "motion_Filter()"
if(x == 5):
self.camera.SpotColorTrackHSV_Filter(160,120,12, (12,60,120) )
print "SpotColorTrackHSV_Filter(160,120,12, (12,60,120) )"
if(x == 6):
self.camera.SpotColorTrackRGB_Filter(160,120,12, (30,30,30) )
print "SpotColorTrackRGB_Filter(160,120,12, (30,30,30) )"
def INIT(engine):
camera = phissioncamera() # create a new phission camera
camera.setup(1,320,240) # set up the phission camera -- change first param to 0 if not on robot
brain = SimpleBrain('SimpleBrain', engine) # create the brain
brain.camera = camera # set the brain's camera variable
return brain |
Intelligent Motion Track
The following example will begin by finding the largest moving object in the video stream, and capturing the color of that object. The state will then be changed to ColorTrack, where the largest object of the captured color will be tracked. If at any point there is no object at or above a reasonable tolerance (1000 pixles) the state will return to MotionCatch and the process will be repeated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
from pyrobot.brain.behaviors import *
from pyrobot.brain.behaviors.core import *
from time import *
from phissioncamera import *
camera = phissioncamera()
class MotionCatch(State):
def onActivate(self):
print "Activating MotionCatch State"
self.motion_thresh = 1000
camera.motion_Filter()
sleep(1)
def update(self):
max_blob = camera.getMaxBlob()
if(max_blob.mass > self.motion_thresh):
camera.SpotColorTrackHSV_Filter(max_blob.cx, max_blob.cy, 15, (12, 60, 200) )
self.goto('ColorTrack')
class ColorTrack(State):
def onActivate(self):
print "Activating ColorTrack State"
sleep(1)
self.track_thresh = 1000
def update(self):
max_blob = camera.getMaxBlob()
print "-------------------"
print "mass =", max_blob.mass
print "(x,y) = ", max_blob.cx, max_blob.cy
if(max_blob.mass < self.track_thresh):
self.goto('MotionCatch')
class state1(State):
def update(self):
self.goto('MotionCatch')
def INIT(engine):
brain = FSMBrain(engine)
brain.add(state1(1))
brain.add(MotionCatch())
brain.add(ColorTrack())
brain.camera = camera
camera.setup(1,320,240)
return brain |
Fuzzy Camera Example
The following example combines the use of Fuzzy logic and the Phission system to track an object using a PanTiltZoom (PTZ) camera. It does this by taking a color histogram of the center of the initial image. This histogram will be used for the duration of the run. The largest object matching the given color (within the provided tolerance) will be tracked by the camera.
This example requires the use of a PTZ camera
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from pyrobot.brain.fuzzy import *
from pyrobot.brain.behaviors import *
from phissioncamera import *
from time import sleep
class BBB(BehaviorBasedBrain):
def destroy(self):
self.removeDevice("ptz0")
class Avoid (Behavior):
"""Avoid Class"""
def update(self):
if( camera.getMaxBlob_mass() > 200 ):
self.IF(Fuzzy(0, 160 ) << camera.getMaxBlob_cx(), 'pan', 5.0, "pan left")
self.IF(Fuzzy(0, 160 ) >> camera.getMaxBlob_cx(), 'pan', -5.0, "pan right")
self.IF(Fuzzy(0, 120 ) << camera.getMaxBlob_cy(), 'tilt',-5.0, "tilt down")
self.IF(Fuzzy(0, 120 ) >> camera.getMaxBlob_cy(), 'tilt', 5.0, "tilt up")
class state1 (State):
""" sample state """
def setup(self):
print "about to train color"
sleep(10)
camera.SpotColorTrackHSV_Filter(80,60,8, (20, 80,110))
print "color trained!"
self.add(Avoid(1, {'pan': 1, 'tilt': 1}))
def INIT(engine):
engine.robot.startDevice("ptz")
ptz = engine.robot.ptz[0]
brain = BBB({'pan' : ptz.panRel,
'tilt' : ptz.tiltRel,
'update' : engine.robot.update }, engine)
brain.camera = phissioncamera()
brain.camera.setup(0,160,120);
brain.add(state1())
brain.activate('state1')
return brain |
Pyro Modules Table of Contents
-
Pyro - Back to Pyro main page
-
Beyond Legos - NSF grant that pays for Pyro
Modules
Additional Resources
Reference: PyroSiteNotes
