Advertisement
Guest User

Untitled

a guest
Nov 9th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import os
  2. import urllib.request
  3. from PIL import Image
  4. import numpy as np
  5.  
  6. # Provide a project ID and feature ID.
  7. PROJECT_ID = "clon7uqxl01st073p72a1a8q2"
  8. feature_id = "clonq7vmq01122a6e9tawqe9s"
  9.  
  10. # Create a directory to store the mask images
  11. output_directory = "masks"
  12. os.makedirs(output_directory, exist_ok=True)
  13.  
  14. # Number of frames (405 in your case)
  15. num_frames = 405
  16.  
  17. # Iterate through each frame and fetch the mask image
  18. for frame_number in range(1, num_frames + 1):
  19. mask_url = f"https://api.labelbox.com/api/v1/projects/{PROJECT_ID}/annotations/{feature_id}/index/{frame_number}/mask"
  20.  
  21. # Make the API request
  22. req = urllib.request.Request(mask_url, headers=client.headers)
  23.  
  24. # Open the URL and retrieve the image
  25. image = Image.open(urllib.request.urlopen(req))
  26.  
  27. # Check if the mask image is empty (all black)
  28. mask_data = np.array(image)
  29. if np.max(mask_data) == 0:
  30. print(f"Frame {frame_number} has no mask.")
  31. else:
  32. # Save the image with the frame number as the filename
  33. filename = os.path.join(output_directory, f"{frame_number}.png")
  34. image.save(filename, "PNG")
  35.  
  36. print("Mask images saved in the 'masks' directory.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement