import PySimpleGUI as sg
import cv2
import threading
import time
import os
各カメラの設定を保存する辞書
cameras = {
'camera1': {'interval': 10, 'folder': './'},
'camera2': {'interval': 10, 'folder': './'},
# 必要なだけカメラを追加
}
def capture_image(camera_id, folder):
cap = cv2.VideoCapture(camera_id)
ret, frame = cap.read()
if ret:
filename = os.path.join(folder, f'{camera_id}_{time.time()}.jpg')
cv2.imwrite(filename, frame)
cap.release()
def capture_images():
while True:
for camera_id, camera_info in cameras.items():
capture_image(camera_id, camera_info['folder'])
time.sleep(camera_info['interval'])
def start_capturing():
threading.Thread(target=capture_images, daemon=True).start()
レイアウトを定義
layout = [[sg.Text('Camera1 Folder'), sg.Input(key='-FOLDER1-'), sg.FolderBrowse()],
[sg.Text('Camera1 Interval'), sg.Input(key='-INTERVAL1-')],
[sg.Text('Camera2 Folder'), sg.Input(key='-FOLDER2-'), sg.FolderBrowse()],
[sg.Text('Camera2 Interval'), sg.Input(key='-INTERVAL2-')],
[sg.Button('Start Capturing')]]
ウィンドウを作成
window = sg.Window('Camera App', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Start Capturing':
cameras['camera1']['folder'] = values['-FOLDER1-']
cameras['camera1']['interval'] = int(values['-INTERVAL1-'])
cameras['camera2']['folder'] = values['-FOLDER2-']
cameras['camera2']['interval'] = int(values['-INTERVAL2-'])
start_capturing()
window.close()