Turn Videos Into Circles (and Your PC Into a Heater) With CircleVision
by tanish satpal in Circuits > Computers
78 Views, 2 Favorites, 0 Comments
Turn Videos Into Circles (and Your PC Into a Heater) With CircleVision
Well, you have just been rickrolled. This is the CircleVision, which turns your videos into circles, and by changing the number of rows and columns, turn your PC into a bombshell. But, its not just about a rickroll.
This tutorial explains how to create a pixelated video player using Python. The code processes video frames, reduces their resolution, and displays them as grids of colored circles using Matplotlib. The result resembles a low-resolution pixel-art effect. We’ll break the code into sections and explain each step in detail.
Supplies
You will need to use a python processing program like Pycharm. You will need to install certain libraries into the terminal using the pip or pip3 command. Here are the commands:
pip install cv2
pip install matplotlib
pip install numpy
You can replace pip with pip3 in case your computer doesn't accept pip in the terminal.
You will also need a video to display in the folder. I am using rickroll.mp4
Importing Libraries
Now that we have installed the libraries, we need to import them to make them active in our code. They have their own functions.
• cv2: OpenCV is used for video processing, including reading frames and resizing them.
• matplotlib.pyplot: Used to display video frames as plots with custom styling.
• numpy: Provides support for numerical operations, though unused in this code.
Specify Video File Path
The path to the video file to be processed. Ensure the file exists in the specified location. Place the name in a variable.
Open the Video File
Open the video for reading and handle errors if the file cannot be accessed.
• cv2.VideoCapture: Loads the video.
• cap.isOpened(): Checks if the video was successfully opened.
Setting Up the Plot
Create a canvas called plt to display video frames interactively.
• plt.ion(): Enables real-time updates to the plot.
• fig.set_facecolor('black'): Sets a black background for better visuals.
Defining Pixelation Parameters
Set the resolution for pixelation and the size of the circles. The resolution lets you control how detailed the image should be. Warning: exceeding 256:128 resolution can cause a computer overload.
• pixelated_resolution: Controls the grid size (16x8 pixels).
• circle_radius: Determines the size of each grid cell’s circle.
Processing Video Frames
Read video frames one at a time and exit when the video ends.
• cap.read(): Fetches the next frame.
• ret: Indicates whether a frame was successfully read.
Converting Frame Color Format
Adjust the frame’s color format for Matplotlib compatibility.
• cv2.COLOR_BGR2RGB: Converts OpenCV’s default BGR format to Matplotlib’s RGB format.
Resizing the Frame
Reduce the frame resolution to simulate a pixelated effect.
• cv2.resize: Resizes the frame to the pixelated resolution.
Clearing the Previous Frame
Clear the plot for displaying the next frame, reducing lag
• Avoids overlapping of frames during real-time display.
Plotting Each Pixel As a Circle
Display each pixel as a fixed-size colored circle.
• Loops through the pixel grid:
• i and j: Row and column indices.
• color: Normalizes RGB values to [0, 1].
• plt.Circle: Adds a circle at (x_pos, y_pos) with the pixel’s color.
Adjusting Plot Properties
Fine-tune the grid display and hide unnecessary axes.
• Sets axis limits and aspect ratio to match the grid.
Simulating Frame Rate
Pause briefly to control the speed of the video playback.
• plt.pause(0.03): Pauses for 30 milliseconds (approximately 33 FPS).
Releasing Resources
Clean up resources and close the plot after playback.
• cap.release(): Releases the video file.
• plt.close(): Closes the Matplotlib plot.
Conclusion
This script creatively pixelates video frames and displays them as grids of circles, providing a fun and artistic way to visualize video data. However, it’s not without its downsides. The process involves heavy real-time computations for rendering individual frames and drawing numerous circles on the plot, which can quickly overwhelm a PC, especially for longer or high-resolution videos.
While it’s a playful project, this method is an inefficient and “silly” solution for practical video playback. Professional tools like OpenCV’s built-in rendering functions or video editors are far more optimized for these tasks. Nonetheless, this exercise demonstrates programming concepts such as video processing, real-time plotting, and creative visualization, making it an engaging but resource-intensive experiment.
And as usual, the google colab link: https://colab.research.google.com/drive/1JRapgvN5cfsyea6Zu8M3TeRaNvxIVjO5?usp=sharing