MotionBox: Difference between revisions
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Example of feeding data from [[Motion]] into a PyGame window | Example of feeding data from [[Motion]] into a [[PyGame]] window. | ||
To run the program, you start motion, configured to output coordinate data, then pipe the results to python. '''NB: the use of the -u option to make python run "unbuffered".''' | [[Media:Motionbox.zip]] | ||
To run the program, you start motion, configured to output coordinate data (see motionbox.conf in the zip), then pipe the results to python. '''NB: the use of the -u option to make python run "unbuffered".''' | |||
<source lang="bash"> | <source lang="bash"> | ||
motion -c motionbox.conf | python -u motionbox.py | motion -c motionbox.conf | python -u motionbox.py | ||
</source> | </source> | ||
<source lang="python"> | |||
#!/usr/bin/python | |||
import sys | |||
import pygame | |||
from pygame.locals import * | |||
from pygame.time import Clock | |||
from time import sleep | |||
import thread | |||
def main(): | |||
(mx, my, mw, mh) = (10, 10, 50, 50) | |||
pygame.init() | |||
# screen = pygame.display.set_mode((640, 480), FULLSCREEN, 32) | |||
screen = pygame.display.set_mode((640, 480), 0, 32) | |||
pygame.display.set_caption("motionbox") | |||
clock = Clock() | |||
while True: | |||
# TIMING | |||
clock.tick(30) | |||
# PROCESS EVENTS | |||
for event in pygame.event.get(): | |||
if event.type==QUIT or \ | |||
(event.type == KEYDOWN and event.key == K_ESCAPE): | |||
sys.exit() | |||
elif event.type == USEREVENT: | |||
# print event | |||
mx = event.x | |||
my = event.y | |||
mw = event.w | |||
mh = event.h | |||
# adjust mx, my (as they are a center point) | |||
mx -= mw/2 | |||
my -= mh/2 | |||
screen.fill((0, 0, 0)) | |||
# draw rectangle using current position & size | |||
pygame.draw.rect(screen, (255, 255, 255), (mx, my, mw, mh), 1) | |||
pygame.display.update() | |||
# sleep(0.01) | |||
# start pygame in a separate thread | |||
# starting a thread is like running a program with & on the shell | |||
thread.start_new_thread(main, ()) | |||
# READ INFO FROM STDIN | |||
while 1: | |||
line = sys.stdin.readline() | |||
if (not line): break | |||
line = line.strip() | |||
if line.startswith("motion"): | |||
# print "read motion data:", line | |||
(x, y, w, h, numpixels) = [int(x) for x in line.split()[1:]] | |||
evt = pygame.event.Event(USEREVENT, {"x" : x, "y": y, "w":w, "h":h}) | |||
pygame.event.post(evt) | |||
</source> | |||
Next, [[MotionImage]] |
Latest revision as of 11:09, 1 June 2009
Example of feeding data from Motion into a PyGame window.
To run the program, you start motion, configured to output coordinate data (see motionbox.conf in the zip), then pipe the results to python. NB: the use of the -u option to make python run "unbuffered".
motion -c motionbox.conf | python -u motionbox.py
#!/usr/bin/python
import sys
import pygame
from pygame.locals import *
from pygame.time import Clock
from time import sleep
import thread
def main():
(mx, my, mw, mh) = (10, 10, 50, 50)
pygame.init()
# screen = pygame.display.set_mode((640, 480), FULLSCREEN, 32)
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("motionbox")
clock = Clock()
while True:
# TIMING
clock.tick(30)
# PROCESS EVENTS
for event in pygame.event.get():
if event.type==QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
sys.exit()
elif event.type == USEREVENT:
# print event
mx = event.x
my = event.y
mw = event.w
mh = event.h
# adjust mx, my (as they are a center point)
mx -= mw/2
my -= mh/2
screen.fill((0, 0, 0))
# draw rectangle using current position & size
pygame.draw.rect(screen, (255, 255, 255), (mx, my, mw, mh), 1)
pygame.display.update()
# sleep(0.01)
# start pygame in a separate thread
# starting a thread is like running a program with & on the shell
thread.start_new_thread(main, ())
# READ INFO FROM STDIN
while 1:
line = sys.stdin.readline()
if (not line): break
line = line.strip()
if line.startswith("motion"):
# print "read motion data:", line
(x, y, w, h, numpixels) = [int(x) for x in line.split()[1:]]
evt = pygame.event.Event(USEREVENT, {"x" : x, "y": y, "w":w, "h":h})
pygame.event.post(evt)
Next, MotionImage