gpt4 book ai didi

python - 如何识别左键和右键单击以及0xFF

转载 作者:行者123 更新时间:2023-12-02 16:34:47 25 4
gpt4 key购买 nike

我有一个图像捕获程序,当按下键盘上的某些键时,可以捕获网络摄像头中的任何帧。目前,除了键盘之外,我还想创建一个替代方案,即使用mouseclick,但我都需要同时工作。这是代码:

    while True:
# Capture frame-by-frame
ret, frame = video_capture.read()

pressedKey = cv2.waitKey(1) & 0xFF

#take picture
if pressedKey == ord('c'):
cv2.imwrite('./results/'+str(subject)+'_'+str(position)+'_'+rgba[counter]+'.png',frame)
counter= counter + 1
details=str(subject)+','+str(position)+','+str(counter)+'\n'
with open('./details.txt', 'a') as f:
f.write(details)
print("Image taken succesfully")
#quit
elif pressedKey== ord('q'):
print("Closing application")
break
cv2.imshow('Video', frame)

我想通过按“c”或使用LMouseClick拍照,然后按“q”或使用RMouseClick退出。

最佳答案

由于没有主题,位置等信息,我对您的代码做了一些修改。

无论如何,这是可以根据需要工作的最终版本:

import cv2
video_capture = cv2.VideoCapture(0)

status = 1
# mouse callback function
def mouse_func(event,x,y,flags,param):
global status
if event == cv2.EVENT_LBUTTONDOWN:
cv2.imwrite('save.png',frame)
details='details'
with open('./details.txt', 'a') as f:
f.write(details)
print("Image taken succesfully")

elif event == cv2.EVENT_RBUTTONDOWN:
status = 0
print("Closing application")

cv2.namedWindow('Video')
cv2.setMouseCallback('Video',mouse_func)

while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
pressedKey = cv2.waitKey(1) & 0xFF

#take picture
if pressedKey == ord('c'):
cv2.imwrite('save.png',frame)
details='details'
with open('./details.txt', 'a') as f:
f.write(details)
print("Image taken succesfully")
#quit
elif pressedKey== ord('q') or status == 0:
print("Closing application")
break
cv2.imshow('Video', frame)
print("Application Closed")
cv2.destroyAllWindows()

您应该创建一个回调函数( mouse_func),然后应该使用 EVENT_LBUTTONDOWNEVENT_RBUTTONDOWN左右单击。

注意,有一个全局变量 status,如果发生任何右键单击,则将其设置为 0。然后,在主循环中,我们检查按下的键是否为 qstatus是否设置为 0

关于python - 如何识别左键和右键单击以及0xFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60846048/

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