Spaces:
Runtime error
Runtime error
Update app.py
Browse files43 unmodified lines444546474849505152535455565758596036 unmodified lines979899100101102103104105289 unmodified lines39539639739839940040140240340440540640740840941041141241341441541641741841942042143 unmodified linesreturn fpsdef preprocess_image(image):return image, gr.State([]), gr.State([]), image, gr.State()def preprocess_video_in(video_path):# Generate a unique ID based on the current date and timeunique_id = datetime.now().strftime('%Y%m%d%H%M%S')output_dir = f'frames_{unique_id}'# Create the output directoryos.makedirs(output_dir, exist_ok=True)# Open the video filecap = cv2.VideoCapture(video_path)36 unmodified linesif os.path.splitext(p)[-1] in [".jpg", ".jpeg", ".JPG", ".JPEG"]]scanned_frames.sort(key=lambda p: int(os.path.splitext(p)[0]))# 'image' is the first frame extracted from video_inreturn first_frame, gr.State([]), gr.State([]), first_frame, first_frame, output_dir, scanned_frames, None, None, gr.update(open=False)def get_point(point_type, tracking_points, trackings_input_label, first_frame_path, evt: gr.SelectData):print(f"You selected {evt.value} at {evt.index} from {evt.target}")289 unmodified linesoutput_video = gr.Video(visible=False)# output_result_mask = gr.Image()clear_points_btn.click(fn = preprocess_image,inputs = input_first_frame_image,outputs = [first_frame_path, tracking_points, trackings_input_label, points_map, stored_inference_state],queue=False)video_in.upload(fn = preprocess_video_in,inputs = [video_in],outputs = [first_frame_path, tracking_points, trackings_input_label, input_first_frame_image, points_map, video_frames_dir, scanned_frames, stored_inference_state, stored_frame_names, video_in_drawer],queue = False)points_map.select(fn = get_point,inputs = [point_type, tracking_points, trackings_input_label, first_frame_path],outputs = [tracking_points, trackings_input_label, points_map],queue = False)"""working_frame.change(fn = switch_working_frame,43 unmodified lines44454647484950515253545556575859606162636465666768697036 unmodified lines107108109110111112113114115116117118119120121122123124125126127289 unmodified lines41741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447543 unmodified linesreturn fpsdef preprocess_image(image):# we clean allreturn [image, # first_frame_path[], # tracking_points[], # trackings_input_labelimage, # points_mapNone # stored_inference_state]def preprocess_video_in(video_path):# Generate a unique ID based on the current date and timeunique_id = datetime.now().strftime('%Y%m%d%H%M%S')# Set directory with this ID to store video framesoutput_dir = f'frames_{unique_id}'# Create the output directoryos.makedirs(output_dir, exist_ok=True)### Process video frames #### Open the video filecap = cv2.VideoCapture(video_path)36 unmodified linesif os.path.splitext(p)[-1] in [".jpg", ".jpeg", ".JPG", ".JPEG"]]scanned_frames.sort(key=lambda p: int(os.path.splitext(p)[0]))print(f"SCANNED_FRAMES: {scanned_frames}")return [first_frame, # first_frame_pathgr.State([]), # tracking_pointsgr.State([]), # trackings_input_labelfirst_frame, # input_first_frame_imagefirst_frame, # points_mapoutput_dir, # video_frames_dirscanned_frames, # scanned_framesNone, # stored_inference_stateNone, # stored_frame_namesgr.update(open=False) # video_in_drawer]def get_point(point_type, tracking_points, trackings_input_label, first_frame_path, evt: gr.SelectData):print(f"You selected {evt.value} at {evt.index} from {evt.target}")289 unmodified linesoutput_video = gr.Video(visible=False)# output_result_mask = gr.Image()# When new video is uploadedvideo_in.upload(fn = preprocess_video_in,inputs = [video_in],outputs = [first_frame_path,tracking_points, # update Tracking Points in the gr.State([]) objecttrackings_input_label, # update Tracking Labels in the gr.State([]) objectinput_first_frame_image, # hidden component used as ref when clearing pointspoints_map, # Image component where we add new tracking pointsvideo_frames_dir, # Array where frames from video_in are deep storedscanned_frames, # Scanned frames by SAM2stored_inference_state, # Sam2 inference statestored_frame_names, #video_in_drawer, # Accordion to hide uploaded video player],queue = False)# triggered when we click on image to add new pointspoints_map.select(fn = get_point,inputs = [point_type, # "include" or "exclude"tracking_points, # get tracking_points valuestrackings_input_label, # get tracking label valuesfirst_frame_path, # gr.State() first frame path],outputs = [tracking_points, # updated with new pointstrackings_input_label, # updated with corresponding labelspoints_map, # updated image with points],queue = False)# Clear every points clicked and added to the mapclear_points_btn.click(fn = preprocess_image,inputs = input_first_frame_image, # we get the untouched hidden imageoutputs = [first_frame_path,tracking_points,trackings_input_label,points_map,stored_inference_state,],queue=False)"""working_frame.change(fn = switch_working_frame,
app.py+68-14