import cv2
clicked = False
def onMouse(event, x, y, flags, param): #定义鼠标回调事件的函数
global clicked
if event == cv2.EVENT_LBUTTONUP: #鼠标左键松开
clicked = True #标记此时鼠标左键已松开
camerac = cv2.VideoCapture(0)
cv2.namedWindow("MyWindow") #创建指定名称的窗口
cv2.setMouseCallback("MyWindow", onMouse) #为该窗口设置鼠标回调事件
success, frame = camerac.read()
while success and cv2.waitKey(1) == -1 and not clicked: #读取帧失败或按下任意键或鼠标左键松开时停止
cv2.imshow("MyWindow", frame)
success, frame = camerac.read()
cv2.destroyWindow("MyWindow")
camerac.release()