Я пытался сделать так, чтобы мой .exe-файл запускался до компиляции с помощью PYINSTALLER.Его функция - позволить мне выбрать камеру, которую я буду использовать для OpenCV.Но консоли поднимаются, а затем закрываются, сообщая, что есть проблема с «MAIN» в строке 30, где вызывается модуль включения или модуль c ++:
# Get camera list
device_list = device.getDeviceList() /*RIGHT HERE LINE: 30
index = 0
Я искал список всех доступных кулачковподключен к моему ПК, чтобы выбрать, какой из них будет правильным для использования с 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])
test22.py работает до компиляции, когда компиляция .EXE-файла не работает.Командная строка открывается и закрывается.
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()
Мне интересно, могут ли некоторые из вас сориентировать меня, чтобы понять, что я должен включать при компиляции, пожалуйста.