gpt4 book ai didi

python - 如何在opencv中捕获图片

转载 作者:行者123 更新时间:2023-12-02 17:39:39 28 4
gpt4 key购买 nike

我在这里有以下代码,我正在使用以下代码使用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()

我发出命令时需要它来捕获图片(例如“立即捕获”)。当用户发出书面命令而不是通过按键时,谁能帮助我如何捕获帧。谢谢

最佳答案

你可以这样写

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()

关于python - 如何在opencv中捕获图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583138/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com