How to work with OpenCV python library using brython

只谈情不闲聊 提交于 2019-12-10 10:13:12

问题


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

But when I searched on how to integrate Python code with a JavaScript wrapper, I found few of the open-source API's

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

I have used brython & was able to run few basic python codes

But my python project contain some import statements of libraries which I have installed directly into the OS using

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 project contains import statements as

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

My Aim is to be able to run my python code using the JavaScript wrapper on a Cordova project, but since these libraries will not be available by default, I tried downloading them & importing through local paths, but then I started facing many import errors & console errors thrown by brython is not understandable. I'm trying to build image processing software build using Python running on Cordova Android&iOS.

Any help would be appreciated. Thanks in Advance


回答1:


Brython is a transpiler for Python code -

While it is nicely compliant to Python 3.5 implementation, it can, in no way, run native code. That is: projects that use native code, like openCV, among many others, can't run with Brython - as it does not "transpile" native x86 binaries into a compatible javascript object, like what it does with Python source code.

Other approaches like "skulpt" work diffrently: they compile the C code of the Python runtime itself into javascript - and if they have an option to compile binary Python modules along with the Python runtime, they could be made to work. (I am not familiar with Skulpt or the others client-side Python approaches to know if this is at least possible).

But even if it works, openCV take a lot of advantage of modern CPUs, inclding instructions SIMD, and likely even using the GPU. All of that would be emulated in Javascript (if it worked at all), bringing a performance loss of 3 or 4 orders of magnitude - not to mention the completly modified conditions for file I/O (as in: non existent - if no code you call has any file I/O side effects, you may get away with HTTP requests and HTML local storage).

There are efforts in the sense of running native-code on the Browser (nacl ) and other approaches, but I am not aware of the state of these, or these usng the cPython runtime on the browser.

What is possible then?

Have your image-dealing code server side, and build a backend that allows you to call RPC functions on the server, from code on the Brython side.
You just have to expose the desired openCV functionality in HTTP Views, using a common Python framework like Flask or Pyramid - setting up the code to transfer image data and operation meta-data between the browser and this server won't be a hard thing to do using Brython.

Another side note:

Brython won't allow you to import arbitrary files you've installed on your system - while it will likely work with any pure-Python3 code which does not perform I/O , the files have to be placed in specific paths, that are served through HTTP, so that Brython's import machinery can get them



来源:https://stackoverflow.com/questions/42204667/how-to-work-with-opencv-python-library-using-brython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!