Instagram
youtube
Facebook
Twitter

Draw Shapes on Videos

  • Drawing is a common operation in image processing and video processing.

  • With the help of OpenCV, we can draw shapes or annotations and add text to video frames in real time.

  • This feature is very useful in applications such as video surveillance, sports analysis, and video editing.

  • To draw shapes including lines, rectangles, etc. we can use OpenCV’s inbuilt drawing functions. We have already given a detailed overview of drawing function in our drawing function tutorial.

In this tutorial we’ll learn how to draw shapes and add text on videos.

input:

Example - Drawing rectangle on video using OpenCV. 

Code:

import cv2
my_input = cv2.VideoCapture("video1.mp4")
if not my_input.isOpened():
    print("Error opening video file")
    exit()
output_file = "output_rectangle.mp4"
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, 30.0, (int(my_input.get(cv2.CAP_PROP_FRAME_WIDTH)),int(my_input.get(cv2.CAP_PROP_FRAME_HEIGHT))))
while my_input.isOpened():
    ret, frame = my_input.read()
    if ret:
        rect = cv2.rectangle(frame, (50, 200), (880, 20),(90, 80, 10), 20)
        out.write(rect)
        cv2.imshow("Frame", rect)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
my_input.release()
out.release()
cv2.destroyAllWindows()
  • We imported required libraries.

  • The cv2.VideoCapture() function is used to read the video file.

  • Then, we used cv2.isOpened() to check if the video file was opened successfully. If it was not opened, then an error message was printed.

  • The name of the output file is specified as "output_blur.mp4" and the codec was set as "mp4v".

  • The cv2.VideoWriter() function is used, with parameters such as image, codec, fps, and resolution to create a new video file with the specified parameters.

  • Inside the while loop we used cv2.rectangle with some parameters to draw the shape.

  • At last we have used the release() function to release all the resources.

 

Result:

 

Example - Adding Text on video using OpenCV.

Code:

import cv2
my_input = cv2.VideoCapture("video1.mp4")
if not my_input.isOpened():
    print("Error opening video file")
    exit()
output_file = "output_text.mp4"
font = cv2.FONT_ITALIC
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, 30.0, (int(my_input.get(cv2.CAP_PROP_FRAME_WIDTH)),int(my_input.get(cv2.CAP_PROP_FRAME_HEIGHT))))
while my_input.isOpened():
    ret, frame = my_input.read()
    if ret:
        rect = cv2.putText(frame, 'Codersdaily', (20,400), font, 4, (90, 80, 10), 10)
        out.write(rect)
        cv2.imshow("Frame", rect)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
my_input.release()
out.release()
cv2.destroyAllWindows()

Here, all the code is same as previous example just we have used cv2.putText function to add the text with required values like - image path,Text content, Starting coordinates, Font, Font size, color code, and thickness.
 

Result: