Tag Archives: cv2.waitKey()

Creating a Bouncing Ball Screensaver using OpenCV-Python

A screensaver is a computer program that fills the screen with anything you wish when the computer is left idle for some time. Most of you might have used a screensaver on your laptops, TV etc. In the good old days, they used to fascinate most of us. In this blog, we will be creating a bouncing ball screensaver using OpenCV-Python.

Task:

Create a Window that we can write text on. If we don’t write for 10 seconds screensaver will start.

For this we need to do two things:

  • First, we need to check whether a key is pressed in the specified time. Here, I have used 10 sec.
  • Second, create a bouncing ball screensaver and display it only if no key is pressed in the specified time, otherwise, display the original screen.

The first part can be done using the OpenCV cv2.waitKey() function which waits for a specific time for a key press (See here for more details).

For the second part, we first need to create a bouncing ball screensaver. The main idea is to change the sign of increment (dx and dy in the code below) on collision with the boundaries. This can be done using the following code

The snapshot of the screensaver looks like this

Now, we need to integrate this screensaver function with the cv2.waitKey() function as shown in the code below

You need to set the size of the screensaver and background image to be the same. The output looks like this

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Show current DateTime on live video using OpenCV-Python

Have you seen the security cameras output where DateTime continuously keeps updating? In this blog, we will be doing the same using OpenCV-Python i.e. we will put current DateTime on the live webcam feed. So, let’s get started.

For fetching current DateTime, we will be using Python’s DateTime module. The following code shows how to get the current DateTime

To put the DateTime on the live video, we will be using cv2.putText() on each frame as shown below

To know more about cv2.putText(), refer to this blog.

Above are the two things, that we will be needing for this task. I hope you understand these. Now, let’s get started

Steps:

  • Open the camera using cv2.VideoCapture()
  • Until the camera is open
    • Grab each frame using cap.read()
    • Put the current DateTime on each frame using cv2.putText() as discussed above
    • Display each frame using cv2.imshow()
  • On termination, release the webcam and destroy all windows using cap.release() and cv2.destroyAllWindows() respectively.

Code:

The snapshot of the output looks like this

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Write Text on images in real-time using OpenCV-Python

In this blog, we will learn how to use OpenCV cv2.putText() function for writing text on images in real-time. Most of you might have used cv2.putText(), if not this is how it looks like.

cv2.putText(img, text, position, font, fontScale, color, thickness, lineType, bottomLeftOrigin)

The above function draws the text on the input image at the specified position. If the specified font is unable to render any character, it is replaced by a question mark.

Another important thing that we will be using is OpenCV cv2.waitKey() function. This returns -1 when no key is pressed otherwise returns the ASCII value of the key pressed or a 32-bit integer value depending upon the platform or keyboard modifier(Num lock etc.). You can find this by printing the key as shown below.

If it returns a 32-bit integer, then use cv2.waitKey() & 0xFF which leaves only the last 8 bits of the original 32 bit.

ord(‘q’) converts the character to an int while chr(113) does exactly the opposite as shown in the code below.

I hope you understand all this, now let’s get started

Steps:

  • Read the image and initialize the counter that will be used for changing the position of the text.
  • Inside an infinite while loop,
    • display the image and use cv2.waitKey() for a keypress.
    • Convert this key into character using chr() and draw it on the image using cv2.putText().
    • Increase the counter.
    • Provide the termination condition
  • On exit, destroy all windows.

Below is the code for this

The output looks like this

Hope you enjoy reading. In the next blog, we will learn how to write text on images at mouse click position.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Write Text on images at mouse click position using OpenCV-Python

In the previous blog, we discussed how to write text on images in real-time. In that, we manually specified the position for text placement. This is quite tedious if we were to write text at multiple positions.

So, what if we automate this process. That is we automatically get the coordinates of the image where we click and then put text at that position using cv2.putText() function as we did in the previous blog.

This is what we will do in this blog i.e. write text on images at mouse click position. To do this, we will create a mouse callback function and then bind this function to the image window.

Mouse callback function is executed whenever a mouse event takes place. Mouse event refers to anything we do with the mouse like double click, left click etc. All available events can be found using the following code

Below is an example of a simple mouse callback function that draws a circle where we double click.

We then need to bind this callback function to the image window. This is done using
cv2.setMouseCallback(window_name, mouse_callback_function) as shown below

I hope you understood mouse callback function, now let’s get started

Steps:

  • Create a mouse callback function where on every left double click position we put text on the image.
  • Create or read an image.
  • Create an image window using cv2.namedWindow()
  • Bind the mouse callback function to the image window using cv2.setMouseCallback()
  • Display the new image using an infinite while loop

Code:

In the above code, press ‘q’ to stop writing and left double click anywhere to again start writing.

You can play with mouse callback function using other mouse events. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.