Turn Videos Into Circles (and Your PC Into a Heater) With CircleVision

by tanish satpal in Circuits > Computers

294 Views, 5 Favorites, 0 Comments

Turn Videos Into Circles (and Your PC Into a Heater) With CircleVision

Image 16-11-24 at 7.14 PM.jpeg

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

import cv2
import matplotlib.pyplot as plt
import numpy as np

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

video_path = "rickroll.mp4"

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

cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error: Could not open video file.")
exit()

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

plt.ion()
fig, ax = plt.subplots()
fig.set_facecolor('black')

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

pixelated_resolution = (32, 16)
circle_radius = 0.4

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

while True:
ret, frame = cap.read()
if not ret:
break

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

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

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

small_frame = cv2.resize(frame, pixelated_resolution)

Reduce the frame resolution to simulate a pixelated effect.

• cv2.resize: Resizes the frame to the pixelated resolution.

Clearing the Previous Frame

ax.clear()

Clear the plot for displaying the next frame, reducing lag

• Avoids overlapping of frames during real-time display.

Plotting Each Pixel As a Circle

for i in range(small_frame.shape[0]):
for j in range(small_frame.shape[1]):
color = small_frame[i, j] / 255.0
x_pos = j + 0.5
y_pos = small_frame.shape[0] - i - 0.5
ax.add_patch(plt.Circle((x_pos, y_pos), circle_radius, color=color))

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

ax.set_xlim(0, small_frame.shape[1])
ax.set_ylim(0, small_frame.shape[0])
ax.set_aspect('equal')
ax.axis('off')

Fine-tune the grid display and hide unnecessary axes.

• Sets axis limits and aspect ratio to match the grid.

Simulating Frame Rate

plt.pause(0.03)

Pause briefly to control the speed of the video playback.

• plt.pause(0.03): Pauses for 30 milliseconds (approximately 33 FPS).

Releasing Resources

cap.release()
plt.close()

Clean up resources and close the plot after playback.

• cap.release(): Releases the video file.

• plt.close(): Closes the Matplotlib plot.

Conclusion

Image 16-11-24 at 7.34 PM.jpeg

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