How to Use Check for Multiple Events in Python using OpenCV



Python


In this article, we show how to check for multiple events in Python using the OpenCV module.

Events are actions that a user takes such as a left mouse click, a right mouse click, strolling the mouse, a key press, etc.

Events are important because they allow for dynamic applications in which things occur in response to a user's actions.

We don't have to simply monitor or look for one specific event in Python using the OpenCV module. We can check for multiple events that a user may do.

In this article, we're going to create a program that monitors for 2 events, a left mouse click and a right mouse click.

If the user does a left mouse click, then the text 'Left Click' appears on the screen. If the user does a right mouse click, the text 'Right Click' appears on the screen.

Below is the image showing the result of this program.

Checking for multiple events in Python with OpenCV

This is an example again of monitoring for multiple events that a user may do.

You'll see that the way of checking for multiple events is simply a series of if/elif statements that allow us to see if a certain event is performed. If so, we can program any action based on the occurrence of that event.

Below is the full code for this Python program using OpenCV which checks and take certain actions based on the user event.



Let's now go over this code.

First, we import all the modules we need which is cv2 (OpenCV) and numpy (to create a blank image).

Next, we create a blank white image and store this in the variable, image. This is done using numpy.

Next, we create our custom function, which will print 'Left Click' when the user left clicks on the mouse and prints 'Right Click' when the user right clicks on the mouse. The name of the function is show_clicked and it takes with it a few parameters.

The first 3 parameters are vital and we use them in this program.

The first parameter is the event. What event on the image that a user takes do you want to trigger an action? There are quite a few events, including left mouse clicks, right mouse clicks, key presses, etc. In this example, we use a left mouse click as one event and a right mouse click as a second event.

The second and third parameters are x and y. x is the x-coordinate and y is the y-coordinate. They track exactly where a user is on the window.

So, if the left mouse click occurs, then we use the cv2.putText() function to add 'Left Click' to the window, where the user clicks. This is done because the org attribute is set to (x,y).

If the right mouse click occurs, then we use the cv2.putText() function to add 'Right Click' to the window, where the user clicks. This is done because the org attribute is set to (x,y).

Notice how we're able to check for multiple events with OpenCV. We simply use if/elif statements. You can add as many if/elif statements as you want or need to check for various events and accordingly take action for each of these events that a user does. This is all that is required to check for multiple events in Python with OpenCV.

The rest of the code is simply about getting the callback function to work, linking it to the window and to our custom function.

And this is how we can check for multiple events in Python with the OpenCV module.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...