我有这个问题,我尝试在python上导入cv2并获取以下错误消息.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: %1 is not a valid Win32 application.
Run Code Online (Sandbox Code Playgroud)
我知道有很多关于这个的帖子,建议包的位数与python包不同.
但是,我运行的所有内容都是64位.我在win7 64位,我有winpython 2.7.3.3,64位分配,我用64位编译opencv,用这里提供的指令将cv2.pyd dll放在python的Lib/site-packages文件夹中.
不幸的是,使用32位版本的python的建议对我来说不再适用,因为我必须处理32位以上的numpy数组.
谢谢!!!
------更新
唯一缺少的是将新的opencv二进制路径(C:\ opencv\build\bin\Release)添加到Windows PATH环境变量,重新启动python.
现在一切似乎都很好!
我正在尝试制作一个将属性添加到类的方法的python装饰器,以便我可以从方法本身内部访问和修改这些属性。装饰器代码是
from types import MethodType
class attribute(object):
def __init__(self, **attributes):
self.attributes = attributes
def __call__(self, function):
class override(object):
def __init__(self, function, attributes):
self.__function = function
for att in attributes:
setattr(self, att, attributes[att])
def __call__(self, *args, **kwargs):
return self.__function(*args, **kwargs)
def __get__(self, instance, owner):
return MethodType(self, instance, owner)
retval = override(function, self.attributes)
return retval
Run Code Online (Sandbox Code Playgroud)
我在下面的玩具示例中尝试了此装饰器。
class bar(object):
@attribute(a=2)
def foo(self):
print self.foo.a
self.foo.a = 1
Run Code Online (Sandbox Code Playgroud)
尽管我可以从foo()中访问属性'a'的值,但无法将其设置为另一个值。 确实,当我调用时bar().foo(),我得到以下AttributeError。
AttributeError: 'instancemethod' object has no attribute 'a'
Run Code Online (Sandbox Code Playgroud)
为什么是这样?更重要的是,我如何实现自己的目标?
编辑
更具体地说,我试图找到一种简单的方法来实现位于类方法内的静态变量。从上面的示例继续,我想实例化b = …