您的位置:首页 > 编程语言 > Qt开发

PyQt使用中遇到的若干问题(2)(事件触发,QImage对象转化,pyinstaller生成exe)

2016-12-06 20:59 639 查看
这篇是接着上面那篇继续记录一下,之后我在绑定按钮QPushButton的时候发现一个问题,为什么直接重载clicked事件的话没有反应,但是如果self.clicked.connect来绑定事件就可以,这个有知道的麻烦留言告诉我下~

后来的步骤需要将QImage对象的转化为ndarray对象放入Keras模型中,但是一开始并不知道如何把QImage对象转化为ndarry对象,后来上网查了之后发现了一个qimage2numpy方法,可以直接用,上代码:

def qimage2numpy(qimage, dtype='array'):
"""Convert QImage to numpy.ndarray.  The dtype defaults to uint8
for QImage.Format_Indexed8 or `bgra_dtype` (i.e. a record array)
for 32bit color images.  You can pass a different dtype to use, or
'array' to get a 3D uint8 array for color images."""
result_shape = (qimage.height(), qimage.width())
temp_shape = (qimage.height(),
qimage.bytesPerLine() * 8 / qimage.depth())
if qimage.format() in (QtGui.QImage.Format_ARGB32_Premultiplied,
QtGui.QImage.Format_ARGB32,
QtGui.QImage.Format_RGB32):
if dtype == 'rec':
dtype = QtGui.bgra_dtype
elif dtype == 'array':
dtype = np.uint8
result_shape += (4,)
temp_shape += (4,)
elif qimage.format() == QtGui.QImage.Format_Indexed8:
dtype = np.uint8
else:
raise ValueError("qimage2numpy only supports 32bit and 8bit images")
# FIXME: raise error if alignment does not match
buf = qimage.bits().asstring(qimage.numBytes())
result = np.frombuffer(buf, dtype).reshape(temp_shape)
if result_shape != temp_shape:
result = result[:, :result_shape[1]]
if qimage.format() == QtGui.QImage.Format_RGB32 and dtype == np.uint8:
result = result[..., :3]
result = result[:,:,::-1]
return result


方法来自这篇博客戳我,博客中的代码是没有最后result = result[:,:,::-1]这一行的,这是我发现返回的result的结果是按BGR三通道顺序的,所有需要转一下,之后就可以直接用了。

之后代码基本上计算完成了,给我的一个感觉是将keras嵌入pyqt中在cpu上程序运行的并不算快,不过也不算很慢。。。。接下来我打算把文件打包做成exe来方便展示,在py2exe和pyinstaller中,我果断选择了后者,首先py2exe太老了,而且效果也一般,不过我发现网上很多人都被pyinstaller的bug被打败了。。所以大致说下我的坎坷之路。。。

首先。。。。。release版和github上的那个貌似都不行。。。。会报那个啥can not script pyi_rth_pkgres的错误对不对!坑死爹了。看下面图。。。去官网下这个。。。



之后直接python setup.py build和python setup.py install就好了,但是很多人还是会报不能运行自己程序的错误,这其实有个debug的方法,我使用代码跑的:

if __name__ == '__main__':
from PyInstaller.__main__ import run
opts=['Face_Retrieval_Demo.py','-d','--icon=demo_128.ico']
run(opts)


这个‘-d’是调试模型,你大概看一下报的错是啥,如果是no module named **,你就在自己代码开头import上就好了(注:不是上面这段代码啊),这其中很多module并不是你使用的,但是你import上就好了,之后如果还是报错,那我就不知道了~我当时的错是因为图片路径的问题,我用的是相对路径,但是最后生成的exe文件一直都在./dist路径下的,所以是读不出来那些图片的,所以把exe文件拿出来放在你的.py目录下就好了。最后吐槽一句,因为pyinstaller是会把你所有import的包都加进去的。。。最后的exe文件真的大啊。。。(如果你用的是’-F’生成单一exe的话是这样的,如果不用’-F’的话,虽然exe文件小了,但是dist文件夹大了啊。。。。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pyqt Keras pyinstalle python
相关文章推荐