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! ============")
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()
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) if(isFirst): isFirst = False else: frameGray, points_nxt, pointsInterest_nxt, pointsInterest_count = calculateOpticalFlow(alignedFrameCurr, alignedFrameRef, points, pointsInterest_curr, pointsInterest_count, mask, lk_params) points = points_nxt pointsInterest_curr = pointsInterest_nxt cv2.imshow('frame', frameGray) if cv2.waitKey(25) & 0xFF == ord('q'): break
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
out_frame = frame[x1:x2, y1:y2] canvas = 225 * np.ones( (int(h / 2) + 41, int(h / 2) + 41, 3), np.uint8) canvas[max(0, -int(y / 2 - 20)):(max(0, -int(y / 2 - 20)) + out_frame.shape[0]), max(0, -int(x / 2 - 20)):(max(0, -int(x / 2 - 20)) + out_frame.shape[1])] = out_frame pts1 = np.float32([[0, 0], [0, canvas.shape[0]], [canvas.shape[0], canvas.shape[0]], [canvas.shape[0], 0]]) pts2 = np.float32([[0, 0], [0, 250], [250, 250], [250, 0]]) M = cv2.getPerspectiveTransform(pts1, pts2) dst = cv2.warpPerspective(canvas, M, (250, 250)) # Display the resulting frame dst = stabilizer.stabilize_frame(input_frame=dst, smoothing_window=30, border_type='replicate') if frame_count < 31: continue dst = dst[13:237, 13:237] video.write(dst) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows() fvs.stop()
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") resized = cv2.resize(stabilized_frame, dim, interpolation = cv2.INTER_AREA) #crop = image[50:600,20:500] croped = resized[crop_y:new_height, crop_x:new_width] #cv2.imshow('Video Capture',croped) #cv2.imshow('Orignal',resized) if cv2.waitKey(1) & 0xFF == ord('q'): # press q to quit break if stabilized_frame is None: # There are no more frames available to stabilize break vid.write(croped)