daily pastebin goal
83%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 55 in 2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FramePlotter(_ObjectRequester):
  2.  
  3.     # The following attributes are global image attributes.
  4.     _frame_id = None         # bb_binary frame id.
  5.     _title = None            # Text to plot in the upper left corner.
  6.     _scale = None            # Resizing of the image prior to plotting.
  7.     _crop_coordinates = None # Allows displaying only a small part of the image.
  8.     _crop_mode = None        # Default "shift": keep cropping region valid, "crop": crop out smaller image at border.
  9.     _path_alpha = None       # The base transparency of the paths.
  10.     _raw = None              # Requests a near-lossless image without plotting.
  11.     _no_rotate = None        # The image won't be rotated to hive coordinates (use with raw=True).
  12.     _decode_all_frames = None # Whether to decode and cache the complete video.
  13.     _decode_n_frames = None  # Whether to decode and cache the subsequent n frames.
  14.  
  15.     # The following attributes are vectors.
  16.     _xs, _ys = None, None    # Positions of markers.
  17.     _angles = None           # For arrows: rotation of markers.
  18.     _sizes = None            # For circles: radius of circle.
  19.     _colors = None           # matplotlib colors.
  20.     _labels = None           # Text to print at each marker.
  21.  
  22.     # Internal attributes.
  23.     _paths = None            # Generated by VideoPlotter.track_labels.
  24.  
  25.     def __init__(self, **args):
  26.         """
  27.        Arguments:
  28.            frame_id: Database/bb_binary frame id of the image.
  29.            title: Text that will be displayed in the image (or "auto" for automatic title).
  30.            scale: Factor to resize the returned image (default = 0.5).
  31.            crop_coordinates: Image pixel coordinates of the region to crop.
  32.            crop_mode: "shift": Cropping regions that are out of the image bounds will be shifted (default),
  33.                        "crop": Crops at the image border will yield smaller subimages.
  34.            path_alpha: Opacity of the traced tracks (default = 0.25).
  35.            raw: Whether to return the raw image data as a numpy array without compressing it to JPEG.
  36.            no_rotate: Only with raw=True. Whether to return the image in the original (camera) orientation.
  37.            decode_all_frames: Whether to proactively decode and cache all frames of the video.
  38.            decode_n_frames: Whether to decode and cache the next n frames of the video.
  39.            xs: List of x coordinates (in image pixels).
  40.            ys: List of y coodinates (in image pixels).
  41.            angles: List of orientations of arrows to be drawn. Same order as xs/ys.
  42.            sizes: List of sizes (radius in pixels) of small circles for every point in xs/ys.
  43.            colors: List of matplotlib colors for the markers of the points xs/ys.
  44.            labels: Text to draw above the markers in xs/ys.
  45.        """
  46.         for property in ("xs", "ys",
  47.                          "angles", "sizes", "colors", "labels",
  48.                          "frame_id", "title", "scale", "crop_coordinates", "crop_mode",
  49.                          "path_alpha", "raw", "no_rotate", "decode_all_frames", "decode_n_frames"):
  50.             if property not in args:
  51.                 continue
  52.             setattr(self, "_" + property, args[property])
  53.        
  54.         if self._no_rotate and not self._raw:
  55.             raise ValueError("no_rotate=True requires raw=True.")
  56.         if self._crop_mode:
  57.             if self._crop_mode == "crop" and not self._raw:
  58.                 raise ValueError("crop_mode='crop' requires raw=True.")
  59.             elif self._crop_mode not in ("shift", "crop"):
  60.                 raise ValueError("crop_mode must be one of 'shift', 'crop'.")
  61.    
  62.     @classmethod
  63.     def from_dict(cls, data):
  64.         return cls(**data)
  65.     @classmethod
  66.     def from_json(cls, data_json):
  67.         data = json.loads(data_json)
  68.         return cls.from_dict(data)
  69.     def to_json(self):
  70.         return json.dumps(dict(self))
  71.  
  72.     # Retrieve all properties as (name, value) pairs.
  73.     # Used to convert to dictionary.
  74.     def __iter__(self):
  75.         def all_attributes():
  76.             yield "frame_id", self._frame_id
  77.             yield "xs", self._xs
  78.             yield "ys", self._ys
  79.             yield "angles", self._angles
  80.             yield "sizes", self._sizes
  81.             yield "colors", self._colors
  82.             yield "labels", self._labels
  83.             yield "title", self._title
  84.             yield "scale", self._scale
  85.             yield "crop_coordinates", self._crop_coordinates
  86.             yield "crop_mode", self._crop_mode
  87.             yield "path_alpha", self._path_alpha
  88.             yield "raw", self._raw
  89.             yield "no_rotate", self._no_rotate
  90.             yield "decode_all_frames", self._decode_all_frames
  91.             yield "decode_n_frames", self._decode_n_frames
  92.  
  93.         for (name, value) in all_attributes():
  94.             if value is not None:
  95.                 yield (name, value)
  96.  
  97.     def get_image(self):
  98.         """
  99.            Requests the image from the backend server.
  100.                
  101.            Returns:
  102.                numpy array containing the image
  103.        """
  104.         data = dict(frame_options=self.to_json())
  105.         buf = self.execute_request("plot_frame", data=data)
  106.         if not self._raw:
  107.             import matplotlib.pyplot as plt
  108.             return plt.imread(buf, format="JPG")
  109.         else:
  110.             import numpy as np
  111.             return np.load(buf, allow_pickle=False)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top