Spaces:
Build error
Build error
Commit ·
53a8438
1
Parent(s): 9b9cd68
requirement fix
Browse filesFiles changed (9)
12 unmodified lines13141516171819202122232425180 unmodified lines20620720820921021121213 unmodified lines22622722822923023123223333 unmodified lines26726826927027127227340 unmodified lines3143153163173183193203213083093223233243253263273283293303313323333343353363373383393403163413423433193203443453461 unmodified line34834935035135235335435535635735835925 unmodified lines3853863873593883893903913922 unmodified lines395396397368398399400401162 unmodified lines56456556653756756856957012 unmodified linesimport jsonimport torchvisionimport torch from tools.interact_tools import SamControlerfrom tracker.base_tracker import BaseTrackerfrom tools.painter import mask_paintertry: from mmcv.cnn import ConvModuleexcept: os.system("mim install mmcv")
# download checkpointsdef download_checkpoint(url, folder, filename):180 unmodified lines
# tracking vosdef vos_tracking_video(video_state, interactive_state, mask_dropdown):
model.xmem.clear_memory() if interactive_state["track_end_number"]: following_frames = video_state["origin_images"][video_state["select_frame_number"]:interactive_state["track_end_number"]]13 unmodified lines template_mask = video_state["masks"][video_state["select_frame_number"]] fps = video_state["fps"] masks, logits, painted_images = model.generator(images=following_frames, template_mask=template_mask) # clear GPU memory model.xmem.clear_memory()
if interactive_state["track_end_number"]: video_state["masks"][video_state["select_frame_number"]:interactive_state["track_end_number"]] = masks33 unmodified lines
# inpaint def inpaint_video(video_state, interactive_state, mask_dropdown):
frames = np.asarray(video_state["origin_images"]) fps = video_state["fps"] inpaint_masks = np.asarray(video_state["masks"])40 unmodified lines torchvision.io.write_video(output_path, frames, fps=fps, video_codec="libx264") return output_path
# args, defined in track_anything.pyargs = parse_augment()
# check and download checkpoints if neededSAM_checkpoint = "sam_vit_h_4b8939.pth" sam_checkpoint_url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"SAM_checkpoint_dict = { 'vit_h': "sam_vit_h_4b8939.pth", 'vit_l': "sam_vit_l_0b3195.pth", "vit_b": "sam_vit_b_01ec64.pth"}SAM_checkpoint_url_dict = { 'vit_h': "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth", 'vit_l': "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth", 'vit_b': "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"}sam_checkpoint = SAM_checkpoint_dict[args.sam_model_type] sam_checkpoint_url = SAM_checkpoint_url_dict[args.sam_model_type] xmem_checkpoint = "XMem-s012.pth"xmem_checkpoint_url = "https://github.com/hkchengrex/XMem/releases/download/v1.0/XMem-s012.pth"e2fgvi_checkpoint = "E2FGVI-HQ-CVPR22.pth"e2fgvi_checkpoint_id = "10wGdKSUOie0XmCr8SQ2A2FeDe-mfn5w3"
folder ="./checkpoints"SAM_checkpoint = download_checkpoint(sam_checkpoint_url, folder, SAM_checkpoint)SAM_checkpoint = download_checkpoint(sam_checkpoint_url, folder, sam_checkpoint)xmem_checkpoint = download_checkpoint(xmem_checkpoint_url, folder, xmem_checkpoint)e2fgvi_checkpoint = download_checkpoint_from_google_drive(e2fgvi_checkpoint_id, folder, e2fgvi_checkpoint)# args, defined in track_anything.pyargs = parse_augment()# args.port = 12315# args.device = "cuda:2"# args.mask_save = True1 unmodified line# initialize sam, xmem, e2fgvi modelsmodel = TrackingAnything(SAM_checkpoint, xmem_checkpoint, e2fgvi_checkpoint,args)
title = """<p><h1 align="center">Track-Anything</h1></p> """description = """<p>Gradio demo for Track Anything, a flexible and interactive tool for video object tracking, segmentation, and inpainting. I To use it, simply upload your video, or click one of the examples to load them. Code: <a href="https://github.com/gaomingqi/Track-Anything">https://github.com/gaomingqi/Track-Anything</a> <a href="https://huggingface.co/spaces/watchtowerss/Track-Anything?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>"""
with gr.Blocks() as iface: """ state for 25 unmodified lines "fps": 30 } )
gr.Markdown(title) gr.Markdown(description) with gr.Row():
# for user video input2 unmodified lines video_input = gr.Video(autosize=True) with gr.Column(): video_info = gr.Textbox() video_info = gr.Textbox(value="Due to server restrictions, please upload a video that is no longer than 2 minutes. If you want to use the inpaint function, it is best to download and use a machine with more VRAM locally. \ resize_info = gr.Textbox(value="Due to server restrictions, please upload a video that is no longer than 2 minutes. If you want to use the inpaint function, it is best to download and use a machine with more VRAM locally. \ Alternatively, you can use the resize ratio slider to scale down the original image to around 360P resolution for faster processing.") resize_ratio_slider = gr.Slider(minimum=0.02, maximum=1, step=0.02, value=1, label="Resize ratio", visible=True) 162 unmodified lines # cache_examples=True, ) iface.queue(concurrency_count=1)iface.launch(debug=True, enable_queue=True)iface.launch(debug=True)
6 unmodified lines789101112131453 unmodified lines6869706971727374757677767778798081826 unmodified lines89909190929394956 unmodified linesimport cv2import importlibimport numpy as npfrom tqdm import tqdm
from inpainter.util.tensor_util import resize_frames, resize_masks
53 unmodified lines if ratio == 1: size = None else: size = (int(W*ratio), int(H*ratio)) size = [int(W*ratio), int(H*ratio)] if size[0] % 2 > 0: size[0] += 1 if size[1] % 2 > 0: size[1] += 1 masks = np.expand_dims(masks, axis=3) # expand to T, H, W, 1 binary_masks = resize_masks(masks, size) frames = resize_frames(frames, size) # T, H, W, 3 binary_masks = resize_masks(masks, tuple(size)) frames = resize_frames(frames, tuple(size)) # T, H, W, 3 # frames and binary_masks are numpy arrays
h, w = frames.shape[1:3]6 unmodified lines imgs, masks = imgs.to(self.device), masks.to(self.device) comp_frames = [None] * video_length
for f in range(0, video_length, self.neighbor_stride): for f in tqdm(range(0, video_length, self.neighbor_stride), desc='Inpainting image'): neighbor_ids = [ i for i in range(max(0, f - self.neighbor_stride), min(video_length, f + self.neighbor_stride + 1))127 unmodified lines128129130131132133134135136137127 unmodified lines windows: (B*num_windows, T*window_size*window_size, C) """ B, T, H, W, C = x.shape
x = x.view(B, T, H // window_size[0], window_size[0], W // window_size[1], window_size[1], C)
windows = x.permute(0, 2, 4, 1, 3, 5, 6).contiguous().view( -1, T * window_size[0] * window_size[1], C) return windows9 unmodified lines1011121314151314181915169 unmodified linesopencv-pythonpycocotoolsmatplotlibonnxruntimeonnxmetaseg==0.6.1pyyamlavmmcv-fullmmengineopenmimtqdm11234565 unmodified lines12131415161713141518192021222320 unmodified lines44454642474849505 unmodified lines56575854596061import PIL import PILfrom tqdm import tqdm
from tools.interact_tools import SamControlerfrom tracker.base_tracker import BaseTrackerfrom inpainter.base_inpainter import BaseInpainter5 unmodified linesclass TrackingAnything(): def __init__(self, sam_checkpoint, xmem_checkpoint, e2fgvi_checkpoint, args): self.args = args self.sam_checkpoint = sam_checkpoint self.xmem_checkpoint = xmem_checkpoint self.e2fgvi_checkpoint = e2fgvi_checkpoint self.samcontroler = SamControler(sam_checkpoint, args.sam_model_type, args.device) self.xmem = BaseTracker(xmem_checkpoint, device=args.device) self.baseinpainter = BaseInpainter(e2fgvi_checkpoint, args.device) self.samcontroler = SamControler(self.sam_checkpoint, args.sam_model_type, args.device) self.xmem = BaseTracker(self.xmem_checkpoint, device=args.device) self.baseinpainter = BaseInpainter(self.e2fgvi_checkpoint, args.device) # def inference_step(self, first_flag: bool, interact_flag: bool, image: np.ndarray, # same_image_flag: bool, points:np.ndarray, labels: np.ndarray, logits: np.ndarray=None, multimask=True): # if first_flag:20 unmodified lines masks = [] logits = [] painted_images = [] for i in range(len(images)): for i in tqdm(range(len(images)), desc="Tracking image"): if i ==0: mask, logit, painted_image = self.xmem.track(images[i], template_mask) masks.append(mask)5 unmodified lines masks.append(mask) logits.append(logit) painted_images.append(painted_image) print("tracking image {}".format(i)) return masks, logits, painted_images 125 unmodified lines126127128129130131132125 unmodified lines def clear_memory(self): self.tracker.clear_memory() self.mapper.clear_labels() torch.cuda.empty_cache()
## how to use:app.py+38-8
assets/avengers.gif

Large File Pointer Details
- SHA256: 9193a028c2e968ff7a7ee222ccc27166a5fbbe40a4d971cee13eba519134c5cf
- Pointer size: 133 Bytes
- Size of remote file: 99.2 MB

Large File Pointer Details
- SHA256: 5e07b86ee4cf002b3481c71e2038c03f4420883c3be78220dafbc4b59abfb32d
- Pointer size: 133 Bytes
- Size of remote file: 30 MB
inpainter/.DS_Store
inpainter/base_inpainter.py+6-4
inpainter/model/modules/tfocal_transformer_hq.py+2
requirements.txt+2-5
track_anything.py+10-6
tracker/.DS_Store
tracker/base_tracker.py+1