Opencv+Python 显示摄像头的帧


import cv2

cameraCapture = cv2.VideoCapture(0)

# ------------------------获取鼠标指令------------------------
clicked = False
def onMouse(event, x, y, flags, param):
    global clicked
    if event == cv2.EVENT_LBUTTONUP:  # 鼠标左键松开
        clicked = True
cv2.namedWindow("MyWindow", cv2.WINDOW_AUTOSIZE)  # 可以省略
cv2.setMouseCallback("MyWindow", onMouse)
# cv2.setMouseCallback()是等待获取鼠标输入
# cv2.waitKey()是等待获取键盘输入,单位毫秒,返回值为:-1(没有键盘按下),ASCII码(键盘按下)

print("Showing camera feed. Click window or press any key to stop")

success, frame = cameraCapture.read()
frame = cv2.flip(frame, 1)  # 左右镜像,上下镜像(1和-1)
while success and cv2.waitKey(1) == -1 and not clicked:  # 可以读帧,延迟1ms若键盘无指令则继续下一帧操作,鼠标无指令
    cv2.imshow("MyWindow", frame)
    success, frame = cameraCapture.read()
    frame = cv2.flip(frame, 1)

cv2.destroyAllWindows()
cameraCapture.release()

你可能感兴趣的:(Opencv+Python)