Advantage of using Python

Advantages of Using Python for Computer Vision

Python is a programming language that aims for both new and experienced programmers to be able to convert ideas into code easily. It is currently the most prevalent, mature, and supported among programming languages in the area of machine learning, which is why many developers use Python for Computer Vision (CV).

Computer Vision, on the other hand, allows computers to identify objects through digital images or videos. Implementing CV through Python allows developers to automate tasks that involve visualization with much more ease.

While there are other programming languages that support Computer Vision, Python dominates the competition.

Here are the reasons why:

  • Ease of coding
    • “Code as plain English” is Python’s primary goal. This allows programmers to focus on the design and not on coding. This is perfect for those who are just getting their feet wet with machine learning or programming itself. This advantage is very beneficial, especially when faced with complex scenarios.
  • Fast prototyping
    • Since you can focus more on the design, you can now experiment with more designs ideas. Python is well-suited for implementing new features. Libraries like OpenCV are written in C++ and make Python have slower runtime as it will still call C/C++ libraries. This means you will have the development advantage from Python while you can have performance optimization from C++.
  • Vast libraries for machine learning
    • Python has been most commonly used for machine learning. Even data scientists can invest their time contributing since it’s easy to code and it’s free. CV developers don’t have to worry much about projects that they’re working on since most of their cases are already covered by Python libraries.
  • It is open source
    • Unlike MATLAB which specializes in data analysis, exploration, visualization, etc. but is not free, you can get started with Python without having to pay. Needless to say, for Python, all you need is a computer and you are good to go. You can even deploy your work for free on sites like pythonanywhere.
  • It can be directly integrated with web frameworks
    • Mature web frameworks (like Django) which aim fast development time, neat and realistic design are also available in Python. Python even has micro frameworks that are just as functional as their larger counterparts.
  • Most commonly used
    • This means it has a bigger community. There are a lot of blog posts and online resources regarding Python + OpenCV so you won’t get stuck for most of the time trying to fix a problem all by yourself.

How about other options?

We are not limiting ourselves only to Python for CV support. There is already CV support for other major programming languages like php-opencv for PHP, ruby-opencv for Ruby, common-cv for CommonLisp, gocv for Go, cv-rs for Rust and EmguCV for C#. Always remember that there is no single programming language that is the best for CV. It will always depend on the project to be built and what programming language you are more comfortable with.

Here’s a simple face detector using Python, C++, and Java. The code uses the same method with three popular programming languages so that we can compare the differences.

Python code:

import cv2

face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)

image = cv2.imread(‘me.jpg’)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h)  in faces:
   Image = cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

cv2.imshow(‘Me’, image)

In the code above, the library cv2 needs to be imported for image processing in Python. In order to identify faces in an image, a face classifier needs to be loaded. OpenCV expects a grayscale image so the original image has been converted to gray in line  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY). From the grayscale image, the program detects the faces that are based on the declared classifier face_cascade = v2.CascadeClassifier(‘haarcascade_frontalface_default.xml’). Since the faces are detected, a for loop is implemented to draw a rectangle on the original image to identify and label the key points in the image. The lines cv2.waitKey and cv2.destroyAllWindows allows the user to press a key before exiting the program.

Here’s an output image using OpenCV in Python.

The picture shows the face detection result with a box highlighting the face detected in the photo

As you can see, it correctly identified which part is a face and which is not despite the colorful background. Go and try out the codes provided above, though you might need to redeclare the classifiers like importing with the absolute file path rather than the relative path. And now here are the code snippets for the other programming languages:


#include “opencv2/objdetect.hpp”
#include “opencv2/highgui.hpp”
#include “opencv2/imgproc.hpp”
#include <iostream>

using namespace std;
using namespace cv;

int main()

   image = imread(‘me.jpg’, CV_LOAD_IMAGE_COLOR);
   Mat gray;
   cvtColor(image, gray, COLOR_BGR2GRAY);

   vector<Rect> faces;
   faceCascade.detectMultiScale(gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30));

   for (size_t i = 0; i < faces.size(); i++) {
       Rect r = faces[i];
               cvRound(r.x * scale),
               cvRound(r.y * scale)
               cvRound((r.x + r.width - 1) * scale),
               cvRound((r.y + r.height) - 1) * scale)
           Scalar(255, 0, 0), 3, 8, 0);

   imshow(“Me”, image);

   return 0;

In the C++ code above, we first imported different components from the opencv2 library for image processing and also imported iostream to enable read and write to input/output stream. Namespaces are then declared which can be used for scoping, allowing us to code “std::cout” to only “cout”. The primary code is then placed inside int main function as the main interface. Then, as an identifier is needed to feed on the image processing, a face classifier is loaded. Then grab the image and convert to grayscale since OpenCV expects grayscale images. All the requirements are now met. Next is to feed the image to the image processor with the face classifier declared. As the faces are detected, a for loop is implemented to draw a rectangle on the original image to identify and label the key points in the image. The original image is then displayed with identified faces within it. return 0; is then used to signify that the program worked fine.


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class FaceDetector {
   public static void main(String[] args) {

       CascadeClassifier faceCascade = new CascadeClassifier(

       Mat image = Highgui.imread(FaceDetector.class.getResource(‘me.jpg’).getPath());

       MatOfRect faceDetections = new MatOfRect();
       faceCascade.detectMultiScale(image, faceDetections);

       for (Rect rect: faceDetections.toAray()) {
               new Point(rect.x, rect.y),
               new Point(rect.x + rect.width, rect.y + rect.height),
               new Scalar(255, 0, 0)

       Highgui.imwrite(‘me.jpg’, image);

In the Java code above, we imported different components from OpenCV library such as Core to call in the native OpenCV library, Mat and MatOfRect for storing into Matrix objects, Point for 2D points from a given x and y coordinates, Rect for 2D rectangles, Scalar for 4-element vector, Highgui to read/write image and then CascadeClassifier for loading our face classifier. A class with function main is needed for a Java program to compile smoothly or else we’ll encounter unnecessary errors, so we added one. The Core will then call the native OpenCV library with the line System.loadLibrary(Core.NATIVE_LIBRARY_NAME);. We also loaded in the face classifier since we need it to feed to the image processing. The face classifier will then detect the faces inside the image and put them in the faceDetections matrix variable. Now that we have the faces identified, we can then loop through them and draw a rectangle on the original image to identify and label the key points in the image. Highgui can then write the image with identified faces to the file named `me.jpg`.

Listen to this episode


All codes shown above have the same goal and achieve it using the same methods. And if you just noticed, the Python code is shorter and more straightforward than the other programming languages’. Though there is really nothing special about the programming language, the essence is how you use it and for what purpose, since each programming language places emphasis on its particular goals. Python for ease of coding and C++ for fast execution.

Here at Full Scale – one of the leading offshore services companies in Cebu City, Philippines, we explore these advantages and measure their limits to determine and use the best option in our applications. Full Scale provides the best talents in the field of software development. These talents include, but are not limited to, Python, PHP, Java, C#, .NET and Mobile Development.

Contact us now to start building your team!