video

video.py a component of openallure.py

Implement webcam image capture and processing

Copyright (c) 2010 John Graves

MIT License: see LICENSE.txt

class video.GreenScreen

Process to capture average background image and subtract from snapshot image

calibration(snapshot)
Capture 30 background images and average them out.
process(snapshot)
Toggles between returning calibration image and processed ( green screen ) image.
threshold(snapshot)

Finds which pixels are beyond a threshold of the average background and makes them white.

pygame.transform.threshold( DestSurface, Surface, color, threshold = ( 0,0,0,0 ), diff_color = ( 0,0,0,0 ), change_return = 1, Surface = None, inverse = False ): return num_threshold_pixels

Finds which, and how many pixels in a surface are within a threshold of a color.

It can set the destination surface where all of the pixels not within the threshold are changed to diff_color. If inverse is optionally set to True, the pixels that are within the threshold are instead changed to diff_color.

If the optional second surface is given, it is used to threshold against rather than the specified color. That is, it will find each pixel in the first Surface that is within the threshold of the pixel at the same coordinates of the second Surface.

If change_return is set to 0, it can be used to just count the number of pixels within the threshold if you set. If change_return is set to 1, the pixels set in DestSurface will be those from the color. If change_return is set to 2, the pixels set in DestSurface will be those from the first Surface.

You can use a threshold of ( r,g,b,a ) where the r,g,b can have different thresholds. So you could use an r threshold of 40 and a blue threshold of 2 if you like.

New in pygame 1.8

Comments:

November 11, 2009 3:11pm - Anonymous This documentation does not seem to match what is currently in 1.8.1. Instead: pygame.transform.threshold( DestSurface, Surface, color, threshold = ( 0,0,0,0 ), diff_color = ( 0,0,0,0 ), change_return = True, Surface =None ): return num_threshold_pixels

January 15, 2009 11:36am - Anonymous The target surface is first filled with diff_color. A pixel is matched if it’s distance from the color-argument ( or the corresponding pixel from the optional third surface ) is less than threshold_color ( for every color component ). If a pixel is matched, it will be set to color. The number of matched pixels is returned.

So, if color = ( 255,0,0 ), and threshold_color = ( 10,10,10 ), any pixel with value ( r>245, g<10, b<10 ) will be matched.

From http://www.pygame.org/docs/ref/transform.html#pygame.transform.threshold

class video.VideoCapturePlayer(processFunction=None, display=None, photos=None, **argd)

A VideoCapturePlayer object is an encapsulation of the processing and display of a video stream.

A process can be given (as a function) that is run on every frame between capture and display.

For example a filter function that takes and returns a surface can be given. This player will take the webcam image, pass it through the filter then display the result.

If the function takes significant computation time (>1second) The VideoCapturePlayer takes 3 images between each, this flushes the buffer, ensuring an updated picture is used in the next computation.

This class uses the pygame.camera module.

get_and_flip()
Use webcam to take a snapshot, flip it right-to-left, subtract the background ( green screen ) and then display it.
listen()
smile()
talk()

Previous topic

voice

Next topic

CHANGES.txt