Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line234567891011121357 unmodified lines717273747576776 unmodified lines8485868788899016 unmodified lines1071081091101111121131141151161171181191 unmodified lineimport osos.environ["TORCH_CUDNN_SDPA_ENABLED"] = "1"import torchimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Imagefrom sam2.build_sam import build_sam2from sam2.sam2_image_predictor import SAM2ImagePredictor# use bfloat16 for the entire notebooktorch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()57 unmodified linesreturn masks_storedef sam_process(input_image):image = Image.open(input_image)image = np.array(image.convert("RGB"))6 unmodified linespredictor.set_image(image)input_point = np.array([[539, 384]])input_label = np.array([1])print(predictor._features["image_embed"].shape, predictor._features["image_embed"][-1].shape)16 unmodified linesreturn resultswith gr.Blocks() as demo:with gr.Column():input_image = gr.Image(label="input image", type="filepath")submit_btn = gr.Button("Submit")output_result = gr.Gallery()submit_btn.click(fn = sam_process,inputs = [input_image],outputs = [output_result])demo.launch()1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404157 unmodified lines991001011021031041056 unmodified lines11211311411511611711816 unmodified lines1351361371381391401411421431441451461471481491501511521531541551561571581591601 unmodified lineimport osos.environ["TORCH_CUDNN_SDPA_ENABLED"] = "1"import torchimport numpy asimport cv2import matplotlib.pyplot as pltfrom PIL import Image, ImageFilterfrom sam2.build_sam import build_sam2from sam2.sam2_image_predictor import SAM2ImagePredictordef preprocess_image(image):return image, gr.State, gr.Statedef get_point(tracking_points, trackings_input_label, first_frame_path, evt: gr.SelectData):print(f"You selected {evt.value} at {evt.index} from {evt.target}")tracking_points.value.append(evt.index)print(f"TRACKING POINT: {tracking_points.value}")trackings_input_label.value.append(1)print(f"TRACKING INPUT LABEL: {trackings_input_label.value}")# for SAM2# input_point = np.array(tracking_points.value)# print(f"SAM2 INPUT POINT: {input_point}")# input_label = np.array([1])transparent_background = Image.open(first_frame_path).convert('RGBA')w, h = transparent_background.sizetransparent_layer = np.zeros((h, w, 4))for track in tracking_points.value:cv2.circle(transparent_layer, track, 5, (255, 0, 0, 255), -1)transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8))selected_point_map = Image.alpha_composite(transparent_background, transparent_layer)return tracking_points, trackings_input_label, selected_point_map# use bfloat16 for the entire notebooktorch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()57 unmodified linesreturn masks_storedef sam_process(input_image, tracking_points, trackings_input_label):image = Image.open(input_image)image = np.array(image.convert("RGB"))6 unmodified linespredictor.set_image(image)input_point = np.array(tracking_points.value)input_label = np.array([1])print(predictor._features["image_embed"].shape, predictor._features["image_embed"][-1].shape)16 unmodified linesreturn resultswith gr.Blocks() as demo:first_frame_path = gr.State()tracking_points = gr.State([])trackings_input_label = gr.State([])with gr.Column():gr.Markdown("# SAM2 Image Predictor")with gr.Row():input_image = gr.Image(label="input image", interactive=True, type="filepath")with gr.Column():points_map = gr.Image(label="points map")submit_btn = gr.Button("Submit")output_result = gr.Gallery()input_image.upload(preprocess_image, input_image, [first_frame_path, tracking_points, trackings_input_label])input_image.select(get_point, [tracking_points, trackings_input_label, first_frame_path], [tracking_points, trackings_input_label, points_map])submit_btn.click(fn = sam_process,inputs = [input_image, tracking_points, trackings_input_label],outputs = [output_result])demo.launch()
app.py+49-8