我希望您可以看一下我的代码,并告诉我我没有正确使用线程。我正在尝试通过Python的线程模块将命令发送到USB附加摄像头。 “ get_live_view”告诉相机每2秒拍摄一次低分辨率图像。 “侦听器”正在等待字符“ 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()