Spent sometime to learn Python for setting up a Django web application framework. Summarize some useful tips for Python programming as well as share a modified program for feature-based homography tracking using OpenCV python wrapper (the sample codes come with OpenCV source has some bugs that cannot be executed)
Tips for Python Programming
1. Useful basic data types - list, tuple and dictionary
list - each one in the list is numbered, starting from zero. You can remove values from the list, and add new values to the end.
tuple - just like lists, but you can’t change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero.
dictionary - similar to what their name suggests. In a dictionary, you have an ‘index’ of words, and for each of them a definition.
2. List Comprehensions
A list comprehension is a construct consisting of logic that builds a list containing the values/objects generated by the logic.
e.g. Let’s say we have a list containing the integers 0 through 9. What if we wanted to increment each number and get all the results back in a list? With list comprehension:
In computer programming with object-oriented programming languages, duck-typing is a style of dynamic typing in which an object’s methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.
4. Functions are First-Class Objects
In Python, you can treat functions as any other object, such as, store them in container, assign them to different variables, pass them in as arguments to functions, and so forth. The only difference is you can execute function objects, meaning you treat them as a function by appending the usual parentheses and arguments.
This is tricky. If we had set default to datetime.date.today(), the function would be called at the time the model was defined, which is not what we want. Instead, we pass the function object; Django is aware of this and calls the function at instance-creation time to generate the value for us.
5. *args and **kwargs
In function calls, * means to unpack a tuple and ** means to unpack a dictionary. In function signature, define variable arguments(varargs) using * representing a “tuple” and ** a “dictionary” as a “shopping bag”.
To reinstall ubuntu, opencv, cuda is always a struggle, but also a progress to learn and know better about all of these. Now working in ubuntu 12.04, opencv 220.127.116.11 and cuda 5.5. First time to try OpenCV python wrapper, and to be surprised by its efficiency and the ease to program. However there are some bugs in the official release.
The Bug is noted in the source (RectSelector Class in common.py), however I don’t quite understand why it happens. Instead of using the class, I rewrite a PlanerTracker Class to perform the tracking by mouse clicking.
The source is uploaded here.