0

我希望您可以查看我的代码并告诉我我没有正确使用线程。我正在尝试通过 Python 的线程模块向我的 USB 连接相机发送命令。“get_live_view”告诉相机每 2 秒拍摄一张低分辨率图像。“Listener”正在等待字符“c”告诉“capture_image”拍摄高分辨率图像。我尝试使用线程,因此这两个函数不会同时访问相机,但每当我按“c”时,我都会收到错误消息:“无法认领 USB 设备...确保没有其他程序或内核模块正在使用设备。”

import os, threading
from time import sleep
from pynput.keyboard import Key, Listener

def capture_image():
    os.system("gphoto2 --capture-image-and-download --no-keep")
    os.system("rm capt0000.jpg")
    sleep(1)

def on_press(key):
    if key.char in ('c'):
        print('capture')
        t2 = threading.Thread(target=capture_image, args=())
        t2.start(); t2.join()

def on_release(key):
    if key == Key.esc:
        # Stop listener
        return False

def capture_preview():
    os.system("gphoto2 --capture-preview")
    os.system("rm capture_preview.jpg")
    sleep(2)

def get_live_view():
    while True:
        t3=threading.Thread(target=capture_preview, args=())
        t3.start(), t3.join()

t1 = threading.Thread(target=get_live_view, args=())
t1.start()

with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()
4

0 回答 0