Advertisement
Guest User

Untitled

a guest
Nov 7th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. from labelbox import Client
  2. from matplotlib import pyplot as plt
  3. from IPython.display import clear_output
  4. import numpy as np
  5. import json
  6. import ndjson
  7. import requests
  8. import cv2
  9. from typing import Dict, Any
  10. import os
  11. from typing import List, Dict
  12.  
  13. # Pick a video project with completed labels
  14. PROJECT_ID = ""
  15.  
  16. # Add your API key
  17. API_KEY = ""
  18. client = Client(api_key=API_KEY)
  19. project = client.get_project(PROJECT_ID)
  20.  
  21. export_url = project.export_labels()
  22. exports = requests.get(export_url).json()
  23.  
  24. # Grab the annotation URL
  25. annotation_url = exports[0]["Label"]["frames"]
  26. headers = {"Authorization": f"Bearer {API_KEY}"}
  27. annotations = ndjson.loads(requests.get(annotation_url, headers=headers).text)
  28. annotations = {annot["frameNumber"]: annot for annot in annotations}
  29.  
  30. # Store the video URL in a file in the current working directory
  31. video_url = exports[0]["Labeled Data"]
  32. with open("12.mp4", "wb") as file:
  33. file.write(requests.get(video_url).content)
  34.  
  35. vidcap = cv2.VideoCapture("12.mp4")
  36. count = 0
  37.  
  38. while True:
  39. success, image = vidcap.read()
  40.  
  41. if not success:
  42. break # Exit the loop when there are no more frames
  43.  
  44. image = image[:, :, ::-1] # Convert BGR to RGB
  45.  
  46. # Get the annotation data for the current frame
  47. annotation = annotations.get(count)
  48.  
  49. if annotation:
  50. # Extract the mask URL from the annotation
  51. mask_url = annotation["objects"][0]["instanceURI"]
  52.  
  53. # Download and process the mask
  54. mask_image = cv2.cvtColor(cv2.imread(mask_url), cv2.COLOR_BGR2GRAY)
  55.  
  56. # Resize the mask image to match the frame's dimensions
  57. mask_image = cv2.resize(mask_image, (image.shape[1], image.shape[0]))
  58.  
  59. # Create a binary mask where the mask region is non-zero
  60. binary_mask = np.zeros_like(mask_image)
  61. binary_mask[mask_image > 0] = 255
  62.  
  63. # Combine the binary mask with the frame using a bitwise operation
  64. masked_frame = cv2.bitwise_and(image, image, mask=binary_mask)
  65.  
  66. # Display the masked frame
  67. plt.imshow(masked_frame)
  68. plt.title(f"Frame {count}")
  69. plt.show()
  70.  
  71. count += 1
  72.  
  73. vidcap.release()
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement