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)
Beispiel #2
0
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()
class InputFeeder:
    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 load_feed(self, cam_input):
        if "video" in self._input_type:
            self.cap = cv2.VideoCapture(self.input_feed)
        elif "image" in self._input_type:
            self.cap = cv2.imread(self.input_feed)
        elif "cam" in self.input_feed.lower():
            self._input_type = self.input_feed
            self.cap = cv2.VideoCapture(cam_input)
        else:
            msg = f"Source: {self.input_feed} not supported!"
            logger.warn(msg)
            raise FormatNotSupported(msg)
        logger.info(f"Loaded input source type: {self._input_type}")

    @staticmethod
    def check_file_exists(file):
        if "cam" in file:
            return

        if not os.path.exists(os.path.abspath(file)):
            raise FileNotFoundError(f"{file} does not exist.")

    @property
    def source_width(self):
        return (int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)) if hasattr(
            self.cap, "get") else input_feed.cap.shape[1])

    @property
    def source_height(self):
        return (int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) if hasattr(
            self.cap, "get") else input_feed.cap.shape[0])

    @property
    def video_len(self):
        return int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))

    @property
    def fps(self):
        return int(self.cap.get(cv2.CAP_PROP_FPS))

    @property
    def frame_size(self):
        return (self.source_height, self.source_width)

    @property
    def progress_bar(self):
        if not self._progress_bar:
            self._progress_bar = tqdm(total=int(self.video_len - self.fps + 1))
        return self._progress_bar

    def resize(self, frame, height=None, width=None):
        if (height and width) is None:
            width, height = (self.source_width // 2, self.source_height // 2)
        return cv2.resize(frame, (width, height))

    def draw_cirle(self,
                   frame,
                   width=None,
                   height=None,
                   radius=100,
                   color=(0, 255, 0),
                   thickness=5):
        width = width if width is not None else self.source_width // 2
        height = height if height is not None else self.source_height // 2
        x1 = width - radius
        y1 = height - radius
        x2 = width + radius
        y2 = height + radius
        cv2.circle(
            frame,
            (width, height),
            radius,
            color,
            thickness,
        )
        return (x1, y1), (x2, y2)

    @staticmethod
    def add_text(text, image, position, font_size=0.75, color=(255, 255, 255)):
        cv2.putText(
            image,
            text,
            position,
            cv2.FONT_HERSHEY_COMPLEX,
            font_size,
            color,
            1,
        )

    def show(self, frame=None, frame_name="video"):
        if frame is None:
            cv2.imshow("image", self.cap)
            cv2.waitKey(0)  # waits until a key is pressed
        else:
            cv2.imshow(frame_name, frame)

    def write_video(self, output_path=".", filename="output_video.mp4"):
        out_video = cv2.VideoWriter(
            os.path.join(output_path, filename),
            cv2.VideoWriter_fourcc(*"avc1"),
            self.fps,
            (self.source_width, self.source_height),
            True,
        )
        return out_video

    def next_frame(self,
                   quit_key="q",
                   progress=True,
                   smoothing_window=30,
                   stabilize_video=False):
        """Returns the next image from either a video file or webcam."""
        while self.cap.isOpened():
            if progress:
                self.progress_bar.update(1)
            flag = False
            for _ in range(1):
                flag, frame = self.cap.read()

            if not flag:
                break
            if stabilize_video:
                # Pass frame to stabilizer even if frame is None
                frame = self._video_stabilizer.stabilize_frame(
                    input_frame=frame, smoothing_window=smoothing_window)
            yield frame

            key = cv2.waitKey(1) & 0xFF
            # if `quit_key` was pressed, break from the loop
            if key == ord(quit_key):
                break

    # TODO: Add context-manager to handle the closing
    def close(self):
        """Closes the VideoCapture."""
        if "image" not in self._input_type:
            self.cap.release()
            if self.progress_bar:
                self.progress_bar.close()
        cv2.destroyAllWindows()
        logger.info("============ CleanUp! ============")
Beispiel #4
0
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')  
Beispiel #6
0
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)
Beispiel #7
0
from vidstab.VidStab import VidStab

stabilizer = VidStab()
vidcap = cv2.VideoCapture('front_m108_12_f07_i0_0.mpg')

while True:
    grabbed_frame, frame = vidcap.read()

    if frame is not None:
        # Perform any pre-processing of frame before stabilization here
        pass

    # Pass frame to stabilizer even if frame is None
    # stabilized_frame will be an all black frame until iteration 30
    stabilized_frame = stabilizer.stabilize_frame(input_frame=frame,
                                                  smoothing_window=30)
    if stabilized_frame is None:
        # There are no more frames available to stabilize
        break

    # Perform any post-processing of stabilized frame here
    pass
Beispiel #8
0
    file_path = "../RawData/Video/" + args["Videopath"] + ".MOV"
    # print("Initializing Face Segmentation...")
    fvs = FileVideoStream(file_path).start()
    img_transform = transforms.Compose([
        transforms.Resize((256, 256)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225],
                             inplace=False)
    ])
    t = transforms.Resize(size=(256, 256))
    predictor = dlib.shape_predictor(
        "../shape_predictor_68_face_landmarks.dat")
    detector = dlib.get_frontal_face_detector()
    fvs = FileVideoStream(file_path).start()
    stabilizer = VidStab()
    prev_anchor = None
    prev_ROTATION = None
    MOTION_DIFF = 0
    frame_count = 0
    refreshed = False
    # ROTATION_ANGLE = 75
    tracker = ECOTracker(True)

    prevTrackPts = []
    nextTrackPts = []
    last_object = []
    kalman_points = []
    predict_points = []
    for i in range(68):
        predict_points.append((0.0, 0.0))
Beispiel #9
0
    "----Stabilizing video---\ninput movie: %s\nsmooth window: %s \nsmooth windowX: %s\nsmooth windowY: %s\nsmooth windowR: %s"
    % (
        movie,
        smoothWindow,
        XsmoothWindow,
        YsmoothWindow,
        RsmoothWindow,
    ))
print("gaussian Filter: %s" % gaussianFilter)

print(
    "generate Xforms: %s\ntransformDir: %s\noffsets: %s \noffsetX: %s\noffsetY: %s\nlock frame: %s \nlock frame X: %s\nlock frame Y: %s\nlock frame r: %s\noutput movie: %s \n"
    % (generateXforms, transformDir, offsets, offsetX, offsetY, lockFrame,
       lockFrameX, lockFrameY, lockFrameR, movie_smoothed))

stabilizer = VidStab()

####  Compute transforms
if generateXforms:
    print("---- Generating transforms")
    stabilizer.gen_transforms(input_path=movie,
                              smoothing_window=smoothWindow,
                              show_progress=True)

    if os.path.exists(transformDir):
        if not os.path.isdir(transformDir):
            print("%s exists but is not a directory" % transformDir)
            exit(0)
    else:
        os.mkdir(transformDir)
Beispiel #10
0
def stab(cropvid_name, stabvid_name):
    stabilizer = VidStab()
    stabilizer.stabilize(input_path=cropvid_name, output_path=stabvid_name)
Beispiel #11
0
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")