Realtime image processing in Python
morepypy.blogspot.com
morepypy.blogspot.com
grab the archive http://wyvern.cs.uni-duesseldorf.de/~antocuni/pypy-image-dem...
brew install pypy
brew install mplayer
pypy demo/magnify.py
pypy demo/sobel.py
For me, 24 average fps on magnify.py, 34 average fps on sobel.we use a c++ wrapper around opencv as well.
opencv isn't valuable for its algorithms or its api. The opencv value proposition is tied up with painstaking optimization of the inner loops of several high level operations using SIMD intrinsics.
Advances in compiler technology seem to be pointing towards generated code with similar levels of optimization especially in JIT generated code.
First off, PyPy uses a JIT, so there's no obvious reason why it would have to be slower than 'optimized SIMD code' (whatever that is). The actual performance all depends on the quality of the JIT and the quality of the input into the JIT.
Second, they clearly state in the blog post that the PyPy version of the algorithm is easier to write than the equivalent C++, because the JIT can transform their polymorphic Python into efficient native code - doing the same with C++ would require the use of template expansion or a code generator.
Third, if the idea of sacrificing a tiny amount of performance in order to reduce the cost of development and maintenance is that abhorrent to you, Python is almost certainly Not For You.
If you don't know what SIMD is, you probably shouldn't even talking about high-performance image processing in the first place. The parent is correct that this is probably still at least a full order of magnitude slower than proper SIMD code.
But comparing this to good SIMD is not quite fair, as the intent of this sort of JIT is to be competitive with naive compiled C, not to be competitive with optimized assembly routines. Neither is attempting to -- or has a chance of -- replacing proper hand-written SIMD for performance-critical code.
If you want to automatically generate SIMD code, you'd want something more along the lines of a special-purpose vector language, like orc.
I see this as being similar to why we're only now taking abstractions and compilers geared towards parallelism seriously for mainstream programming: not enough people needed it to justify the effort required.
But in python, the vector operations are normally programmed using simple array semantics. For example in NumPy:
>>> a = array( [2,3,4] )
>>> b = array( [2,3,4] )
>>> a+b
[4,6,8]
These can be easily converted into SIMD operations.It's matter of time before PyPy's new NumPy implementation take traction and make simply beautiful optimizing JITs using that.
Mathematically intense code may run faster when it is compiled JIT, but Python users have relied on specially made libraries (Numpy/Scipy/Gmpy/etc.) to get serious speedup.
http://qinsb.blogspot.com/2011/03/unladen-swallow-retrospect...