-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |