def magic(videoPath, bg_path): stabilizer = VidStab() cap = cv2.VideoCapture(videoPath) fps = 30 # 保存视频的FPS,可以适当调整 # 可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: sudo apt-get install ffmepg fourcc = cv2.VideoWriter_fourcc(*'XVID') numFrame = 0 bg = cv2.imread(bg_path, cv2.IMREAD_UNCHANGED) if cap.isOpened(): while True: ret, img = cap.read() # img 就是一帧图片 if not ret: break # 当获取完最后一帧就结束 if numFrame == 0: size = img.shape #得到原视频的大小 w = size[1] h = size[0] videoWriter = cv2.VideoWriter('F:/5.avi', fourcc, fps, (w, h)) #将输出视频大小设置为原视频 bg = cv2.resize(bg, (w * 2, h)) #将背景大小设置,这一步按个人兴趣即可 I = change_bg(img, bg, numFrame * 2, 0) #得到转换后的图像 I = cv2.cvtColor(I, cv2.COLOR_RGB2BGR) #由于输出图像为RGB通道,要将其转化为GBR通道 videoWriter.write(I) #写入文件 numFrame = numFrame + 1 #帧数+1 print('done the ' + str(numFrame) + 'frame') else: print('视频打开失败!') videoWriter.release()
def __init__(self, input_feed=None, cam_input=0): """ This class can be used to feed input from an image, webcam, or video to your model. Parameters ---------- input_feed: str The file that contains the input image or video file. Leave empty for cam input_type. cam_input: int WebCam input [Default: 0] Example ------- ``` feed = InputFeeder(input_feed='video.mp4') for frame in feed.next_frame(): do_something(frame) feed.close() ``` """ self.input_feed = input_feed assert isinstance(self.input_feed, str) self.check_file_exists(self.input_feed) try: self._input_type, _ = mimetypes.guess_type(self.input_feed) assert isinstance(self._input_type, str) except AssertionError: self._input_type = "" self._progress_bar = None self._video_stabilizer = VidStab() self.load_feed(cam_input)
def main(): # initialize stabilizer stabilizer = VidStab() # os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp" # sets video capture source, live feed or using existing file # cap = cv2.VideoCapture(0) # cap = cv2.VideoCapture('vtest2.avi') # cap = cv2.VideoCapture("autovideosrc ! decodebin ! appsink", cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture("autovideosrc ! decodebin ! appsink", cv2.CAP_FFMPEG) # cap = cv2.VideoCapture("gst-launch-1.0 appsink ! application/x-rtp, encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink", cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('autovideosrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink',cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('autovideosrc ! decodebin ! videorate ! video/x-raw,framerate=20/1 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink', cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('autovideosrc ! decodebin ! appsink', cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('autovideosrc ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('rtsp://127.0.0.1:5000/out.h264') # cap = cv2.VideoCapture('autovideosrc ! ffmpegcolorspace ! video/x-raw-rgb ! appsink', cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('gst-launch-1.0 udpsrc port=5000 ! application/x-rtp, encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink') # set codec fourcc = cv2.VideoWriter_fourcc(*'XVID') # allows user to set parameter for size of frame & initializes new frame size spar = .7 bor = 20 w = cap.get(cv2.CAP_PROP_FRAME_WIDTH) * spar h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * spar out = cv2.VideoWriter('output.avi', fourcc, 30.0, (int(2 * (w + 2 * bor)), int(h + 2 * bor))) idframe = 0 while cap.isOpened(): ret, frame = cap.read() idframe += 1 # check if frame valid if ret: # defines text over raw frame & resize frame w/user set parameter text = f'Width: {w} Height: {h} frame id: {idframe}' datet = str(datetime.now()) frame = cv2.resize(frame, None, fx=spar, fy=spar, interpolation=cv2.INTER_CUBIC) # duplicates frame to avoid double text on stabilized frame frame_with_text = copy.copy(frame) frame_with_text = cv2.putText(frame_with_text, text, (10, int(h - bor)), cv2.FONT_HERSHEY_DUPLEX, .5, (255, 255, 255), 1, cv2.LINE_AA) frame_with_text = cv2.putText(frame_with_text, datet, (10, bor), cv2.FONT_HERSHEY_DUPLEX, .5, (255, 255, 255), 1, cv2.LINE_AA) frame_with_text = cv2.copyMakeBorder(frame_with_text, bor, bor, bor, bor, borderType=0) # initiates stabilization parameter for first few frames, enables smooth stabilization from start window = idframe - 1 if idframe < 31 else 30 # stabilization function based on parameters given forehand res = stabilizer.stabilize_frame(input_frame=frame, smoothing_window=window, border_type='black', border_size=bor) # text addition (date, time, frame number) for stabilized frame text = f'Width: {w} Height: {h} frame id: {idframe - 1}' res = cv2.putText(res, text, (10, int(h + 5)), cv2.FONT_HERSHEY_DUPLEX, .5, (255, 255, 255), 1, cv2.LINE_AA) res = cv2.putText(res, datet, (10, 30), cv2.FONT_HERSHEY_DUPLEX, .5, (255, 255, 255), 1, cv2.LINE_AA) # concatenates the 2 windows to a single window side by side concat = np.concatenate((frame_with_text, res), axis=1) cv2.imshow('stabilization comparison', concat) out.write(concat) # waits for optional 'q' or 'esc' user keystroke to quit at any time keyboard = cv2.waitKey(1) if keyboard == ord('q') or keyboard == 27: break # option to pause video using spacebar, continue on any key elif keyboard == 32: cv2.waitKey(0) # auto-exits when video is over if idframe == int(cap.get(cv2.CAP_PROP_FRAME_COUNT)): break # shows graphic representation of video stabilization trajectory, option for exporting data stabilizer.plot_trajectory() plt.show() stabilizer.plot_transforms() plt.show() # auto-release video capture & deletes open windows cap.release() out.release() cv2.destroyAllWindows()
def test_func(): from vidstab.VidStab import VidStab stabilizer = VidStab() stabilizer.stabilize(input_path='../t.mp4', output_path='stable_video.avi')
from Filter_Frame import * from LK_Method import * from Rotation_Correction import * from vidstab.VidStab import VidStab fileName = "/home/dilan/Desktop/Final Year Project/Programming Testing/Filter Test/Videos/CleanNew.mp4" videoFile = cv2.VideoCapture(fileName) stabilizer = VidStab() cap = cv2.VideoCapture(fileName) isFirst = True ret, alignedFrameRef = cap.read() while(ret != True): ret, frameRef = videoFile.read() alignedFrameRef = cv2.cvtColor(alignedFrameRef, cv2.COLOR_BGR2GRAY) alignedFrameRef = filterFrame(alignedFrameRef) mask, numOfPoints, pointsInterest_count, pointsInterest_curr, points = initializeMethod(alignedFrameRef) while cap.isOpened(): ret, frame = cap.read() if ret: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = filterFrame(frame) alignedFrameCurr = alignImages(alignedFrameRef, frame) alignedFrameCurr = stabilizer.stabilize_frame(input_frame=alignedFrameCurr, smoothing_window=30)
def stab(cropvid_name, stabvid_name): stabilizer = VidStab() stabilizer.stabilize(input_path=cropvid_name, output_path=stabvid_name)
from vidstab.VidStab import VidStab import cv2 stabilizer = VidStab(kp_method='SIFT') fourcc = cv2.VideoWriter_fourcc(*"XVID") vidcap = cv2.VideoCapture('18.avi') grabbed_frame, frame = vidcap.read() width = frame.shape[1] height = frame.shape[0] vid = cv2.VideoWriter("output.avi", fourcc, 30, (width,height)) xwidth = int(width * 1.2) xheight = int(height * 1.2) dim = (xwidth, xheight) crop_x = int(width*0.15) crop_y = int(height*0.15) new_width = xwidth - crop_x new_height = xheight - crop_y vidcap = cv2.VideoCapture('18.avi') while True: grabbed_frame, frame = vidcap.read() # Pass frame to stabilizer even if frame is None # stabilized_frame will be an all black frame until iteration 30 if frame is None: # There are no more frames available to stabilize break stabilized_frame = stabilizer.stabilize_frame(input_frame=frame, smoothing_window=1,border_size=50, border_type="replicate")