python怎么按键开始与停止,如何在Python的Opencv Cam窗口中提供开始,停止,捕获和关闭按钮...

How to give start, stop, capture, and close buttons in video capture window to start, to stop, to take snapshot, to close the window?

I am using the below code to to open camera for video streaming:

import cv2.cv as cv

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

while True:

img = cv.QueryFrame(capture)

cv.ShowImage("camera", img)

if cv.WaitKey(10) == 27:

break

解决方案

Buttons aren't possible but you can use mouse clicks and key strokes to control your video. For example, use left click to toggle play/pause and implement record via key stroke:

import cv2

run=False

frame=0

path=#some video path

def foo(event, x, y, flags, param):

global run

global frame

#check which mouse button was pressed

#e.g. play video on left mouse click

if event == cv2.EVENT_LBUTTONDOWN:

run= not run

while run:

frame+=1

frame=cap.read()[1]

cv2.imshow(window_name, frame)

key = cv2.waitKey(5) & 0xFF

if key == ord("v"):

pass

#do some stuff on key press

elif event == cv2.EVENT_RBUTTONDOWN:

pass

#do some other stuff on right click

window_name='videoPlayer'

cv2.namedWindow(window_name)

cv2.setMouseCallback(window_name, foo)

cap=cv2.VideoCapture(path)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值