How to use Python OpenCV functions in Sikuli - python

I'd like to use OpenCV functions, like Hough Transform and Corner Detection, in Sikuli.
I tried the OpenCV installation instructions for CPython on Sikuli and it's a no go.
I understand that Sikuli is Jython and this might be the hard way to do things. What are the easier alternatives?
I'd still like to use Sikuli & Python because the code I write just works; but maybe I'm hitting the limits of Sikuli.

You could use Automa as an alternative to Sikuli. It is a pure Python library (so you can use any other Python library with it) and its image search algorithms are fully compatible with Sikuli. In fact, it uses OpenCV's Python bindings itself, and thus ships with them.
Disclaimer: I'm one of Automa's developers.

Generally spoken: Python modules containing C-based code (which is the case for Python OpenCV) cannot be used in the Sikuli Script (Jython).
The integration of some OpenCV features in Sikuli is realized using the Java-To-C interface (JNI) and the usage of OpenCV features itself is coded in C++.
If you want to use OpenCV features not supported by Sikuli, then you either have to use the above mentioned JNI approach or use the Java implementation of OpenCV (javacv) (Java stuff can be used seamlessly from Jython scripts).
Another option of course is to implement the additional things in plain Python scripts using the Python/OpenCV and call these scripts via the C-Python interpreter in a subprocess.

Related

Python equivalent of MATLAB function findchangepts

I need a python equivalent of the MATLAB command findchangepts . I am unable to find one yet. Essentially I want to find K abrupt changes in my time series.
I am able to use the matlab command but unable to find an equivalent function in python. Any help/pointers to existing libraries will be very helpful. I am not proficient in the optimization modules of scipy and thus would prefer an existing python package.
Thanks.
If you cannot use Matlab under the hood there is e.g. ruptures which implements change point detection (docs).
Just in case the answer might help:
I have both MATLAB and Python on my computer. I was able to use MATLAB bindings for python to use findchangepts in my python code.
Source for bindings : MATLAB API for Python

How can i use Sikuli in my python code OR python libraries in Sikuli script

I want to make a python base application in which i have to take decisions in my python .py code file to automate the GUI components using SikuliX.
How Can I use Sikuli functionality in my python code?
Can i use python libraries like matplotlib, pandas etc. in Sikuli.script code?
Since Sikuli is written in Java and its "Python" version is not really Python but Jython, you need to seek support for specific Pythonic libraries through Jython documentation and forums. For example have a look at the below links in regards to Jython + matplotlib:
https://sourceforge.net/p/jython/mailman/message/31496842/
http://matplotlib.1069221.n5.nabble.com/jython-and-matplotlib-td9782.html
Most generic Python functionality and libraries are supported by Jython but unfortunately not all Python libraries can be used with it.
Having said that, you can have access to all Sikuli features using Java and can easily embed it into Java code.

Are there existing python bindings to the FlyCapture API?

I'm trying to write some code using OpenCV. My sensor is from Point Grey Systems, and it uses the FlyCapture API to grab images. I'd like to grab those images and do some stuff in OpenCV to them, using Python. FlyCapture is all C/C++, so I'm firing up SWIG to create the bindings. Am I reinventing the wheel? Do python bindings for FlyCapture already exist somewhere that I don't know about?
As of FlyCapture 2.11 there are official Point Grey / FLIR python wrappers. I've used the python 2.7 so far.
the pyflycapture2 is still available as well...
just started using this wrapper last week pyflycapture2
working well so far with my FireFly MV USB
Here's a new Cython wrapper I started for the FlyCapture V1 API:
https://github.com/kbrafford/pyfly1
It also has some wx.Python demos showing it working and allowing you to assess performance.
If the Python language is your only restriction, you may use the wrappers for FlyCapture from the JavaCPP Presets by calling the appropriate functions via Jython.

Is IronPython a 100% pure Python variant?

I just downloaded the original Python interpreter from Python's site. I just want to learn this language but to start with, I want to write Windows-based standalone applications that are powered by any RDBMS. I want to bundle it like any typical Windows setup.
I searched old posts on SO and found guys suggesting wxPython and py2exe. Apart from that few suggested IronPython since it is powered by .NET.
I want to know whether IronPython is a pure variant of Python or a modified variant. Secondly, what is the actual use of Python? Is it for PHP like thing or like C# (you can either program Windows-based app. or Web.).
IronPython isn't a variant of Python, it is Python. It's an implementation of the Python language based on the .NET framework. So, yes, it is pure Python.
IronPython is caught up to CPython (the implementation you're probably used to) 2.6, so some of the features/changes seen in Python 2.7 or 3.x will not be present in IronPython. Also, the standard library is a bit different (but what you lose is replaced by all that .NET has to offer).
The primary application of IronPython is to script .NET applications written in C# etc., but it can also be used as a standalone. IronPython can also be used to write web applications using the SilverLight framework.
If you need access to .NET features, use IronPython. If you're just trying to make a Windows executable, use py2exe.
Update
For writing basic RDBMS apps, just use CPython (original Python), it's more extensible and faster. Then, you can use a number of tools to make it stand alone on a Windows PC. For now, though, just worry about learning Python (those skills will mostly carry over to IronPython if you choose to switch) and writing your application.
IronPython is an independent Python implementation written in C# as opposed to the original implementation, often referred to as CPython due to it being written in (no surprise) C.
Python is multi-purpose - you can use it to write web apps (often using a framework such as Django or Pylons), GUI apps (as you've mentioned), command-line tools and as a scripting language embedded inside an app written in another language (for instance, the 3D modelling tool Blender can be scripted using Python).
what does "Pure Python" mean? If you're talking about implemented in Python in the same sense that a module may be pure python, then no, and no Python implementation is. If you mean "Compatible with cPython" then yes, code written to cPython will work in IronPython, with a few caveats. The one that's likely to matter most is that the libraries are different, for instance code depending on ctypes or Tkinter won't work. Another difference is that IronPython lags behind cPython by a bit. the very latest version of this writing is 2.6.1, with an Alpha version supporting a few of the 2.7 language features available too.
What do you really need? If you want to learn to program with python, and also want to produce code for windows, you can use IronPython for that, but you can also use cPython and py2exe; both will work equally well for this with only differences in the libraries.
IronPython is an implementation of Python using C#. It's just like the implementation of Python using Java by Jython. You might want to note that IronPython and Jython will always lag behind a little bit in development. However, you do get the benefit of having some libraries that's not available in the standard Python libraries. In IronPython, you will be able to get access to some of the .NET stuff, like System.Drawings and such, though by using these non-standard libraries, it will be harder to port your code to other platforms. For example, you will have to install mono to run apps written in IronPython on Linux (On windows you will need the .NET Framework)

Which version of python opencv should I go for?

Having successfully installed opencv 2.0 with python bindings I'm starting to run into trouble and before I go any further I wondered if I should change to another option. As ezod on this post says:
"As a caveat, as of the 2.0 release, the new Python bindings are incomplete: many functions I would consider rather important missing. Meanwhile, the SWIG bindings are nothing short of agonizing to work with. The ctypes-opencv bindings (3rd party project), as of version 0.8.0, do not support OpenCV 2.0."
So, should I soldier on with 2.0 or should I go for ctypes? What am I missing out on either way?
I'm using OSX, python 2.5 and wanting to do tracking in 2d of moving object and neither a python nor machine vision expert!
A late answer. If you do not have to depend on earlier versions, and want to use OpenCV with Python, choose the latest stable version. Today it is OpenCV 2.3.1.
The major benefit of OpenCV ≥ 2.3 for Python users: a new cv2 module in addition to the old (backwards compatible) cv module. New cv2 module is much more pythonic and doesn't require manual memory allocations for intermediate data structures. Old cv module is more like direct translation of the C++ API.
For example, compare the new Python cv2.findContours (OpenCV ≥ 2.3):
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
It requires only three parameters, and can handle all memory allocations automatically, returns only the final result. Just one line of the user code.
Vs. the old cv.FindContours:
FindContours(image, storage [, mode [, method [, offset]]]) -> None
It requires the user to explicitly allocate "storage" before the call (+ 1 or 2 lines of code). It doesn't return the result, instead it saves it in the allocated storage (it works like a linked list, and the user has to write some loop to actually extract the data out of storage). Overall, more low-level, and more like C++ than Python. At least 4-5 lines of code in the common use case, instead of just one line with new cv2 module.
I'm using a self-compiled OpenCV 2.0 and it's built-in python binding. Up to now I was missing 2 or 3 functions (e.g. FindFundamentalMat). If you get the source code of OpenCV you find a text file interfaces/python/api that defines the parameter and return types for each OpenCV function that is available from Python. Upon recompilation an automatic generator will parse this file and build the python extension. For all the cases I've been through I found that adding an appropriate definition to the api for the functions I needed, then recompiling opencv, worked quite well.
I'd recommend you use the official Python bindings to OpenCV 2.1 which as far as I've seen has feature parity with the C++ libraries. Most of them have either a pythonic wrapper, or a direct translation from the C++ version.
Python's OpenCV documentation isn't as complete as C++'s, but if you feel that the language advantages for prototyping are worth it, you'll be able to understand the Python usage from the C++ documentation.
Beware that much of the existing example code you'll find is from the previous versions and are incompatible (for example now everything resides under the cv package), but it's not hard to figure out how update it.

Categories