python - 导入 cv2 后,在 OSX 中使用 PyQT5 的 objc 奇怪警告

标签 python python-3.x opencv pyqt5

在我使用 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.
    

    最佳答案

    在 Python 上实现 Qt 和 OpenCV 的方式是通过绑定(bind)机制,其中可以从 Python 解释器调用共享 native 库中的元素(在这两种情况下都使用 C++ 编程)。这可以通过多种方式实现,例如 ctypes标准 Python 库的模块。

    问题是当两个共享库公开相同的符号时。 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 执行相同操作)。我还建议您开始使用虚拟环境,从那时起,您还可以通过为不同项目使用不同版本的依赖项来缓解此问题。

    关于python - 导入 cv2 后,在 OSX 中使用 PyQT5 的 objc 奇怪警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366671/

    相关文章:

    python - 用于具有自定义带宽链接的通用树形拓扑的 Mininet 脚本

    python - 在基类的类方法中获取继承类名

    Python 数据框复制切片警告

    c++ - GPU 上的 OpenCV FAST 检测器

    c++ - 视频处理指南

    python - 从列表创建 DataFrame

    python - Windows CMD 用python的sys.stdout在打印行的末尾添加一个随机数

    python - Selenium 源缺少登录字段

    python-3.x - websocket 中接收和发送方法的区别

    python - python-Opencv 或 numpy 中的非 sobel 离散梯度