Following is the bare-minimum code you need to get started with Stabilizer Class and various VideoCapture Gears:
You can use any VideoCapture Gear instead of CamGear in the similar manner, as shown in this usage example.
Performance Improvement in v0.3.5
Starting from v0.3.5, the Stabilizer class has been refactored to bound the Average Sliding-Window (ASW) memory to O(smoothing_radius) and real-time streaming is now possible without memory issues.
# import required librariesfromvidgear.gears.stabilizerimportStabilizerfromvidgear.gearsimportCamGearimportcv2# To open live video stream on webcam at first index(i.e. 0) devicestream=CamGear(source=0).start()# initiate stabilizer object with default parametersstab=Stabilizer()# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# send current frame to stabilizer for processingstabilized_frame=stab.stabilize(frame)# wait for stabilizer which still be initializingifstabilized_frameisNone:continue# {do something with the stabilized frame here}# Show output windowcv2.imshow("Output Stabilized Frame",stabilized_frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# clear stabilizer resourcesstab.clean()# safely close video streamstream.stop()
The VidGear's stabilizer class can also work standalone easily with any Computer Vision library such as OpenCV itself. Following is the bare-minimum code you need to get started with Stabilizer Class and OpenCV:
# import required librariesfromvidgear.gears.stabilizerimportStabilizerimportcv2# Open suitable video stream, such as webcam on first index(i.e. 0)stream=cv2.VideoCapture(0)# initiate stabilizer object with default parametersstab=Stabilizer()# loop overwhileTrue:# read frames from stream(grabbed,frame)=stream.read()# check for frame if not grabbedifnotgrabbed:break# send current frame to stabilizer for processingstabilized_frame=stab.stabilize(frame)# wait for stabilizer which still be initializingifstabilized_frameisNone:continue# {do something with the stabilized frame here}# Show output windowcv2.imshow("Stabilized Frame",stabilized_frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# clear stabilizer resourcesstab.clean()# safely close video streamstream.release()
Stabilizer class provide certain parameters which you can use to tweak its internal properties. The complete usage example is as follows:
New in v0.3.5
The mode parameter and the StabilizerMode enum were added in v0.3.5. Existing code that does not pass mode keeps working - defaults to StabilizerMode.ASW.
# import required librariesfromvidgear.gears.stabilizerimportStabilizer,StabilizerModefromvidgear.gearsimportCamGearimportcv2# To open live video stream on webcam at first index(i.e. 0) devicestream=CamGear(source=0).start()# initiate stabilizer object with defined parametersstab=Stabilizer(mode=StabilizerMode.ASW,smoothing_radius=30,crop_n_zoom=True,border_size=5,logging=True,)# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# send current frame to stabilizer for processingstabilized_frame=stab.stabilize(frame)# wait for stabilizer which still be initializingifstabilized_frameisNone:continue# {do something with the stabilized frame here}# Show output windowcv2.imshow("Output Stabilized Frame",stabilized_frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# clear stabilizer resourcesstab.clean()# safely close video streamstream.stop()
VideoGear's stabilizer can be used in conjunction with WriteGear API directly without any compatibility issues. The complete usage example is as follows:
You can also add live audio input to WriteGear pipeline. See this bonus example ➶
# import required librariesfromvidgear.gears.stabilizerimportStabilizerfromvidgear.gearsimportCamGearfromvidgear.gearsimportWriteGearimportcv2# Open suitable video streamstream=CamGear(source="unstabilized_stream.mp4").start()# initiate stabilizer object with default parametersstab=Stabilizer()# Define writer with default parameters and suitable output filename for e.g. `Output.mp4`writer=WriteGear(output="Output.mp4")# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if not None-typeifframeisNone:break# send current frame to stabilizer for processingstabilized_frame=stab.stabilize(frame)# wait for stabilizer which still be initializingifstabilized_frameisNone:continue# {do something with the stabilized frame here}# write stabilized frame to writerwriter.write(stabilized_frame)# Show output windowcv2.imshow("Stabilized Frame",stabilized_frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# clear stabilizer resourcesstab.clean()# safely close video streamstream.stop()# safely close writerwriter.close()
VideoGear API provides a special internal wrapper around Stabilizer class that enables easy stabilization for various video-streams (real-time or not) with minimum effort and writing way fewer lines of code.