Syllabus 2010 t1 ps2 Bernhard
motion -c a2.conf | python -u a2.py
a2.py
#!/usr/bin/python
import sys
import pygame
from pygame.locals import *
from pygame.time import Clock
from time import sleep
import thread
import random
col = 0
cdir = "up"
def nextColor ():
global col
if col == 10:
cdir = "up"
if col == 245:
cdir = "down"
global cdir
if cdir == "up":
col = col + 1
elif cdir == "down":
col = col - 1
return col
def reader ():
while True:
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)
(mx, my, mw, mh) = (0, 0, 0, 0)
pygame.init()
# screen = pygame.display.set_mode((640, 480), FULLSCREEN, 32)
screen = pygame.display.set_mode((640, 480), 0, 32)
screen.fill((55, 55, 55))
screen.convert_alpha()
pygame.display.set_caption("a2")
clock = Clock()
thread.start_new_thread(reader, ())
while True:
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
pygame.draw.line(screen, (nextColor(), nextColor(), nextColor()), (mx, my),(mx+random.randint(-(mw/3), (mh/3)), my+random.randint(-(mw/3), (mh/3))), 1)
pygame.display.update()
clock.tick(30)
a2.conf
daemon off
quiet on
# You may very well need to change this (check with 'dmesg'
# after plugging in your webcam).
videodevice /dev/video0
# Image size in pixels (valid range is camera dependent).
# width 352
# height 288
width 640
height 480
framerate 25
quality 85
auto_brightness off
# General threshold level and noise threshold
# level (for distinguishing between noise and motion).
threshold 4500
noise_level 64
# Initial brightness, contrast, hue (NTSC), and saturation.
# 0 = disabled (valid range 0-255).
brightness 0
contrast 0
saturation 0
hue 0
# Encode movies in real-time (install ffmpeg before enabling).
ffmpeg_cap_new off
# Codec to be used by ffmpeg for the video compression.
# Supported formats: mpeg4, msmpeg4.
ffmpeg_video_codec msmpeg4
# Target base directory for pictures and films (you may need
# to change this (or change its permissions) depending on
# which system user runs motion).
# target_dir /var/lib/motion/snapshots
target_dir .
output_motion off
output_normal off
# locate off
on_motion_detected echo motion %K %L %i %J %D
# FROM THE MOTION DOCS:
# %i Width of the rectangle containing the motion pixels (the rectangle that is shown on the image when locate is on).
# %J Height of the rectangle containing the motion pixels (the rectangle that is shown on the image when locate is on).
# %K X coordinate in pixels of the center point of motion. Origin is upper left corner.
# %L Y coordinate in pixels of the center point of motion. Origin is upper left corner and number is positive moving downwards (I may change this soon).
# %D Number of pixels detected as Motion. If labelling is enabled the number is the number of pixels in the largest labelled motion area.