Author: Hakan Ardo <[email protected]>
Branch: extradoc
Changeset: r3641:f231edc39c26
Date: 2011-06-11 13:48 +0200
http://bitbucket.org/pypy/extradoc/changeset/f231edc39c26/
Log: sobel demo
diff --git a/talk/iwtc11/benchmarks/image/io.py
b/talk/iwtc11/benchmarks/image/io.py
--- a/talk/iwtc11/benchmarks/image/io.py
+++ b/talk/iwtc11/benchmarks/image/io.py
@@ -1,7 +1,7 @@
import os, re, array
-def mplayer(Image, fn='tv://'):
- f = os.popen('mplayer -really-quiet -noframedrop ' +
+def mplayer(Image, fn='tv://', options=''):
+ f = os.popen('mplayer -really-quiet -noframedrop ' + options + ' '
'-vo yuv4mpeg:file=/dev/stdout 2>/dev/null </dev/null ' + fn)
hdr = f.readline()
m = re.search('W(\d+) H(\d+)', hdr)
diff --git a/talk/iwtc11/benchmarks/image/noborder.py
b/talk/iwtc11/benchmarks/image/noborder.py
--- a/talk/iwtc11/benchmarks/image/noborder.py
+++ b/talk/iwtc11/benchmarks/image/noborder.py
@@ -44,17 +44,23 @@
self[x, y] = data[y][x]
return self
- def clone(self):
- return self.__class__(self.width, self.height)
+ def clone(self, **kwargs):
+ return self.__class__(self.width, self.height, **kwargs)
def tofile(self, f):
self.data.tofile(f)
class NoBorderImagePadded(NoBorderImage):
- def __init__(self, w, h):
+ def __init__(self, w, h, typecode='d', fromfile=None):
self.width = w
self.height = h
- self.data = array('d', [0]) * (w*(h+2)+2)
+ self.typecode = typecode
+ if fromfile is None:
+ self.data = array(typecode, [0]) * (w*(h+2)+2)
+ else:
+ self.data = array(typecode, [0]) * (w + 1)
+ self.data.fromfile(fromfile, w*h)
+ self.data += array(typecode, [0]) * (w + 1)
def _idx(self, p):
if isinstance(p, Pixel):
@@ -68,6 +74,9 @@
def pixelrange(self):
return xrange(self.width + 1, (self.width+1) * self.height + 1)
+ def tofile(self, f):
+ self.data[(self.width+1):(-self.width-1)].tofile(f)
+
class Pixel(object):
def __init__(self, idx, image):
diff --git a/talk/iwtc11/benchmarks/image/sobel.py
b/talk/iwtc11/benchmarks/image/sobel.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/image/sobel.py
@@ -0,0 +1,58 @@
+from noborder import NoBorderImagePadded
+from math import sqrt
+
+def sobeldx(img):
+ res = img.clone(typecode='d')
+ for p in img.pixeliter():
+ res[p] = (-1.0 * img[p + (-1,-1)] + 1.0 * img[p + (1,-1)] + \
+ -2.0 * img[p + (-1, 0)] + 2.0 * img[p + (1, 0)] + \
+ -1.0 * img[p + (-1, 1)] + 1.0 * img[p + (1, 1)]) / 4.0
+ return res
+
+def sobeldy(img):
+ res = img.clone(typecode='d')
+ for p in img.pixeliter():
+ res[p] = (-1.0*img[p + (-1,-1)] -2.0*img[p + (0,-1)] -1.0*img[p +
(1,-1)] + \
+ 1.0*img[p + (-1, 1)] +2.0*img[p + (0, 1)] +2.0*img[p + (1,
1)]) / 4.0
+ return res
+
+def sobel_magnitude(img):
+ res = img.clone(typecode='d')
+ for p in img.pixeliter():
+ dx = -1.0 * img[p + (-1,-1)] + 1.0 * img[p + (1,-1)] + \
+ -2.0 * img[p + (-1, 0)] + 2.0 * img[p + (1, 0)] + \
+ -1.0 * img[p + (-1, 1)] + 1.0 * img[p + (1, 1)]
+ dy = -1.0*img[p + (-1,-1)] -2.0*img[p + (0,-1)] -1.0*img[p + (1,-1)] +
\
+ 1.0*img[p + (-1, 1)] +2.0*img[p + (0, 1)] +2.0*img[p + (1, 1)]
+ res[p] = sqrt(dx**2 + dy**2) / 4.0
+ return res
+
+def uint8(img):
+ res = img.clone(typecode='B')
+ for p in img.pixeliter():
+ res[p] = min(max(int(img[p]), 0), 255)
+ return res
+
+if __name__ == '__main__':
+ from io import mplayer, view
+ import sys
+ from time import time
+
+ if len(sys.argv) > 1:
+ fn = sys.argv[1]
+ else:
+ fn = 'test.avi'
+
+ try:
+ import pypyjit
+ pypyjit.set_param(trace_limit=200000)
+ except ImportError:
+ pass
+
+ start = time()
+ for fcnt, img in enumerate(mplayer(NoBorderImagePadded, fn)):
+ #view(img)
+ #sobeldx(img)
+ view(uint8(sobel_magnitude(img)))
+ print 1.0 / (time() - start), 'fps'
+ start = time()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit