python newbie here我有以下代码,我用它来使用opencv捕获图片。当我按下键盘上的 q 键时,它会拍摄照片。 到目前为止工作正常。
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
out = cv2.imwrite('capture.jpg', frame)
break
cap.release()
cv2.destroyAllWindows()
当我发出命令时,我需要它来捕获图片(比如'立即捕获')。当用户提供书面命令而不是按键时,任何人都可以帮助我捕获帧。感谢
答案 0 :(得分:1)
你可以这样写
reqCommand = 'Capture_pic'
command = input('Enter command')
if command == reqCommand:
out = cv2.imwrite('capture.jpg', frame)
更新
此更新旨在使其无法阻止程序的执行
import cv2
import threading
command = None
def process():
while True:
command = input('Enter command')
thread = threading.Thread(target=process)
thread.start()
cap = cv2.VideoCapture(0)
reqCommand = 'Capture_pic'
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if command == reqCommand:
out = cv2.imwrite('capture.jpg', frame)
thread.terminate()
break
cap.release()
cv2.destroyAllWindows()