Javascript – How to use the OpenCV python library using brython

How to use the OpenCV python library using brython… here is a solution to the problem.

How to use the OpenCV python library using brython

I have a fully functional Python project that has been tested directly through the terminal.

But when I searched for how to integrate Python code with JavaScript wrappers, I found few open-source APIs

http://www.brython.info/
http://www.skulpt.org/
http://pypyjs.org/
http://transcrypt.org/
http://stromberg.dnsalias.org/~strombrg/pybrowser/python-browser.html

I’ve used Brython and was able to run some basic Python code

But my python project contains some import statements for libraries that I installed directly into the operating system
Use

apt-get install python-opencv
apt-get install python-numpy
apt-get install python-skimage
apt-get install cython
pip install --user imutils
pip install --user scikit-image

And the project contains import statements as

import imutils
from skimage.filter import threshold_adaptive
import numpy as np
import argparse
import cv2

My goal was to be able to run my python code using a JavaScript wrapper on a Cordova project, but since these libraries were not available by default, I tried downloading them and importing via a local path, but then I started facing many problems that the import errors and console errors thrown by brython were incomprehensible. I’m trying to build image processing software using Python running on Cordova Android and iOS.

Any help would be appreciated.
Thanks in advance

Solution

Brython is a transpiler for Python code

While it works well with Python 3.5 implementations, it must never run native code. That is: projects that use native code, such as openCV, etc., cannot run with Brython – because it does not “convert” native x86 binaries to compatible JavaScript objects, as it does with Python source code.

Other methods like “skulpt” work differently: they compile C code from the Python runtime itself into javascript – and they can make them work if they have the option to compile binary Python modules alongside the Python runtime. (I’m not familiar with Skulpt or other client-side Python methods and don’t know if this is at least doable).

But even if it works, openCV takes full advantage of modern CPUs, including SIMD instructions, and perhaps even GPUs. All of this will be simulated in Javascript (if it works), with a performance penalty of 3 or 4 orders of magnitude — not to mention the full modification condition of file I/O (like: non-existent — you might get away with HTTP requests and HTML local storage if there are any file I/O side effects of your calls without code).

There is some effort in running native code (nacl) and other methods in the browser, but I don’t know the state of these, or if these use the cPython runtime on the browser.

So what is possible?

Put the image processing code on the server side and build a backend that allows you to call RPC functions on the server from code on the Brython side.
You just need to expose the required openCV functionality in HTTP View, using common Python frameworks such as Flask or Pyramid – setting up code to transfer image data and manipulate metadata between the browser and this server is not a difficult task using Brython.

Another side note:

Brython does not allow you to import arbitrary files that are already installed on your system – while it may work for any pure Python3 code that does not perform I/O, the files must be placed in a specific path, served over HTTP, so that Brython’s import machine can fetch them

Related Problems and Solutions