Quick Intro
In this article, you will learn how to build a cartoon styled version of a video by using OpenCV!
๐Note : OpenCV stands for Open Computer Vision. It is an open-source library written in Python, which can analyze images and videos.
Actually, I came across this article where a video can transform into a "pencil sketch form" & it got me inspired to build the same but with a new style! ๐
๐ Let's dive in as I'm super excited to walk you through it!
๐ฉโ๐ป Coding begins
0. Installation
I assume you must have installed Python on your machine. If not then please install & set it up from here! After this, open your Command Prompt as you have to install OpenCV library using the command --
pip install opencv-python
1. Imports
Open VSCode or any code editor of your choice, and start writing code inside a new .py
file!
- First, import the installed library,i.e,
cv2
(akaopencv
) - Next, specify the video you want to load (I'm using a dolphin ๐ฌ clip here) under function,
cv2.VideoCapture()
import cv2
cap = cv2.VideoCapture("dolphin.mp4")
2. Video Capture Properties
Get the height & width of the video frames & frame rate as well. cap
is the variable you loaded the video in the previous step!
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = cap.get(cv2.CAP_PROP_FPS)
3. Create VideoWriter Object
Here comes the most important step, where you have to specify the following info ๐
filename
: the place to save final resultsfourcc
: 4-character code of codec used to compress the frames. If you are a Windows User like me, then do useDIVX
only! Learn more here- Add the
fps
,height
&width
properties too!video_writer = cv2.VideoWriter('results\output.avi', cv2.VideoWriter_fourcc('D','I','V','X'), fps, (width, height))
4. Creating a Loop
Follow the code steps below ๐
#Loop through each frame
for frame_idx in range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))):
#Reads each frame
ret, frame = cap.read()
#Convert the video into a cartoon styled effect
cartoon_image = cv2.stylization(frame, sigma_s=150, sigma_r=0.25)
#Display the video
cv2.imshow('Video Player', cartoon_image)
#Write (saves) out frame
video_writer.write(cartoon_image)
#Breaks the loop when you press the 'Q'key
if cv2.waitKey(10) & 0xFF == ord('q'):
break
#Closes down everything
cap.release()
cv2.destroyAllWindows()
#Releases video writer
video_writer.release()
Save all the above code & make sure you have the 'original video file' as well as the code all in one place. Open Command Prompt, run the below command --
python cartoon_video.py
๐cartoon_video.py
is the code file. A window will come up running the cartoon effect & have patience until it's done!
5. Demo
๐ฅ Presenting, the combined demo for both of the videos! It's a canva design for a stylish showdown ๐คฉ
๐ AWESOME DEMO ๐
P.S: The background sound comes from the original video. Notice the cartoon effect video, isn't it amazing? ๐ฎ
Wrap Up
Congratulations! ๐๐
You have learned how to give a cartoon-styled effect in a video using Python. I hope you must have learned about the awesome magic of OpenCV here โจ & also had fun along the way.
You are free to tweak the code and add your own twist.
โญ Please do share a demo, if you added any changes or tried something different with the code above! I would love to read them ๐ค
Don't forget to drop your reactions ๐, sweet thoughts, demos & any questions in the comments below!
๐ค Reach me out on Twitter, Github & LinkedIn. for more such type of magical hacks!