The beginning of a demo


In [5]:
from SimpleCV import *
from time import sleep
#import pytesseract
from bs4 import BeautifulSoup

Here we set up the camera


In [6]:
cam = Camera()

Now we also get a single image from the cam, so mug it up!


In [7]:
img = cam.getImage()

In Ipython Notebook, you can sometimes use TAB autocompletion if the variable is in memory.

Let's see what "sh" brings up.

img.sh

Let's try shrinking beforehand


In [8]:
img.scale(0.5).show()
# Okay, let's keep that
img = img.scale(0.5)

Let's try getting some documentation


In [9]:
img.edges?

Okay, cool - built in documentation. Good.

Let's draw some text on it.


In [10]:
# This draws directly onto the image (in place)
img.drawText('RASPBERRY Pi', 10, 10, color=Color.BLUE, )
img.show()


Out[10]:
<SimpleCV.Display Object resolution:((320, 240)), Image Resolution: (320, 240) at memory location: (0x6aa73cb0)>

In [11]:
#Let's try bigger font
img.drawText('RASPBERRY Pi', 10, 10, color=Color.ORANGE, fontsize=50)
img.drawText('SimpleCV', 10, 100, color=Color.ORANGE, fontsize=50)
img.show()


Out[11]:
<SimpleCV.Display Object resolution:((320, 240)), Image Resolution: (320, 240) at memory location: (0x6a9eb670)>

In [12]:
#from SimpleCV import Image, Color, Display

# Make a function that does a half and half image.
def halfsies(left,right): 
    result = left
    # crop the right image to be just the right side.
    crop   = right.crop(right.width/2.0,0,right.width/2.0,right.height)
    # now paste the crop on the left image.
    result = result.blit(crop,(left.width/2,0))
    # return the results.
    return result
# Load an image from imgur.
#img = Image('http://i.imgur.com/lfAeZ4n.png')
# binarize the image using a threshold of 90 
# and invert the results.
output = img.binarize().invert()
# create the side by side image.
result = halfsies(img,output)

result.show()


Out[12]:
<SimpleCV.Display Object resolution:((320, 240)), Image Resolution: (320, 240) at memory location: (0x6a9ebda0)>

In [12]:

Let's look at some people

Who's there?

This function lists what features we can detect (built in).


In [13]:
img.listHaarFeatures()


['face3.xml', 'mouth.xml', 'glasses.xml', 'fullbody.xml', 'face.xml', 'nose.xml', 'lower_body.xml', 'right_eye2.xml', 'upper_body.xml', 'face2.xml', 'face_cv2.xml', 'right_ear.xml', 'left_ear.xml', 'left_eye2.xml', 'face4.xml', 'right_eye.xml', 'upper_body2.xml', 'two_eyes_big.xml', 'lefteye.xml', 'two_eyes_small.xml', 'eye.xml', 'profile.xml']

In [14]:
ppl = Image('group.jpg')
ppl = Image('tng.jpg')
ppl.show()


Out[14]:
<SimpleCV.Display Object resolution:((275, 183)), Image Resolution: (275, 183) at memory location: (0x6aa11440)>

In [15]:
#obs = ppl.findHaarFeatures('face.xml')
obs = ppl.findHaarFeatures('upper_body.xml')

for f in obs:
    print str(f.coordinates())
    f.draw(color=Color.YELLOW, width=4)
    
    
ppl.show()


[227  36]
[58 37]
[178  40]
[94 49]
[36 65]
[210  78]
[125  39]
Out[15]:
<SimpleCV.Display Object resolution:((275, 183)), Image Resolution: (275, 183) at memory location: (0x6aa11828)>

Look at the image set as a whole


In [16]:
print obs.sortX()

# Highlight the person on the end
feat1 = obs.sortX()[-1]
feat1.draw(color=Color.RED, width=4)

feat2 = obs.sortX()[0]


# reshow the image after drawing in it
ppl.show()


[SimpleCV.Features.Detection.HaarFeature at (36,65), SimpleCV.Features.Detection.HaarFeature at (58,37), SimpleCV.Features.Detection.HaarFeature at (94,49), SimpleCV.Features.Detection.HaarFeature at (125,39), SimpleCV.Features.Detection.HaarFeature at (178,40), SimpleCV.Features.Detection.HaarFeature at (210,78), SimpleCV.Features.Detection.HaarFeature at (227,36)]
Out[16]:
<SimpleCV.Display Object resolution:((275, 183)), Image Resolution: (275, 183) at memory location: (0x6aa11df0)>

In [17]:
# Here's a function that may help you swap parts in two images.
def swap( img1, f1, f2):
    # Gte a "mask" that is the first feature cropped and ressized 
    # to the size of the second feature.
    f1mask = f1.crop().resize(w=f2.width(), h=f2.height())
    
    # get the similar for the other
    f2mask = f2.crop().resize(w=f1.width(), h=f1.height())
    
    
    # Set up the output images
    out1 = img1.blit( f2mask, f1.topLeftCorner(), alpha=0.8)
    out2 = out1.blit( f1mask, f2.topLeftCorner(), alpha=0.8)
    
    return out1, out2

In [18]:
# Run the swap and ignore the first return value
img3, img4 = swap( ppl, feat1, feat2)


img4.show()


Out[18]:
<SimpleCV.Display Object resolution:((275, 183)), Image Resolution: (275, 183) at memory location: (0x6aa070f8)>

In [ ]:
while True:
    # Reduce the image size
    img = cam.getImage().scale(0.5)
    faces = img.findHaarFeatures("face.xml")
    for f in faces:
        f.draw(width=3)

    img.show()

In [ ]:
### NOTE: I think you'll want to move this code to the while loop above so it's executed repeatedly.
# That while loop never exits right now (always True)
face1 = faces.sortX()[]
face2= faces.sortX()[1]

img1 = swap (img, face1, face2)

img1.show()

In [ ]: