Skip to content

Commit

Permalink
Create getting_started.ipynb
Browse files Browse the repository at this point in the history
First version of example
  • Loading branch information
siddharthksah committed Jul 14, 2023
1 parent 32cfe0f commit b8256dd
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions notebook/getting_started.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Start the timer
import time
start_time = time.time()

import numpy as np
import matplotlib.pyplot as plt
import requests
from PIL import Image
from io import BytesIO
from lang_sam import LangSAM

model = LangSAM()
image_url = "https://static01.nyt.com/images/2020/09/08/well/physed-cycle-walk/physed-cycle-walk-videoSixteenByNineJumbo1600-v2.jpg"
response = requests.get(image_url)
image_pil = Image.open(BytesIO(response.content)).convert("RGB")
text_prompt = "bicycle"

masks, boxes, phrases, logits = model.predict(image_pil, text_prompt)

# Convert masks to numpy arrays
masks_np = [mask.squeeze().cpu().numpy() for mask in masks]

# Visualize the masks
if len(masks_np) > 1:
fig, axes = plt.subplots(1, len(masks_np), figsize=(12, 4))
for i, mask in enumerate(masks_np):
axes[i].imshow(mask, cmap='gray')
axes[i].set_title('Mask {}'.format(i+1))
axes[i].axis('off')
else:
fig, ax = plt.subplots(figsize=(6, 4))
ax.imshow(masks_np[0], cmap='gray')
ax.set_title('Mask')
ax.axis('off')

# Save the mask as an image in the current working directory
mask_path = 'image_mask.png'
mask_image = Image.fromarray((masks_np[0] * 255).astype(np.uint8))
mask_image.save(mask_path)

plt.show()

# Print other variables
print("Boxes:", boxes)
print("Phrases:", phrases)
print("Logits:", logits)

# Calculate the elapsed time
elapsed_time = time.time() - start_time
print("Elapsed Time:", elapsed_time, "seconds")

0 comments on commit b8256dd

Please sign in to comment.