Today I found an interesting post: Face Detection in Static Images with Python. You give an image as input and the program draws a rectangle around the face(s) on the image.
sudo apt-get install python-opencv libcv-dev opencv-doc
Under Ubuntu 11.04, the required XML is in the
opencv-doc package at a different location, thus you need to modify the source code:
... cascade = cvLoadHaarClassifierCascade( '/usr/share/doc/opencv-doc/examples/haarcascades/haarcascades/haarcascade_frontalface_default.xml.gz', cvSize(1,1)) ...
See the full (patched) source code here.
You can draw a rectangle on an image the following way:
convert original.jpg -stroke red -fill none -draw "rectangle 50,36 115,101" output.jpg
Where 50,36 is the top left, 115,101 is the bottom right coordinates.
/ @reddit /
Today I came across the funny antigravity cartoon at xkcd again. I said to myself: “Well, let’s give it a try.” I was amazed to see that it really works! Try it yourself:
$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import antigravity >>>
These guys were able to put it in the standard library :) Python is really fun, I discover that every day.
I won’t link the image, you must try it yourself.
You have a source file that you want to work with but it mixes tabs and spaces. The interpreter eats the file but it’s a nightmare to work with. How to correct it?
Fortunately, the standard installation comes with a handy utility called “
reindent.py“. You can find it with “
It will make a backup (
bad.py.bak) of the original file and overwrite
bad.py. The new
bad.py will be indented correctly with 4 spaces everywhere.
If you don’t find
reindent.py, it is located here:
You might have to install the package “python-examples” to have it.
The atexit module defines a single function to register cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination.
I tried today SQLite and I noticed that commit() must be called explicitly, it’s not called automatically when the program terminates. I thought that when the connection object goes out of scope, it calls commit and close, but no…
So here is my solution (extract):
SQLITE_DB = None conn = None def init(sqlite_db): """Initialize the DB.""" global SQLITE_DB, conn atexit.register(commit_and_close) # HERE SQLITE_DB = sqlite_db if not os.path.exists(SQLITE_DB): create_db() if not conn: conn = sqlite3.connect(SQLITE_DB) def commit_and_close(): """Commit and close DB connection.""" if conn: conn.commit() conn.close()
You can find the full source code here.
If you want a tuned up Eclipse-Pydev, try Aptana Studio.
- Download Eclipse. You can get the IDE for Java Developers.
- Add the plugin PyDev using the repository http://pydev.org/updates.
- Since the new Ubuntu 11.04 uses Python 2.7 by default, you can tell Eclipse too to use it instead of version 2.6. Go to Window -> Preferences, then Pydev -> Interpreter – Python. Remove Python 2.6 and add Python 2.7.
Now if you want to create a new Python project, choose New -> Pydev Project.
Pydev has lots of cool features. My favorites:
- code completion (CTRL + Space)
- Rename (Shift + Alt + R), which works through projects too! Say, you have two projects, A and B. A is a library that is used by B. If you reference A from B correctly then if you rename something in A, PyDev will rename the necessary references in B too. I find it very useful when doing refactoring and trying to find better names for functions/variables.
- debugger (F11)
- jump around (hold CTRL and left click with your mouse on a function name to jump to the function’s definition)
With PyDev, I get “unresolved import” errors quite often, even if the import is correct and the script runs fine. Here I found some tips how to get rid of them:
- Click Ctrl + 1 and select “Unresolved import error”. This will hide the error.
- Under Preferences -> PyDev -> Editor -> Code Analysis, go to the Imports tab. Change Import redefinition from “Error” to “Warning”. I don’t recommend “Ignore” because it will hide the real errors too.
For more tips, refer to this post.
In C, the ternary operator looks like this:
condition ? value_if_true : value_if_false
In Python, it’s a bit different:
value_when_true if condition else value_when_false
>>> name = None >>> msg = "Hello %s" % name if name else 'gimme your name' >>> msg 'gimme your name' >>> name = 'Jabba' >>> msg = "Hello %s" % name if name else 'gimme your name' >>> msg 'Hello Jabba'
I’m just reading the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (see the reviews here). The code examples are in Java but the tips are general and thus they can be applied to Python too. If you want to learn how to write nice code, how to do refactoring, read this book.