如何在 Python 中的 Opencv Cam 窗口中提供开始、停止、捕获和关闭按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21523398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:04:12  来源:igfitidea点击:

How to give Start, stop, capture and close buttons in Opencv Cam window in Python

pythonpython-2.7opencvbuttonpythoncard

提问by PSSR

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

采纳答案by jlarsch

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)

回答by SimonPJ

I had this problem before with OpenCV. As far as I am aware there is no functionality for buttons in OpenCV.

我之前在 OpenCV 上遇到过这个问题。据我所知,OpenCV 中没有按钮功能。

However, I used Tkinter and created a canvas along with some buttons (in your case these will be start, stop, capture, close). Each frame that was captured using OpenCV I drew onto the Tkinter canvas.

但是,我使用了 Tkinter 并创建了一个带有一些按钮的画布(在您的情况下,这些按钮将是开始、停止、捕获、关闭)。我在 Tkinter 画布上绘制了使用 OpenCV 捕获的每一帧。

I was using this for a frame by frame program, so I am not sure how well this method will perform in real time.

我在逐帧程序中使用它,所以我不确定这种方法的实时性能如何。

A very quick example code:

一个非常快速的示例代码:

from Tkinter import *
import cv2.cv as cv

root = Tk()

w = Canvas(root, width=500, height=300, bd = 10, bg = 'white')
w.grid(row = 0, column = 0, columnspan = 2)

b = Button(width = 10, height = 2, text = 'Button1')
b.grid(row = 1, column = 0)
b2 = Button(width = 10, height = 2, text = 'Button2')
b2.grid(row = 1,column = 1)

cv.NamedWindow("camera",1)
capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    canvas.create_image(0,0, image=img)
    if cv.WaitKey(10) == 27:
        break

root.mainloop()

This may or may not work right off the bat as I am not in a position to test this right now. One potential change I can see would be the image format OpenCV uses. You may need to use one of the conversion functions to change the format.

这可能会也可能不会立即起作用,因为我现在无法对此进行测试。我可以看到的一个潜在变化是 OpenCV 使用的图像格式。您可能需要使用其中一种转换函数来更改格式。