-
-
Notifications
You must be signed in to change notification settings - Fork 285
Real time Video Stabilization
VidGear now provides real-time Video Stabilization with minimalistic latency and at the expense of little to no additional computational power requirement with Stabilizer Class. The basic idea behind it is to tracks and save the salient feature array for the given number of frames and then uses these anchor point to cancel out all perturbations relative to it for the incoming frames in the queue. This class relies heavily on Threaded Queue mode for error-free, ultrafast frame handling and it is enabled by default. This stabilization feature in VideoGear can be activated with stabilize boolean flag during initialization.
- Real-time stabilization with low latency and no extra resources.
- Works exceptionally well with low-frequency jitter.
- Integrated with
VideoGearAPI therefore can be applied to any incoming stream. - Also works standalone.
⚠️ The stabilizer may not perform well against High-frequency jitter in video. Use at your own risk!⚠️
The VidGear's stabilizer class can also work standalone easily with any Computer Vision library such as OpenCV as follows:
Note: 💡 Stabilizer Class 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) o change extended border size that compensates for stabilized output video frames motions. Its default value is
0(no borders). -
crop_n_zoom: (bool) enables the feature where it crops and zooms frames(to original size) to reduce the black borders from stabilization being too noticeable (similar to the feature available in Adobe AfterEffects). It works in conjunction with the
border_sizeparameter, i.e. when this parameter is enabledborder_sizewill be used for cropping border instead of making them. Its default value isFalse. -
border_type:(string) to change the extended border style. Valid border types are
'black','reflect','reflect_101','replicate'and'wrap'. Its default value is'black'⚠️ Alteringborder_typeparameter is disabled whilecrop_n_zoomis enable! -
logging:(boolean) set this flag to enable/disable error logging essential for debugging. Its default value is
False.
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", 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
#clear stabilizer resources
stab.clean()
# safely close video stream
stream.release()
You can easily use the real-time Video Stabilization with Vidgear's various Gears without any extra effort as follows:
# import libraries
from vidgear.gears import VideoGear
from vidgear.gears import WriteGear
import cv2
stream = VideoGear(source=0, stabilize = True).start() # To open any valid video stream(for e.g device at 0 index)
writer = WriteGear(output_filename = 'Output.mp4') #Define writer
# 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
# 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