aharley commited on
Commit
fddd61d
·
verified ·
1 Parent(s): 535a6e0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +1 -0
  2. demo_dense_visualize.py +50 -0
app.py CHANGED
@@ -69,6 +69,7 @@ tracker = Tracker(
69
  stride=8,
70
  inference_iters=4,
71
  target_res=1024,
 
72
  )
73
 
74
  # -------------------- Step 1: Extract the First Frame -------------------- #
 
69
  stride=8,
70
  inference_iters=4,
71
  target_res=1024,
72
+ device=device,
73
  )
74
 
75
  # -------------------- Step 1: Extract the First Frame -------------------- #
demo_dense_visualize.py CHANGED
@@ -1,9 +1,17 @@
1
  import os
2
  import random
3
  import torch
 
 
4
  import sys
 
5
  import torch.nn.functional as F
6
  import numpy as np
 
 
 
 
 
7
 
8
  import utils.loss
9
  import utils.samp
@@ -11,10 +19,16 @@ import utils.data
11
  import utils.improc
12
  import utils.misc
13
  import utils.saveload
 
 
 
14
  import cv2
15
  import imageio
16
  from nets.blocks import InputPadder
 
 
17
  from utils.visualizer import Visualizer
 
18
 
19
  import torch
20
  import requests
@@ -43,6 +57,42 @@ def run_example(processor, model, task_prompt, image, text_input=None):
43
 
44
  return parsed_answer
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  class Tracker:
47
  def __init__(self, model, mean, std, S, stride, inference_iters, target_res, device='cuda'):
48
  """
 
1
  import os
2
  import random
3
  import torch
4
+ import signal
5
+ import socket
6
  import sys
7
+ import json
8
  import torch.nn.functional as F
9
  import numpy as np
10
+ import argparse
11
+ from pathlib import Path
12
+ import torch.optim as optim
13
+ from torch.cuda.amp import GradScaler
14
+ from lightning_fabric import Fabric
15
 
16
  import utils.loss
17
  import utils.samp
 
19
  import utils.improc
20
  import utils.misc
21
  import utils.saveload
22
+ from tensorboardX import SummaryWriter
23
+ import datetime
24
+ import time
25
  import cv2
26
  import imageio
27
  from nets.blocks import InputPadder
28
+ from tqdm import tqdm
29
+ # from pytorch_lightning.callbacks import BaseFinetuning
30
  from utils.visualizer import Visualizer
31
+ from torchvision.transforms.functional import resize
32
 
33
  import torch
34
  import requests
 
57
 
58
  return parsed_answer
59
 
60
+
61
+ def polygons_to_mask(image, prediction, fill_value=255):
62
+ """
63
+ Converts polygons into a mask.
64
+
65
+ Parameters:
66
+ - image: A PIL Image instance whose size will be used for the mask.
67
+ - prediction: Dictionary containing 'polygons' and 'labels'.
68
+ 'polygons' is a list where each element is a list of sub-polygons.
69
+ - fill_value: The pixel value used to fill the polygon areas (default 255 for a binary mask).
70
+
71
+ Returns:
72
+ - A NumPy array representing the mask (same width and height as the input image).
73
+ """
74
+ # Create a blank grayscale mask image with the same size as the original image.
75
+ mask = Image.new('L', image.size, 0)
76
+ draw = ImageDraw.Draw(mask)
77
+
78
+ # Iterate over each set of polygons
79
+ for polygons in prediction['polygons']:
80
+ # Each element in "polygons" can be a sub-polygon
81
+ for poly in polygons:
82
+ # Ensure the polygon is in the right shape and has at least 3 points.
83
+ poly_arr = np.array(poly).reshape(-1, 2)
84
+ if poly_arr.shape[0] < 3:
85
+ print('Skipping invalid polygon:', poly_arr)
86
+ continue
87
+ # Convert the polygon vertices into a list for drawing.
88
+ poly_list = poly_arr.reshape(-1).tolist()
89
+ # Draw the polygon on the mask with the fill_value.
90
+ draw.polygon(poly_list, fill=fill_value)
91
+
92
+ # Convert the PIL mask image to a NumPy array and return it.
93
+ return np.array(mask)
94
+
95
+
96
  class Tracker:
97
  def __init__(self, model, mean, std, S, stride, inference_iters, target_res, device='cuda'):
98
  """