Advertisement
Guest User

Untitled

a guest
Nov 9th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 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 # Import List and Dict types from the typing module
  12.  
  13. #Pick a video project with completed labels
  14. PROJECT_ID = "clon7uqxl01st073p72a1a8q2"
  15.  
  16. #Add your api key
  17. API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbG83NTZmMGQwOWNuMDcyYjlxemI0Ym9kIiwib3JnYW5pemF0aW9uSWQiOiJjbG83NTZlenEwOWNtMDcyYmVwOHoyODBjIiwiYXBpS2V5SWQiOiJjbG9wcmx3dTMwM3Z0MDc0czlwZmY0ZHJxIiwic2VjcmV0IjoiNWQ5OTUxMGFiMjI5MWQ5YzNkZTc2MTZhNzE5YTA3MDQiLCJpYXQiOjE2OTk0NDgyNDgsImV4cCI6MjMzMDYwMDI0OH0.nHr_hPI1PVtBapLwya16aaMefzQ3JV9U3pMbTWPNsds"
  18. client = Client(api_key = API_KEY)
  19. project = client.get_project(PROJECT_ID)
  20.  
  21. export_url = project.export_labels()
  22.  
  23. print(export_url)
  24.  
  25. exports = requests.get(export_url).json()
  26.  
  27. type(exports)
  28.  
  29. exports[0]
  30.  
  31. #View some specific fields of the export
  32. print("LAbel ID:", exports[0]["DataRow ID"])
  33. print("Created By:", exports[0]["Created By"])
  34.  
  35. #View the video in your browser by clicking on the link produced
  36. video_url = exports[0]["Labeled Data"]
  37. print(video_url)
  38.  
  39. #Grab the annotation url
  40. annotation_url = exports[0]["Label"]["frames"]
  41. print(annotation_url) #Gives a link and downloads a json file https://api.labelbox.com/v1/frames/clonitedm0qkl070ncjmr5xgu
  42.  
  43. #Provide the appropriate authorization to view the labeled frames
  44. headers = {"Authorization": f"Bearer {API_KEY}"}
  45. annotations = ndjson.loads(requests.get(annotation_url, headers = headers).text)
  46.  
  47. #Make the data easy to lookup by assigning each frame its frame number
  48. annotations = {annot["frameNumber"]: annot for annot in annotations}
  49.  
  50. #Grab the first frame and print the contents
  51. first_frame = annotations[1]
  52. print(json.dumps(first_frame, indent = 2))
  53.  
  54. # Store the video URL in a file in the current working directory
  55. with open("12.mp4", "wb") as file:
  56. file.write(requests.get(video_url).content)
  57.  
  58. vidcap = cv2.VideoCapture("12.mp4")
  59. success, image = vidcap.read()
  60. image = image[:, :, ::-1]
  61. #Note that frameNumber 1 in the annotation is frame index 0
  62. count = 1
  63. while success and count < 20:
  64. plt.figure(1)
  65. plt.imshow(image)
  66. plt.title("frameNumber" + str(count))
  67. plt.pause(0.25)
  68. plt.clf()
  69. success, image = vidcap.read()
  70. count += 1
  71. if success and count < 20:
  72. clear_output(wait = True)
  73. image = image[:, :, ::-1]
  74.  
  75. # Helper function to visualize the segmentation masks
  76. def visualize_bbox(image: np.ndarray, tool: Dict[str, Any]) -> np.ndarray:
  77. start = (int(tool["bbox"]["left"]), int(tool["bbox"]["top"]))
  78. end = (int(tool["bbox"]["left"] + tool["bbox"]["width"]),
  79. int(tool["bbox"]["top"] + tool["bbox"]["height"]))
  80. return cv2.rectangle(image, start, end, (255, 0, 0), 5)
  81.  
  82. #Overlay Annotations
  83.  
  84. vidcap = cv2.VideoCapture("12.mp4")
  85. success, image = vidcap.read()
  86. image = image[:, :, ::-1]
  87. # Note that frameNumber 1 in the annotation is frame index 0
  88. count = 1
  89.  
  90. while success and count < 20:
  91. annotation = annotations.get(count)
  92. if annotation is not None:
  93. for tool in annotation["objects"]:
  94. if "bbox" in tools:
  95. image = visualize_bbox(image.astype(np.uint8), annotation.get("objects", []))
  96.  
  97. plt.figure(1)
  98. plt.imshow(image)
  99. plt.title("frameNumber " + str(count))
  100. plt.pause(0.25)
  101. plt.clf()
  102.  
  103. success, image = vidcap.read()
  104. count += 1
  105. if success and count < 20:
  106. clear_output(wait=True)
  107. image = image[:, :, ::-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement