Thread: N900 Sentry
View Single Post
laasonen's Avatar
Posts: 565 | Thanked: 618 times | Joined on Jun 2010 @ Finland
#2
I once made python script like this for my netbook. Ill try to find it and test how it works on n900.

I couldn't find my script, but my friend who tried to do the same thing at the time had his

It used use fswebcam, but because we don't have it on maemo, I switched to gstreamer, but it works really slowly, so any faster ideas are welcome
Code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import subprocess
import math
import Image
import os

def imgdiff(A,B):
	Asize = A.size
	Bsize = B.size
	if(Asize != Bsize):
		B = B.resize(Asize)
	Apix = A.load()
	Bpix = B.load()
	diff = 0
	for x in range(0,Asize[0]/10):
		for y in range(0,Asize[1]/10):
			diff += math.sqrt((Apix[x*10,y*10][0]-Bpix[x*10,y*10][0])**2 + (Apix[x*10,y*10][1]-Bpix[x*10,y*10][1])**2 + (Apix[x*10,y*10][1]-Bpix[x*10,y*10][1])**2)

	return 1 - (diff/(Asize[0]/10*Asize[1]/10))/(255.0*math.sqrt(3))

#Faster way on maemo?
def takePic():
	return subprocess.Popen("/usr/bin/gst-launch v4l2camsrc device=/dev/video0 num-buffers=1 \! video/x-raw-yuv,width=640,height=480  \! ffmpegcolorspace \! jpegenc \! filesink location=temp.jpg >> /dev/null", shell=True)

p = takePic()
sts = os.waitpid(p.pid, 0)
A = Image.open("temp.jpg")
p = takePic()

while True:
	if p.poll() == 0:
		B = A
		A = Image.open("temp.jpg")
		p = takePic()
		diff = imgdiff(A, B)
		if diff < 0.97:
			print "Moves"

Last edited by laasonen; 2011-03-17 at 22:01.