# 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()
# 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 defined parametersstab=Stabilizer(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.