Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -44,17 +44,27 @@ def get_video_fps(video_path):
|
|
44 |
return fps
|
45 |
|
46 |
def preprocess_image(image):
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
def preprocess_video_in(video_path):
|
50 |
|
51 |
# Generate a unique ID based on the current date and time
|
52 |
unique_id = datetime.now().strftime('%Y%m%d%H%M%S')
|
|
|
|
|
53 |
output_dir = f'frames_{unique_id}'
|
54 |
|
55 |
# Create the output directory
|
56 |
os.makedirs(output_dir, exist_ok=True)
|
57 |
-
|
|
|
58 |
# Open the video file
|
59 |
cap = cv2.VideoCapture(video_path)
|
60 |
|
@@ -97,9 +107,21 @@ def preprocess_video_in(video_path):
|
|
97 |
if os.path.splitext(p)[-1] in [".jpg", ".jpeg", ".JPG", ".JPEG"]
|
98 |
]
|
99 |
scanned_frames.sort(key=lambda p: int(os.path.splitext(p)[0]))
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
def get_point(point_type, tracking_points, trackings_input_label, first_frame_path, evt: gr.SelectData):
|
105 |
print(f"You selected {evt.value} at {evt.index} from {evt.target}")
|
@@ -395,27 +417,59 @@ with gr.Blocks() as demo:
|
|
395 |
output_video = gr.Video(visible=False)
|
396 |
# output_result_mask = gr.Image()
|
397 |
|
398 |
-
clear_points_btn.click(
|
399 |
-
fn = preprocess_image,
|
400 |
-
inputs = input_first_frame_image,
|
401 |
-
outputs = [first_frame_path, tracking_points, trackings_input_label, points_map, stored_inference_state],
|
402 |
-
queue=False
|
403 |
-
)
|
404 |
|
|
|
|
|
405 |
video_in.upload(
|
406 |
fn = preprocess_video_in,
|
407 |
inputs = [video_in],
|
408 |
-
outputs = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
409 |
queue = False
|
410 |
)
|
411 |
|
|
|
|
|
412 |
points_map.select(
|
413 |
fn = get_point,
|
414 |
-
inputs = [
|
415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
queue = False
|
417 |
)
|
418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
"""
|
420 |
working_frame.change(
|
421 |
fn = switch_working_frame,
|
|
|
44 |
return fps
|
45 |
|
46 |
def preprocess_image(image):
|
47 |
+
# we clean all
|
48 |
+
return [
|
49 |
+
image, # first_frame_path
|
50 |
+
[], # tracking_points
|
51 |
+
[], # trackings_input_label
|
52 |
+
image, # points_map
|
53 |
+
None # stored_inference_state
|
54 |
+
]
|
55 |
|
56 |
def preprocess_video_in(video_path):
|
57 |
|
58 |
# Generate a unique ID based on the current date and time
|
59 |
unique_id = datetime.now().strftime('%Y%m%d%H%M%S')
|
60 |
+
|
61 |
+
# Set directory with this ID to store video frames
|
62 |
output_dir = f'frames_{unique_id}'
|
63 |
|
64 |
# Create the output directory
|
65 |
os.makedirs(output_dir, exist_ok=True)
|
66 |
+
|
67 |
+
### Process video frames ###
|
68 |
# Open the video file
|
69 |
cap = cv2.VideoCapture(video_path)
|
70 |
|
|
|
107 |
if os.path.splitext(p)[-1] in [".jpg", ".jpeg", ".JPG", ".JPEG"]
|
108 |
]
|
109 |
scanned_frames.sort(key=lambda p: int(os.path.splitext(p)[0]))
|
110 |
+
print(f"SCANNED_FRAMES: {scanned_frames}")
|
111 |
+
|
112 |
+
|
113 |
+
return [
|
114 |
+
first_frame, # first_frame_path
|
115 |
+
gr.State([]), # tracking_points
|
116 |
+
gr.State([]), # trackings_input_label
|
117 |
+
first_frame, # input_first_frame_image
|
118 |
+
first_frame, # points_map
|
119 |
+
output_dir, # video_frames_dir
|
120 |
+
scanned_frames, # scanned_frames
|
121 |
+
None, # stored_inference_state
|
122 |
+
None, # stored_frame_names
|
123 |
+
gr.update(open=False) # video_in_drawer
|
124 |
+
]
|
125 |
|
126 |
def get_point(point_type, tracking_points, trackings_input_label, first_frame_path, evt: gr.SelectData):
|
127 |
print(f"You selected {evt.value} at {evt.index} from {evt.target}")
|
|
|
417 |
output_video = gr.Video(visible=False)
|
418 |
# output_result_mask = gr.Image()
|
419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
|
421 |
+
|
422 |
+
# When new video is uploaded
|
423 |
video_in.upload(
|
424 |
fn = preprocess_video_in,
|
425 |
inputs = [video_in],
|
426 |
+
outputs = [
|
427 |
+
first_frame_path,
|
428 |
+
tracking_points, # update Tracking Points in the gr.State([]) object
|
429 |
+
trackings_input_label, # update Tracking Labels in the gr.State([]) object
|
430 |
+
input_first_frame_image, # hidden component used as ref when clearing points
|
431 |
+
points_map, # Image component where we add new tracking points
|
432 |
+
video_frames_dir, # Array where frames from video_in are deep stored
|
433 |
+
scanned_frames, # Scanned frames by SAM2
|
434 |
+
stored_inference_state, # Sam2 inference state
|
435 |
+
stored_frame_names, #
|
436 |
+
video_in_drawer, # Accordion to hide uploaded video player
|
437 |
+
],
|
438 |
queue = False
|
439 |
)
|
440 |
|
441 |
+
|
442 |
+
# triggered when we click on image to add new points
|
443 |
points_map.select(
|
444 |
fn = get_point,
|
445 |
+
inputs = [
|
446 |
+
point_type, # "include" or "exclude"
|
447 |
+
tracking_points, # get tracking_points values
|
448 |
+
trackings_input_label, # get tracking label values
|
449 |
+
first_frame_path, # gr.State() first frame path
|
450 |
+
],
|
451 |
+
outputs = [
|
452 |
+
tracking_points, # updated with new points
|
453 |
+
trackings_input_label, # updated with corresponding labels
|
454 |
+
points_map, # updated image with points
|
455 |
+
],
|
456 |
queue = False
|
457 |
)
|
458 |
|
459 |
+
# Clear every points clicked and added to the map
|
460 |
+
clear_points_btn.click(
|
461 |
+
fn = preprocess_image,
|
462 |
+
inputs = input_first_frame_image, # we get the untouched hidden image
|
463 |
+
outputs = [
|
464 |
+
first_frame_path,
|
465 |
+
tracking_points,
|
466 |
+
trackings_input_label,
|
467 |
+
points_map,
|
468 |
+
stored_inference_state,
|
469 |
+
],
|
470 |
+
queue=False
|
471 |
+
)
|
472 |
+
|
473 |
"""
|
474 |
working_frame.change(
|
475 |
fn = switch_working_frame,
|