导入 cv2 后,在 OSX 中使用 PyQT5 的 objc 奇怪警告
Posted
技术标签:
【中文标题】导入 cv2 后,在 OSX 中使用 PyQT5 的 objc 奇怪警告【英文标题】:objc weird warnings with PyQT5 in OSX after importing cv2 【发布时间】:2020-02-23 20:45:14 【问题描述】:在我使用 PyQt5 创建的一些简单 GUI 应用程序中导入 cv2(否则没有警告)后,我在 OSX 中收到了这些客观的 c 警告。这是一些最小的示例(仅警告,不会产生错误):
版本:
PyQt5 5.10.1 PyQt5-sip 4.19.21 opencv-python 4.1.2.30代码:
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget
import sqlite3
import cv2
class EmployeeBase(QWidget):
def __init__(self, window_title, geometry):
super().__init__()
self.setGeometry(*geometry)
self.setWindowTitle(window_title)
win_rectangle = self.frameGeometry()
center_point = QDesktopWidget().availableGeometry().center()
win_rectangle.moveCenter(center_point)
self.move(win_rectangle.topLeft())
self.setStyleSheet('QPushButton:!hover color: red')
self.show()
if __name__ == '__main__':
connection = sqlite3.connect('Employees.db')
cursor = connection.cursor()
test = QApplication(sys.argv)
window = EmployeeBase('Test', (0, 0, 500, 500))
sys.exit(test.exec_())
结果:
objc[1741]: Class QCocoaPageLayoutDelegate is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee5c0) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport (0x119519f20). One of the two will be used. Which one is undefined.
objc[1741]: Class QCocoaPrintPanelDelegate is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee638) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport (0x119519f70). One of the two will be used. Which one is undefined.
objc[1741]: Class QCocoaApplicationDelegate is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee340) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqcocoa.dylib (0x1194b9480). One of the two will be used. Which one is undefined.
objc[1741]: Class QNSApplication is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee2f0) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqcocoa.dylib (0x1194b94d0). One of the two will be used. Which one is undefined.
objc[1741]: Class QCocoaMenuLoader is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee2a0) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqcocoa.dylib (0x1194b9570). One of the two will be used. Which one is undefined.
objc[1741]: Class QNSImageView is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee660) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqcocoa.dylib (0x1194b9660). One of the two will be used. Which one is undefined.
objc[1741]: Class QNSStatusItem is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee6b0) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqcocoa.dylib (0x1194b96b0). One of the two will be used. Which one is undefined.
objc[1741]: Class QNSOpenSavePanelDelegate is implemented in both /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui (0x110fee480) and /usr/local/lib/python3.7/site-packages/PyQt5/Qt/plugins/platforms/libqcocoa.dylib (0x1194b9750). One of the two will be used. Which one is undefined.
【问题讨论】:
PyQt5和opencv使用Qt,你看到的错误是因为编译PyQt5使用的Qt版本和opencv使用的Qt版本不同。最好指出您安装的 pyqt5 和 opencv 的版本以及安装方式 @eyllanesc 我编辑了我的帖子,检查了版本并使用 pip 安装了两者 【参考方案1】:Qt 和 OpenCV 在 Python 上实现的方式是通过绑定机制,其中可以从 Python 解释器调用共享本机库(在这两种情况下均使用 C++ 编程)中的元素。这可以通过多种方式实现,例如标准 Python 库的ctypes 模块。
问题是当两个共享库公开相同的符号时。 OpenCV 可以使用 Qt 作为引擎来构建 GUI(它也可以使用其他的,但是您正在使用的版本被编译为包含 Qt 符号)。当解释器加载两个共享库(在本例中为 /Users/emadboctor/Library/Python/3.7/lib/python/site-packages/cv2/.dylibs/QtGui
和 /usr/local/lib/python3.7/site-packages/PyQt5/Qt/lib/QtPrintSupport.framework
)时,它会发出警告,因为加载机制在不同的位置检测到相同的符号。警告One of the two will be used. Which one is undefined.
意味着它可能需要一个或另一个,但没有预定义的顺序或任何偏好。如果您期望某些行为仅在两者之一中实现(例如,Qt 的 cv2 库版本是 5.1 版,PyQt 是 5.2 版,并且某个 doSomething
函数从一个版本到另一个版本。加载顺序将以不可预知的方式决定程序的行为)。
总的来说,由于您处于开发的早期阶段,并且 Qt 是一个相当稳定的库,您应该没问题。如果您遇到上述问题,您可以尝试不同的方法,例如重新编译 OpenCV 以使用您期望的 Qt 版本(或对 PyQt 执行相同操作)。我还建议您开始使用虚拟环境,从那时起,您还可以通过为不同项目使用不同版本的依赖项来缓解此问题。
【讨论】:
非常详尽的解释,谢谢,我会尝试重新安装两者,我会运行 PyCharm,默认情况下会创建虚拟环境(我不确定)无论如何我编辑了我的帖子并添加了版本,如果你需要检查.以上是关于导入 cv2 后,在 OSX 中使用 PyQT5 的 objc 奇怪警告的主要内容,如果未能解决你的问题,请参考以下文章