如何形容python 的 openopc库

Python09

如何形容python 的 openopc库,第1张

About OpenOPC

OpenOPC for Python is a free, open source OPC (OLE for Process Control) toolkit designed for use with the popular Python programming language. The unique features that set it apart from the many commercially available OPC toolkits include...

Easy to use

Because the OpenOPC library implements a minimal number of Python functions which may be chained together in a variety of ways, the library is simple to learn and easy to remember. In its simplest form, you can read and write OPC items as easily as any variable in your Python program... print opc['Square Waves.Int4'] opc['Square Waves.Real8'] = 100.0 Cross platform support

OpenOPC works with both Windows and non-Windows platforms. It has been tested with Windows, Linux, and Mac OS X. Functional programming style

OpenOPC allows OPC calls to be chained together in an elegant, functional programming style. For example, you can read the values of all items matching a wildcard pattern using a single line of Python code! opc.read(opc.list('Square Waves.*')) Designed for dynamic languages

Most OPC toolkits today are designed for use with static system languages (such as C++ or C#), providing a close mapping to the underlying Win32 COM methods. OpenOPC discards this cumbersome model and instead attempts to take advantage of the dynamic language features provided by Python.

OpenOPC is also one of the very few OPC-DA toolkits available for any dynamic language, and future support is planned for Ruby. EXAMPLE: Minimal working program

import OpenOPC

opc = OpenOPC.client()

opc.connect('Matrikon.OPC.Simulation')

print opc['Square Waves.Real8']

opc.close()

This project utilizes the de facto OPC-DA (Win32 COM-based) industrial automation standard. If you are looking for an OPC XML-DA library for Python, then please visit the PyOPC project.

OpenOPC Gateway Service

The Gateway Service is an optional Windows service which handles all the Win32 COM/DCOM calls used by the OpenOPC library module. This offers the following potential advantages:

Avoidance of DCOM security headaches and firewall issues when running the OPC client and OPC server on different nodes. The ability to write OPC client applications that can run on non-Windows platforms such as Linux, Unix, and Mac OS X. Installing the service

C:\OpenOPC\bin>OpenOPCService.exe -install

Installing service OpenOpcService

Service installed

Starting the service

C:\OpenOPC\bin>net start zzzOpenOpcService

Stopping the service

C:\OpenOPC\bin>net stop zzzOpenOpcService

NOTE: If you want to use the OpenOPC Gateway service from another machine as a client and are using OpenOPC 1.2 then you must set the environment variable in windows and restart the gateway service:

OPC_GATE_HOST=x.x.x.x

原文标题:10 Python image manipulation tools.

作者 | Parul Pandey

翻译 | 安其罗乔尔、JimmyHua

今天,在我们的世界里充满了数据,图像成为构成这些数据的重要组成部分。但无论是用于何种用途,这些图像都需要进行处理。图像处理就是分析和处理数字图像的过程,主要旨在提高其质量或从中提取一些信息,然后可以将其用于某种用途。

图像处理中的常见任务包括显示图像,基本操作如裁剪、翻转、旋转等,图像分割,分类和特征提取,图像恢复和图像识别。Python成为这种图像处理任务是一个恰当选择,这是因为它作为一种科学编程语言正在日益普及,并且在其生态系统中免费提供许多最先进的图像处理工具供大家使用。

让我们看一下可以用于图像处理任务中的常用 Python 库有哪些吧。

1.scikit-image

scikit-image是一个开源的Python包,适用于numpy数组。它实现了用于研究,教育和工业应用的算法和实用工具。即使是那些刚接触Python生态系统的人,它也是一个相当简单直接的库。此代码是由活跃的志愿者社区编写的,具有高质量和同行评审的性质。

资源

文档里记录了丰富的例子和实际用例,阅读下面的文档:

http://scikit-image.org/docs/stable/user_guide.html

用法

该包作为skimage导入,大多数功能都在子模块中找的到。下面列举一些skimage的例子:

图像过滤

使用match_template函数进行模板匹配

你可以通过此处查看图库找到更多示例。

2. Numpy

Numpy是Python编程的核心库之一,并为数组提供支持。图像本质上是包含数据点像素的标准Numpy数组。因此,我们可以通过使用基本的NumPy操作,例如切片、掩膜和花式索引,来修改图像的像素值。可以使用skimage加载图像并使用matplotlib显示图像。

资源

Numpy的官方文档页面提供了完整的资源和文档列表:

http://www.numpy.org/

用法

使用Numpy来掩膜图像.

3.Scipy

scipy是Python的另一个类似Numpy的核心科学模块,可用于基本的图像操作和处理任务。特别是子模块scipy.ndimage,提供了在n维NumPy数组上操作的函数。该包目前包括线性和非线性滤波,二值形态学,B样条插值和对象测量等功能函数。

资源

有关scipy.ndimage包提供的完整功能列表,请参阅下面的链接:

https://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html#correlation-and-convolution

用法

使用SciPy通过高斯滤波器进行模糊:

4. PIL/ Pillow

PIL( Python图像库 )是Python编程语言的一个免费库,它支持打开、操作和保存许多不同的文件格式的图像。然而, 随着2009年的最后一次发布,它的开发停滞不前。但幸运的是还有有Pillow,一个PIL积极开发的且更容易安装的分支,它能运行在所有主要的操作系统,并支持Python3。这个库包含了基本的图像处理功能,包括点运算、使用一组内置卷积核的滤波和色彩空间的转换。

资源

文档中有安装说明,以及涵盖库的每个模块的示例:

https://pillow.readthedocs.io/en/3.1.x/index.html

用法

在 Pillow 中使用 ImageFilter 增强图像:

5. OpenCV-Python

OpenCV( 开源计算机视觉库 )是计算机视觉应用中应用最广泛的库之一 。OpenCV-Python 是OpenCV的python版API。OpenCV-Python的优点不只有高效,这源于它的内部组成是用C/C++编写的,而且它还容易编写和部署(因为前端是用Python包装的)。这使得它成为执行计算密集型计算机视觉程序的一个很好的选择。

资源

OpenCV-Python-Guide指南可以让你使用OpenCV-Python更容易:

https://github.com/abidrahmank/OpenCV2-Python-Tutorials

用法

下面是一个例子,展示了OpenCV-Python使用金字塔方法创建一个名为“Orapple”的新水果图像融合的功能。

6. SimpleCV

SimpleCV 也是一个用于构建计算机视觉应用程序的开源框架。有了它,你就可以访问几个高性能的计算机视觉库,如OpenCV,而且不需要先学习了解位深度、文件格式、颜色空间等。

它的学习曲线大大小于OpenCV,正如它们的口号所说“计算机视觉变得简单”。一些支持SimpleCV的观点有:

即使是初学者也可以编写简单的机器视觉测试摄像机、视频文件、图像和视频流都是可互操作的资源

官方文档非常容易理解,而且有大量的例子和使用案例去学习:

https://simplecv.readthedocs.io/en/latest/

用法

7. Mahotas

Mahotas 是另一个计算机视觉和图像处理的Python库。它包括了传统的图像处理功能例如滤波和形态学操作以及更现代的计算机视觉功能用于特征计算,包括兴趣点检测和局部描述符。该接口是Python语言,适合于快速开发,但是算法是用C语言实现的,并根据速度进行了调优。Mahotas库速度快,代码简洁,甚至具有最小的依赖性。通过原文阅读它们的官方论文以获得更多的了解。

资源

文档包括安装指导,例子,以及一些教程,可以更好的帮助你开始使用mahotas。

https://mahotas.readthedocs.io/en/latest/install.html

用法

Mahotas库依赖于使用简单的代码来完成任务。关于‘Finding Wally’的问题,Mahotas做的很好并且代码量很少。下面是源码:

https://mahotas.readthedocs.io/en/latest/wally.html

8. SimpleITK

ITK 或者 Insight Segmentation and Registration Toolkit是一个开源的跨平台系统,为开发人员提供了一套广泛的图像分析软件工具 。其中, SimpleITK是建立在ITK之上的简化层,旨在促进其在快速原型设计、教育、解释语言中的应用。SimpleITK 是一个图像分析工具包,包含大量支持一般过滤操作、图像分割和匹配的组件。SimpleITK本身是用C++写的,但是对于包括Python以内的大部分编程语言都是可用的。

资源

大量的Jupyter Notebooks 表明了SimpleITK在教育和研究领域已经被使用。Notebook展示了用Python和R编程语言使用SimpleITK来进行交互式图像分析。

http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/

用法

下面的动画是用SimpleITK和Python创建的刚性CT/MR匹配过程的可视化 。点击此处可查看源码!

9. pgmagick

pgmagick是GraphicsMagick库的一个基于python的包装。 GraphicsMagick图像处理系统有时被称为图像处理的瑞士军刀。它提供了一个具有强大且高效的工具和库集合,支持以88种主要格式(包括重要格式,如DPX、GIF、JPEG、JPEG-2000、PNG、PDF、PNM和TIFF)读取、写入和操作图像。

资源

有一个专门用于PgMagick的Github库 ,其中包含安装和需求说明。还有关于这个的一个详细的用户指导:

https://github.com/hhatto/pgmagick

用法

使用pgmagick可以进行的图像处理活动很少,比如:

图像缩放

边缘提取

10. Pycairo

Pycairo是图像处理库cairo的一组Python捆绑。Cairo是一个用于绘制矢量图形的2D图形库。矢量图形很有趣,因为它们在调整大小或转换时不会失去清晰度 。Pycairo是cairo的一组绑定,可用于从Python调用cairo命令。

资源

Pycairo的GitHub库是一个很好的资源,有关于安装和使用的详细说明。还有一个入门指南,其中有一个关于Pycairo的简短教程。

库:https://github.com/pygobject/pycairo指南:https://pycairo.readthedocs.io/en/latest/tutorial.html用法

使用Pycairo绘制线条、基本形状和径向梯度:

总结

有一些有用且免费的Python图像处理库可以使用,有的是众所周知的,有的可能对你来说是新的,试着多去了解它们。

准备

我测试使用的Python版本为2.7.10,如果你的版本是Python3.5的话,这里就不太适合了。

使用Speech API

原理

我们的想法是借助微软的语音接口,所以我们肯定是要进行调用 相关的接口。所以我们需要安装pywin32来帮助我们完成这一个底层的交互。

示例代码

import win32com.clientspeaker = win32com.client.Dispatch("SAPI.SpVoice")speaker.Speak("Hello, it works!")

小总结

是的,调用接口来实现语音功能就是这么简单,但是我们不得不来聊一聊这种方式的缺点。

对中文支持的不够好,仅仅是这一点,估计在中国没几个用它的了。

还有就是语速不能很好的控制

pyttsx方式

原理

pyttsx 是Python的一个关于文字转语音方面的很不错的库。我们还可以借助pyttsx来实现在线朗读rfc文件或者本地文件等等,最为关键的是,它对中文支持的还是不错的。

示例代码

# coding:utf-8import sysreload(sys)sys.setdefaultencoding('utf8')# __author__ = '郭 璞'# __date__ = '2016/8/6'# __Desc__ = 文字转语音输出import pyttsxengine = pyttsx.init()engine.say('hello world')engine.say('你好,郭璞')engine.runAndWait()# 朗读一次engine.endLoop()

小总结

使用pyttsx,我们可以借助其强大的API来实现我们基本的业务需求。很酷吧。

pyttsx深入研究

做完上面的小实验,你肯定会觉得怎么这么不过瘾呢?

别担心,下面我们就一起走进pyttsx的世界,深入的研究一下其工作原理吧。

语音引擎工厂

类似于设计模式中的“工厂模式”,pyttsx通过初始化来获取语音引擎。当我们第一次调用init操作的时候,会返回一个pyttsx的engine对象,再次调用的时候,如果存在engine对象实例,就会使用现有的,否则再重新创建一个。

pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine

从方法声明上来看,第一个参数指定的是语音驱动的名称,这个在底层适合操作系统密切相关的。如下:

1.drivename:由pyttsx.driver模块根据操作系统类型来调用,默认使用当前操作系统可以使用的最好的驱动

sapi5 - SAPI5 on Windows

nsss - NSSpeechSynthesizer on Mac OS X

espeak - eSpeak on every other platform

2.debug: 这第二个参数是指定要不要以调试状态输出,建议开发阶段设置为True

引擎接口

要想很好的运用一个库,不了解其API是不行的。下面来看看pyttsx。engine.Engine的引擎API。

方法签名参数列表返回值简单释义

connect(topic : string, cb : callable)topic:要描述的事件名称;cb:回调函数→ dict在给定的topic上添加回调通知

disconnect(token : dict)token:回调失联的返回标记Void结束连接

endLoop()None→ None简单来说就是结束事件循环

getProperty(name : string)name有这些枚举值“rate, vioce,vioces,volumn→ object获取当前引擎实例的属性值

setProperty(name : string)name有这些枚举值“rate, vioce,vioces,volumn→ object设置当前引擎实例的属性值

say(text : unicode, name : string)text:要进行朗读的文本数据name: 关联发音人,一般用不到→ None预设要朗读的文本数据,这也是“万事俱备,只欠东风”中的“万事俱备”

runAndWait()None→ None这个方法就是“东风”了。当事件队列中事件全部清空的时候返回

startLoop([useDriverLoop : bool])useDriverLoop:是否启用驱动循环→ None开启事件队列

元数据音调

在pyttsx.voice.Voice中,处理合成器的发音。

age

发音人的年龄,默认为None

gender

以字符串为类型的发音人性别: male, female, or neutral.默认为None

id

关于Voice的字符串确认信息. 通过 pyttsx.engine.Engine.setPropertyValue()来设置活动发音签名. 这个属性总是被定义。

languages

发音支持的语言列表,如果没有,则为一个空的列表。

name

发音人名称,默认为None.

更多测试

朗读文本

import pyttsxengine = pyttsx.init()engine.say('Sally sells seashells by the seashore.')engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

事件监听

import pyttsxdef onStart(name): print 'starting', namedef onWord(name, location, length): print 'word', name, location, lengthdef onEnd(name, completed): print 'finishing', name, completedengine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

打断发音

import pyttsxdef onWord(name, location, length): print 'word', name, location, length if location >10: engine.stop()engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

更换发音人声音

engine = pyttsx.init()voices = engine.getProperty('voices')for voice in voices: engine.setProperty('voice', voice.id) engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

语速控制

engine = pyttsx.init()rate = engine.getProperty('rate')engine.setProperty('rate', rate+50)engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

音量控制

engine = pyttsx.init()volume = engine.getProperty('volume')engine.setProperty('volume', volume-0.25)engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

执行一个事件驱动循环

engine = pyttsx.init()def onStart(name): print 'starting', namedef onWord(name, location, length): print 'word', name, location, lengthdef onEnd(name, completed): print 'finishing', name, completed if name == 'fox': engine.say('What a lazy dog!', 'dog') elif name == 'dog': engine.endLoop()engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.', 'fox')engine.startLoop()

使用一个外部的驱动循环

engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.', 'fox')engin(www.alOnely.Com.Cn)e.startLoop(False)# engine.iterate() must be called inside externalLoop()externalLoop()engine.endLoop()

总结

以上就是Python如何实现文本转语音的全部内容,看完了上面的讲述,是不是感觉Python实现文本转语音还是蛮简单的?那么,大家快来尝试尝试吧。希望本文对大家学习Python有所帮助。