Update frame interval handling and frame_per slider to reflect exported image interval in video processing
Browse files37 unmodified lines3839404142435 unmodified lines4950515253545556575840 unmodified lines9910010110210310410536 unmodified lines142143144145146147148283 unmodified lines43243343443543643743825 unmodified lines464465466467468469470140 unmodified lines61161261361461561637 unmodified linestorch.cuda.empty_cache()return None, ({}, {}), None, None, 0, None, None, None, 0def get_meta_from_video(Seg_Tracker, input_video, scale_slider, checkpoint):output_dir = '/tmp/output_frames'5 unmodified linesif input_video is None:return None, ({}, {}), None, None, 0, None, None, None, 0cap = cv2.VideoCapture(input_video)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))cap.release()output_frames = int(total_frames * scale_slider)frame_interval = max(1, total_frames // output_frames)print(f"frame_interval: {frame_interval}")try:ffmpeg.input(input_video, hwaccel='cuda').output(40 unmodified linesimage_predictor = SAM2ImagePredictor(sam2_model)inference_state = predictor.init_state(video_path=output_dir)predictor.reset_state(inference_state)return (predictor, inference_state, image_predictor), ({}, {}), first_frame_rgb, first_frame_rgb, 0, None, None, None, 0def mask2bbox(mask):if len(np.where(mask > 0)[0]) == 0:36 unmodified linesrgb_color = tuple(map(int, (color[:3] * 255).astype(np.uint8)))inv_color = tuple(map(int, (255 - color[:3] * 255).astype(np.uint8)))x0, y0, x1, y1 = bboximage_with_rect = cv2.rectangle(image.copy(), (x0, y0), (x1, y1), inv_color, thickness=2)return image_with_rectdef sam_click(Seg_Tracker, frame_num, point_mode, click_stack, ann_obj_id, evt: gr.SelectData):283 unmodified lineswith gr.Row():checkpoint = gr.Dropdown(label="Model Size", choices=["tiny", "small", "base-plus", "large"], value="tiny")scale_slider = gr.Slider(label="Downsampe Frame Rate",minimum=0.0,maximum=1.0,step=0.25,25 unmodified lineswith gr.Row():with gr.Column():frame_per = gr.Slider(label = "Percentage of Frames Viewed",minimum= 0.0,maximum= 100.0,step=0.01,140 unmodified linesSeg_Tracker, input_first_frame, drawing_board, last_draw])app.queue(concurrency_count=1)app.launch(debug=True, enable_queue=True, share=False)37 unmodified lines38394041424344454647484950515253545556575859605 unmodified lines6667686970717273747540 unmodified lines11611711811912012112212312412512636 unmodified lines163164165166167168169283 unmodified lines45345445545645745845925 unmodified lines485486487488489490491140 unmodified lines63263363463563663763863964064164264337 unmodified linestorch.cuda.empty_cache()return None, ({}, {}), None, None, 0, None, None, None, 0def change_video(input_video):if input_video is None:return 0, 0cap = cv2.VideoCapture(input_video)fps = cap.get(cv2.CAP_PROP_FPS)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))cap.release()scale_slider = gr.Slider.update(minimum=1.0,maximum=fps,step=1.0,value=fps,)frame_per = gr.Slider.update(minimum= 0.0,maximum= total_frames / fps,step=1.0/fps,value=0.0,)return scale_slider, frame_perdef get_meta_from_video(Seg_Tracker, input_video, scale_slider, checkpoint):output_dir = '/tmp/output_frames'5 unmodified linesif input_video is None:return None, ({}, {}), None, None, 0, None, None, None, 0cap = cv2.VideoCapture(input_video)fps = cap.get(cv2.CAP_PROP_FPS)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))cap.release()frame_interval = max(1, int(fps // scale_slider))print(f"frame_interval: {frame_interval}")try:ffmpeg.input(input_video, hwaccel='cuda').output(40 unmodified linesimage_predictor = SAM2ImagePredictor(sam2_model)inference_state = predictor.init_state(video_path=output_dir)predictor.reset_state(inference_state)frame_per = gr.Slider.update(minimum= 0.0,maximum= total_frames / fps,step=frame_interval / fps,value=0.0,)return (predictor, inference_state, image_predictor), ({}, {}), first_frame_rgb, first_frame_rgb, frame_per, None, None, None, 0def mask2bbox(mask):if len(np.where(mask > 0)[0]) == 0:36 unmodified linesrgb_color = tuple(map(int, (color[:3] * 255).astype(np.uint8)))inv_color = tuple(map(int, (255 - color[:3] * 255).astype(np.uint8)))x0, y0, x1, y1 = bboximage_with_rect = cv2.rectangle(image.copy(), (x0, y0), (x1, y1), rgb_color, thickness=2)return image_with_rectdef sam_click(Seg_Tracker, frame_num, point_mode, click_stack, ann_obj_id, evt: gr.SelectData):283 unmodified lineswith gr.Row():checkpoint = gr.Dropdown(label="Model Size", choices=["tiny", "small", "base-plus", "large"], value="tiny")scale_slider = gr.Slider(label="Downsampe Frame Rate (fps)",minimum=0.0,maximum=1.0,step=0.25,25 unmodified lineswith gr.Row():with gr.Column():frame_per = gr.Slider(label = "Time (seconds)",minimum= 0.0,maximum= 100.0,step=0.01,140 unmodified linesSeg_Tracker, input_first_frame, drawing_board, last_draw])input_video.change(fn=change_video,inputs=[input_video],outputs=[scale_slider, frame_per])app.queue(concurrency_count=1)app.launch(debug=True, enable_queue=True, share=False)
app.py+33-6