如何使用brython使用OpenCV python库 [英] How to work with OpenCV python library using brython

查看:500
本文介绍了如何使用brython使用OpenCV python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能齐全的Python项目,已经通过终端直接测试过。

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

但是当我搜索如何将Python代码与JavaScript包装器集成时,我发现一些开源API

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

我用过brython&能够运行一些基本的python代码

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

但我的python项目包含一些库的导入语句,我已经使用

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

项目包含导入语句

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

我的目标是能够使用Cordova项目上的JavaScript包装器运行我的python代码,但由于默认情况下这些库不可用,我尝试下载它们&通过本地路径导入,但后来我开始面临许多导入错误& brython抛出的控制台错误是不可理解的。我正在尝试使用在Cordova Android& iOS上运行的Python构建图像处理软件。

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

推荐答案

Brython是Python代码的转换器 -

Brython is a transpiler for Python code -

虽然它很好地符合Python 3.5的实现,但它绝不能运行本机代码。也就是说:使用本机代码的项目,比如openCV,以及许多其他项目都无法与Brython一起运行 - 因为它不会将原生x86二进制文件转换为兼容的javascript对象,就像它使用Python源代码一样。

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.

像skulpt这样的其他方法不同地工作:他们将Python运行时本身的C代码编译成javascript - 如果他们有选项可以编译二进制Python模块和Python运行时,它们可以工作。 (我不熟悉Skulpt或其他客户端Python方法,以了解这是否至少是可行的。)

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).

但即使它有效,openCV也需要很多现代CPU的优势,包括指令SIMD,甚至可能使用GPU。所有这些都将在Javascript中模拟(如果它可以工作),带来3或4个数量级的性能损失 - 更不用说文件I / O的完全修改条件(如:不存在 - 如果没有代码你调用有任何文件I / O副作用,你可以逃脱HTTP请求和HTML本地存储。)

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).

在运行本机代码方面有一些努力在浏览器(nacl)和其他方法,但我不知道这些的状态,或者这些在浏览器上使用cPython运行时。

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.

让您的图像处理代码服务器端,并构建一个后端,允许您从Brython端的代码调用服务器上的RPC函数。

您只需使用常见的Python框架(如Flask或Pyramid)在HTTP视图中公开所需的openCV功能 - 设置代码以在浏览器和此服务器之间传输图像数据和操作元数据将不会很难使用Brython。

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.

另一个注意事项:

Brython不允许你导入任意文件你已经安装在你的系统上 - 虽然它可能适用于任何不执行I / O的纯Python3代码,但文件必须放在特定的路径中,通过HTTP提供,这样Brython的导入机制就可以了他们

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

这篇关于如何使用brython使用OpenCV python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆