hysts HF staff commited on
Commit
49f5f36
1 Parent(s): 1109e54

Change how gr.State is used

Browse files
Files changed (1) hide show
  1. app.py +15 -29
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import json
2
  import os
 
3
 
4
  import cv2
5
  import gradio as gr
@@ -418,8 +419,8 @@ class ImageConductor:
418
 
419
 
420
  def reset_states(first_frame_path, tracking_points):
421
- first_frame_path = gr.State()
422
- tracking_points = gr.State([])
423
  return {input_image: None, first_frame_path_var: first_frame_path, tracking_points_var: tracking_points}
424
 
425
 
@@ -432,7 +433,7 @@ def preprocess_image(image, tracking_points):
432
  id = str(uuid.uuid4())[:4]
433
  first_frame_path = os.path.join(output_dir, f"first_frame_{id}.jpg")
434
  image_pil.save(first_frame_path, quality=95)
435
- tracking_points = gr.State([])
436
  return {
437
  input_image: first_frame_path,
438
  first_frame_path_var: first_frame_path,
@@ -449,26 +450,16 @@ def add_tracking_points(
449
  elif drag_mode == "camera":
450
  color = (0, 0, 255, 255)
451
 
452
- if not isinstance(tracking_points, list):
453
- print(f"You selected {evt.value} at {evt.index} from {evt.target}")
454
- tracking_points.value[-1].append(evt.index)
455
- print(tracking_points.value)
456
- tracking_points_values = tracking_points.value
457
- else:
458
- try:
459
- tracking_points[-1].append(evt.index)
460
- except Exception as e:
461
- tracking_points.append([])
462
- tracking_points[-1].append(evt.index)
463
- print(f"Solved Error: {e}")
464
-
465
- tracking_points_values = tracking_points
466
 
467
  transparent_background = Image.open(first_frame_path).convert("RGBA")
468
  w, h = transparent_background.size
469
  transparent_layer = np.zeros((h, w, 4))
470
 
471
- for track in tracking_points_values:
472
  if len(track) > 1:
473
  for i in range(len(track) - 1):
474
  start_point = track[i]
@@ -497,12 +488,7 @@ def add_tracking_points(
497
 
498
 
499
  def add_drag(tracking_points):
500
- if not isinstance(tracking_points, list):
501
- # print("before", tracking_points.value)
502
- tracking_points.value.append([])
503
- # print(tracking_points.value)
504
- else:
505
- tracking_points.append([])
506
  return {tracking_points_var: tracking_points}
507
 
508
 
@@ -511,11 +497,11 @@ def delete_last_drag(tracking_points, first_frame_path, drag_mode):
511
  color = (255, 0, 0, 255)
512
  elif drag_mode == "camera":
513
  color = (0, 0, 255, 255)
514
- tracking_points.value.pop()
515
  transparent_background = Image.open(first_frame_path).convert("RGBA")
516
  w, h = transparent_background.size
517
  transparent_layer = np.zeros((h, w, 4))
518
- for track in tracking_points.value:
519
  if len(track) > 1:
520
  for i in range(len(track) - 1):
521
  start_point = track[i]
@@ -548,11 +534,11 @@ def delete_last_step(tracking_points, first_frame_path, drag_mode):
548
  color = (255, 0, 0, 255)
549
  elif drag_mode == "camera":
550
  color = (0, 0, 255, 255)
551
- tracking_points.value[-1].pop()
552
  transparent_background = Image.open(first_frame_path).convert("RGBA")
553
  w, h = transparent_background.size
554
  transparent_layer = np.zeros((h, w, 4))
555
- for track in tracking_points.value:
556
  if len(track) > 1:
557
  for i in range(len(track) - 1):
558
  start_point = track[i]
@@ -603,7 +589,7 @@ with block:
603
  with gr.Row(equal_height=True):
604
  gr.Markdown(instructions)
605
 
606
- first_frame_path_var = gr.State(value=None)
607
  tracking_points_var = gr.State([])
608
 
609
  with gr.Row():
 
1
  import json
2
  import os
3
+ import uuid
4
 
5
  import cv2
6
  import gradio as gr
 
419
 
420
 
421
  def reset_states(first_frame_path, tracking_points):
422
+ first_frame_path = None
423
+ tracking_points = []
424
  return {input_image: None, first_frame_path_var: first_frame_path, tracking_points_var: tracking_points}
425
 
426
 
 
433
  id = str(uuid.uuid4())[:4]
434
  first_frame_path = os.path.join(output_dir, f"first_frame_{id}.jpg")
435
  image_pil.save(first_frame_path, quality=95)
436
+ tracking_points = []
437
  return {
438
  input_image: first_frame_path,
439
  first_frame_path_var: first_frame_path,
 
450
  elif drag_mode == "camera":
451
  color = (0, 0, 255, 255)
452
 
453
+ print(f"You selected {evt.value} at {evt.index} from {evt.target}")
454
+ if not tracking_points:
455
+ tracking_points = [[]]
456
+ tracking_points[-1].append(evt.index)
 
 
 
 
 
 
 
 
 
 
457
 
458
  transparent_background = Image.open(first_frame_path).convert("RGBA")
459
  w, h = transparent_background.size
460
  transparent_layer = np.zeros((h, w, 4))
461
 
462
+ for track in tracking_points:
463
  if len(track) > 1:
464
  for i in range(len(track) - 1):
465
  start_point = track[i]
 
488
 
489
 
490
  def add_drag(tracking_points):
491
+ tracking_points.append([])
 
 
 
 
 
492
  return {tracking_points_var: tracking_points}
493
 
494
 
 
497
  color = (255, 0, 0, 255)
498
  elif drag_mode == "camera":
499
  color = (0, 0, 255, 255)
500
+ tracking_points.pop()
501
  transparent_background = Image.open(first_frame_path).convert("RGBA")
502
  w, h = transparent_background.size
503
  transparent_layer = np.zeros((h, w, 4))
504
+ for track in tracking_points:
505
  if len(track) > 1:
506
  for i in range(len(track) - 1):
507
  start_point = track[i]
 
534
  color = (255, 0, 0, 255)
535
  elif drag_mode == "camera":
536
  color = (0, 0, 255, 255)
537
+ tracking_points[-1].pop()
538
  transparent_background = Image.open(first_frame_path).convert("RGBA")
539
  w, h = transparent_background.size
540
  transparent_layer = np.zeros((h, w, 4))
541
+ for track in tracking_points:
542
  if len(track) > 1:
543
  for i in range(len(track) - 1):
544
  start_point = track[i]
 
589
  with gr.Row(equal_height=True):
590
  gr.Markdown(instructions)
591
 
592
+ first_frame_path_var = gr.State()
593
  tracking_points_var = gr.State([])
594
 
595
  with gr.Row():