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

button, opencv, python, python-2.7, pythoncard

Solution

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)

Problem

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 ```

Original source