A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/click-response-on-video-output-using-events-in-opencv-python/ below:

Click response on video output using Events in OpenCV – Python

Click response on video output using Events in OpenCV – Python

Last Updated : 23 Jul, 2025

OpenCV is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. 

In this article, we will create a program with a click response on video output using events in OpenCV Python library. We will be using “cv2.EVENT_LBUTTONDOWN” in case whenever the left mouse button is clicked and “cv2.EVENT_RBUTTONDOWN” in case whenever the Right mouse button is clicked.

To use the OpenCV library in python, we need to install these libraries as a prerequisite:

  1. Numpy Library: The computer processes images in the form of a matrix for which NumPy is used and  OpenCV uses it in the background.
  2. OpenCV library: OpenCV library previously it was cv but the updated version is cv2. It is used to manipulate images and videos.

To install these libraries, we need to run these pip commands in cmd:

pip install opencv-python
pip install numpy
The steps to read, display video and add click response on video output in OpenCV are: Step 1:  Import the required modules Python3 Step 2: Create the object of the VideoCapture to read the input file 
cv2.VideoCapture(0): For the first camera or webcam.
cv2.VideoCapture(1):  For the second camera or webcam.
cv2.VideoCapture(“file name.mp4”): For video file
Python3
cap = cv2.VideoCapture("test.mp4")
Step 3:  Check if the input file is opened or not

cap.isOpened() : It returns True or False based on whether or not our cap object has started capturing the frames.

Python3
if cap.isOpened() == False:
    # give error message 
    print("Error in opening file.")
else:
    # proceed forward
Step 4:  Entire file is read frame by frame and set mouse Callback function

cv2.waitkey(): This function of OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. 

cap.read(): This function returns 2 values:-

  1.  ret: This is a boolean value that is true if the frame is read successfully
  2. frame: This is the actual frame that is read.

Parameters:

window_name: A string representing the name of the window in which image to be displayed. 

image: It is the image or frame that is to be displayed.

Return Value: It doesn’t returns anything.

Python3
while(cap.isOpened()):
        ret,frame = cap.read()
        if ret == True:
            cv2.imshow("GFG",frame)
            cv2.setMouseCallback('GFG', mouse_click,param=frame)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break
        else:
            break
Step 5:  Define function to handle mouse click Python3
def mouse_click(event, x, y, flags, param):
    # to check if left mouse button was clicked
    if event == cv2.EVENT_LBUTTONDOWN:
          print("left click")
          cv2.imwrite("frame.jpg",param)
          
    # to check if right mouse button was clicked
    if event == cv2.EVENT_RBUTTONDOWN:
        print("right click")
        cv2.imshow("Current Frame",frame)
Below is the complete implementation Python3
import cv2


def mouse_click(event, x, y, flags, param):
    # to check if left mouse button was clicked
    if event == cv2.EVENT_LBUTTONDOWN:
        print("left click")
        cv2.imwrite("frame.jpg", param)

    # to check if right mouse button was clicked
    if event == cv2.EVENT_RBUTTONDOWN:
        print("right click")
        cv2.imshow("Current Frame", frame)


cap = cv2.VideoCapture("test.mp4")

if cap.isOpened() == False:
    # give error message
    print("Error in opening file.")
else:
    # proceed forward
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret == True:
            cv2.imshow("GFG", frame)
            cv2.setMouseCallback('GFG', mouse_click, param=frame)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break
        else:
            break


cap.release()
cv2.destroyAllWindows()
Output:  

Note: Video file should have in the same directory where program is executed.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4