如何使用C ++扩展/补码编译python文件?

时间:2019-06-10 03:02:01

标签: c++ python-2.7 pyinstaller opencv3.0

在使用PYINSTALLER进行编译之前,我一直在尝试运行.exe文件。它的功能是允许我选择将用于OpenCV的摄像机。但是控制台上升,然后关闭,告诉我在第30行的“ MAIN”上存在问题,其中c ++ include或module称为:

   # Get camera list
device_list = device.getDeviceList() /*RIGHT HERE LINE: 30
index = 0

我一直在寻找列出连接到我的PC的所有可用凸轮,以便选择哪一个将适合与OpenCV一起使用。我使用的是C ++扩展或实现(我不知道该如何称呼),我关注了这篇文章:https://www.codepool.biz/multiple-camera-opencv-python-windows.html?fbclid=IwAR1TX1i5t_5xkiBF86O5YlbqIs5hQcpBtMSsIHhLn2YvMiF8EuFzLZ-M134

我用c ++创建的文件具有检索相机信息的功能,并使用设置(我称其为“ setup22.py”)建立了文件:

from distutils.core import setup, Extension

module_device = Extension('device',
                        sources = ['device.cpp'], 
                        library_dirs=['C:\Program Files (x86)\Windows Kits\10\Lib']
                      )

    setup (name = 'WindowsDevices',
            version = '1.0',
            description = 'Get device list with DirectShow',
            ext_modules = [module_device])

我在pyinstaller中尝试过:

pyinstaller.exe --onefile test22.py (test22 is the python's file name)
pyinstaller.exe --hidden-import=device -F test22.py

文章中提供了c ++代码。

setup22.py位于此处:

from distutils.core import setup, Extension

module_device = Extension('device',
                        sources = ['device.cpp'], 
                        library_dirs=['C:\Program Files (x86)\Windows Kits\10\Lib']
                      )

setup (name = 'WindowsDevices',
        version = '1.0',
        description = 'Get device list with DirectShow',
        ext_modules = [module_device])

当编译.EXE文件不起作用时,test22.py可以在编译前工作。命令行打开和关闭。

import device
import cv2

def select_camera(last_index):
    number = 0
    hint = "Select a camera (0 to " + str(last_index) + "): "
    try:
        number = int(input(hint))
        # select = int(select)
    except Exception:
        print("It's not a number!")
        return select_camera(last_index)

    if number > last_index:
        print("Invalid number! Retry!")
        return select_camera(last_index)

    return number


def open_camera(index):
    cap = cv2.VideoCapture(index)
    return cap

def main():
    # print OpenCV version
    print("OpenCV version: " + cv2.__version__)

    # Get camera list
    device_list = device.getDeviceList()
    index = 0

    for name in device_list:
        print(str(index) + ': ' + name)
        index += 1

    last_index = index - 1

    if last_index < 0:
        print("No device is connected")
        return

    # Select a camera
    camera_number = select_camera(last_index)

    # Open camera
    cap = open_camera(camera_number)

    if cap.isOpened():
        width = cap.get(3) # Frame Width
        height = cap.get(4) # Frame Height
        print('Default width: ' + str(width) + ', height: ' + str(height))

        while True:

            ret, frame = cap.read();
            cv2.imshow("frame", frame)

            # key: 'ESC'
            key = cv2.waitKey(20)
            if key == 27:
                break

        cap.release() 
        cv2.destroyAllWindows() 

if __name__ == "__main__":
    main()

我想知道你们中的一些人是否可以定向我,以便理解我在编译时应该包括的内容。

0 个答案:

没有答案