Skip to content

Real time Video Stabilization

Abhishek Thakur edited this page Jun 22, 2019 · 44 revisions

Real-time Video Stabilization with VidGear

VidGear now packed with Stabilizer class - A powerful Real-time Video Stabilizer with low latency at expense of little to no additional power requirements(ideal for the Raspberry Pi too). The best part is that it can work both standalone and with any VidGear's Multi-Threaded Videocapture Class. Furthermore the stabilizer class wrapper is now directly integrated into VideoGear Class - enabling turbo video stabilization for any given video stream(including RaspiCam, Network etc) easily. This stabilization feature can be activated with stabilize boolean flag during initialization.

Features :

  • Real-time stabilization with low latency and no extra resources.
  • Works exceptionally well with low-frequency jitter.
  • Compatible with any vidgear's video streams including live network streams.
  • Works both standalone and with VidGear Multi-Threaded Classes.
  • integrated into VideoGear Multithreaded Class.

 


⚠️ Important:

  • Remember: This feature as of now is available with testing branch only!
  • The stabilizer may not perform well against High-frequency jitter in video. Use at your own risk!

 

Usage:

1. Firstly, Kindly clone and install the testing branch as follows:

git clone git clone https://github.com/abhiTronix/vidgear.git
cd testing
git checkout testing
sudo pip install .
cd

 

2. Using VideoGear Class with stabilization activated: (Recommended)

To enable stabilization mode, VideoGear Common Class now provides stabilize boolean flag. This flag can be easily set to True(to enable) or unset to False(to disable) this mode. Its default value is False. Nothing else is required to set/unset/alter, It's easy as that!:wink:


Additional Tips: 💡

  • VideoGear is a common class that provides easy to access any of the vidgear primary VideoCapturing classes(i.e. PiGear and CamGear) therefore any video stream can be stabilized using this class directly.
  • VideoGear Class also provides certain user-defined parameter for Stabilizer Class. These parameters can be used to alter the properties of Stabilizer Class On-the-Fly by passing this parameter to **options dict of VideoGear class.

# import libraries
from vidgear.gears import VideoGear 
import cv2

stream = VideoGear(source='test.mp4', stabilize = True, logging = True).start() # To open any valid video stream with `stabilize` flag set to True.

# infinite loop
while True:
	
	frame = stream.read()
	# read stabilized frames

	# check if frame is None
	if frame is None:
		#if True break the infinite loop
		break
	
	# do something with stabilized frame here
	
	cv2.imshow("Stabilized Frame", frame)
	# Show output window

	key = cv2.waitKey(1) & 0xFF
	# check for 'q' key-press
	if key == ord("q"):
		#if 'q' key-pressed break out
		break

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream.

 

3. Using Stabilizer Class Directly with OpenCV:

The VidGear's stabilizer class can also work standalone easily with any Computer Vision library such as OpenCV as follows:


Note: 💡 Stabilizer Class provide access to the certain parameter which is as follows:

  • smoothing_radius (int) : to alter averaging window size. It handles the quality of stabilization at the expense of latency and sudden panning. Larger its value, less will be panning, more will be latency and vice-versa. Its default value is 25.
  • border_size (int) : to alter output border cropping. It's will crop the border to reduce the black borders from stabilization being too noticeable. Larger its value more will be cropping. Its default value is 0 (i.e. no cropping).
  • border_type (string) : to change the border mode. Valid border types are 'black', 'reflect', 'reflect_101', 'replicate' and 'wrap'. It's default value is 'black'

from vidgear.gears.stabilizer import Stabilizer
import cv2

stream = cv2.VideoCapture(0) #Open any video stream such as live webcam video stream on first index(i.e. 0) device

stab = Stabilizer() #initiate stabilizer object with default parameters

# infinite loop
while True:
	
	# read frames
	(grabbed, frame) = stream.read()

	# check if frame empty
	if not grabbed:
		#if True break the infinite loop
		break
		
	#send current frame to stabilizer for processing
	stabilized_frame = stab.stabilize(frame)
		
	#hack for stabilizer which is still initializing
	if stabilized_frame is None:
		#wait till initialization is not completed
		continue 

	#do something with stabilized frame here
	   
	# Show output window
	cv2.imshow("Stabilized Frame", stabilized_frame)

	key = cv2.waitKey(1) & 0xFF
	# check for 'q' key-press
	if key == ord("q"):
		#if 'q' key-pressed break out
		break

cv2.destroyAllWindows()
# close output window

stream.release()
# safely close video stream

 

4. Using Stabilizer Class with VidGear Multi-Threaded Classes:

You can also easily use the Stabilizer class with Vidgear's other Multi-Threaded Classes without any extra effort as follows:

# import libraries
from vidgear.gears import CamGear
from vidgear.gears import WriteGear
from vidgear.gears.stabilizer import Stabilizer

import cv2

stream = CamGear(source='test.mp4').start() # To open any valid video stream 

writer = WriteGear(output_filename = 'Output.mp4') #Define writer

stab = Stabilizer() #initiate stabilizer object

# infinite loop
while True:
	
	frame = stream.read()
	# read stabilized frames

	# check if frame is None
	if frame is None:
		#if True break the infinite loop
		break

        #send current frame to stabilizer for processing
	stabilized_frame = stab.stabilize(frame)
		
	#hack for stabilizer which is still initializing
	if stabilized_frame is None:
		#wait till initialization is not completed
		continue 
	
	# do something with stabilized frame here

        # write stabilized frame to writer
        writer.write(stabilized_frame)
	
	cv2.imshow("Stabilized Frame", frame)
	# Show output window

	key = cv2.waitKey(1) & 0xFF
	# check for 'q' key-press
	if key == ord("q"):
		#if 'q' key-pressed break out
		break

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream
writer.close()
# safely close writer

 

Clone this wiki locally