コード例 #1
0
ファイル: primitives.py プロジェクト: saiprakash-c/504
    def process_video(self, input_path, masks_path=None, video_path=None):
        '''Detect edges using self.detector.

        Args:
            input_path: the input video path
            masks_path: an optional path to write the per-frame edge masks (as
                boolean arrays) in .npy files
            video_path: an optional path to write the edges video
        '''
        # Ensure output directories exist
        if masks_path:
            etau.ensure_basedir(masks_path)
        # VideoProcessor ensures that the output video directory exists

        self.reset()
        with etav.VideoProcessor(input_path, out_video_path=video_path) as p:
            for img in p:
                # Compute edges
                edges = self.process_frame(img)

                if masks_path:
                    # Write edges mask
                    edges_bool = edges.astype(np.bool)
                    np.save(masks_path % p.frame_number, edges_bool)

                if video_path:
                    # Write edges video
                    p.write(cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB))
コード例 #2
0
    def write(self, path, url, params=None):
        '''Writes the URL content to the given local path.

        The download is performed in chunks, so arbitrarily large files can
        be downloaded. The output directory is created, if necessary.

        Args:
            path: the output path
            url: the URL to get
            params: optional dictionary of parameters for the URL

        Raises:
            WebSessionError: if the download failed
        '''
        r = self._get_streaming_response(url, params=params)
        etau.ensure_basedir(path)

        num_bytes = 0
        start_time = time.time()
        with open(path, "wb") as f:
            for chunk in r.iter_content(None):
                num_bytes += len(chunk)
                f.write(chunk)

        time_elapsed = time.time() - start_time
        _log_download_stats(num_bytes, time_elapsed)
コード例 #3
0
ファイル: diagram.py プロジェクト: saiprakash-c/504
 def write(self, path):
     '''Writes the block diagram to disk, creating the output directory if
     necessary.
     '''
     etau.ensure_basedir(path)
     with open(path, "wb") as f:
         f.write(self._file.render())
コード例 #4
0
ファイル: tf.py プロジェクト: zfyong/fiftyone
    def __enter__(self):
        self._idx = -1

        etau.ensure_basedir(self.tf_records_path)

        if self.num_shards:
            self._num_shards = self.num_shards
            tf_records_patt = self.tf_records_path + "-%05d-%05d"
            tf_records_paths = [
                tf_records_patt % (i, self.num_shards)
                for i in range(1, self.num_shards + 1)
            ]
        else:
            self._num_shards = 1
            tf_records_paths = [self.tf_records_path]

        self._writers_context = contextlib.ExitStack()
        c = self._writers_context.__enter__()

        self._writers = [
            c.enter_context(tf.io.TFRecordWriter(path))
            for path in tf_records_paths
        ]

        return self
コード例 #5
0
ファイル: primitives.py プロジェクト: saiprakash-c/504
    def process_video(self, input_path, coords_path=None, video_path=None):
        '''Detect feature points using self.detector.

        Args:
            input_path: the input video path
            masks_path: an optional path to write the per-frame feature points
                as .npy files
            video_path: an optional path to write the feature points video
        '''
        # Ensure output directories exist
        if coords_path:
            etau.ensure_basedir(coords_path)
        # VideoProcessor ensures that the output video directory exists

        self.reset()
        with etav.VideoProcessor(input_path, out_video_path=video_path) as p:
            for img in p:
                # Compute feature points
                keypoints = self.process_frame(img)

                if coords_path:
                    # Write feature points to disk
                    pts = _unpack_keypoints(keypoints)
                    np.save(coords_path % p.frame_number, pts)

                if video_path:
                    # Write feature points video
                    # We pass in an RGB image b/c this function is invariant to
                    # channel order
                    img = cv2.drawKeypoints(
                        img, keypoints, None, color=self.KEYPOINT_RGB_COLOR)
                    p.write(img)
コード例 #6
0
def write(img, path):
    '''Writes image to file. The output directory is created if necessary.

    Args:
        img: a numpy array
        path: the output path
    '''
    etau.ensure_basedir(path)
    cv2.imwrite(path, _exchange_rb(img))
コード例 #7
0
    def download(self, file_id, local_path):
        '''Downloads the file from Google Drive.

        Args:
            file_id: the ID of the file to download
            local_path: the path to the storage location
        '''
        etau.ensure_basedir(local_path)
        with open(local_path, "wb") as f:
            self._do_download(file_id, f)
コード例 #8
0
    def download(self, cloud_path, local_path):
        '''Downloads the file from Google Cloud Storage.

        Args:
            cloud_path: the path to the Google Cloud object to download
            local_path: the local disk path to store the downloaded file
        '''
        blob = self._get_blob(cloud_path)
        etau.ensure_basedir(local_path)
        blob.download_to_filename(local_path)
コード例 #9
0
    def upload_bytes(self, bytes_str, storage_path):
        '''Uploads the given bytes to storage.

        Args:
            bytes_str: the bytes string to upload
            storage_path: the path to the storage location
        '''
        etau.ensure_basedir(storage_path)
        with open(storage_path, "wb") as f:
            f.write(bytes_str)
コード例 #10
0
    def upload_stream(self, file_obj, storage_path):
        '''Uploads the contents of the given file-like object to storage.

        Args:
            file_obj: the file-like object to upload, which must be open for
                reading
            storage_path: the path to the storage location
        '''
        etau.ensure_basedir(storage_path)
        with open(storage_path, "wb") as f:
            for chunk in _read_file_in_chunks(file_obj, self.chunk_size):
                f.write(chunk)
コード例 #11
0
    def download(self, url, local_path):
        '''Downloads the file from the given URL via a GET request.

        Args:
            url: the URL from which to GET the file
            local_path: the path to which to write the downloaded file

        Raises:
            `requests.exceptions.HTTPError`: if the request resulted in an HTTP
                error
        '''
        etau.ensure_basedir(local_path)
        with open(local_path, "wb") as f:
            self._do_download(url, f)
コード例 #12
0
    def execute(parser, args):
        if args.destination:
            if sys.platform.startswith("win"):
                raise RuntimeError(
                    "This command is currently not supported on Windows."
                )

            control_path = os.path.join(
                foc.FIFTYONE_CONFIG_DIR, "tmp", "ssh.sock"
            )
            etau.ensure_basedir(control_path)

            # Port forwarding
            ret = subprocess.call(
                [
                    "ssh",
                    "-f",
                    "-N",
                    "-M",
                    "-S",
                    control_path,
                    "-L",
                    "5151:127.0.0.1:%d" % args.port,
                    args.destination,
                ]
            )
            if ret != 0:
                print("ssh failed with exit code %r" % ret)
                return

            def stop_port_forward():
                subprocess.call(
                    [
                        "ssh",
                        "-S",
                        control_path,
                        "-O",
                        "exit",
                        args.destination,
                    ],
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                )

            fou.call_on_exit(stop_port_forward)

        session = fos.launch_app()

        _watch_session(session)
コード例 #13
0
ファイル: primitives.py プロジェクト: saiprakash-c/504
    def process_video(
            self, input_path, fgmask_path=None, fgvideo_path=None,
            bgvideo_path=None):
        '''Performs background subtraction on the given video.

        Args:
            input_path: the input video path
            fgmask_path: an optional path to write the per-frame foreground
                masks (as boolean arrays) in .npy files
            fgvideo_path: an optional path to write the foreground-only video
            bgvideo_path: an optional path to write the background video
        '''
        # Ensure output directories exist
        if fgmask_path:
            etau.ensure_basedir(fgmask_path)
        # VideoWriters ensure that the output video directories exist

        r = etav.FFmpegVideoReader(input_path)
        try:
            if fgvideo_path:
                fgw = etav.FFmpegVideoWriter(
                    fgvideo_path, r.frame_rate, r.frame_size)
            if bgvideo_path:
                bgw = etav.FFmpegVideoWriter(
                    bgvideo_path, r.frame_rate, r.frame_size)

            self.reset()
            for img in r:
                fgmask, bgimg = self.process_frame(img)

                if fgmask_path:
                    # Write foreground mask
                    fgmask_bool = fgmask.astype(np.bool)
                    np.save(fgmask_path % r.frame_number, fgmask_bool)

                if fgvideo_path:
                    # Write foreground-only video
                    fgw.write(apply_mask(img, fgmask))

                if bgvideo_path:
                    # Write background video
                    bgw.write(bgimg)
        finally:
            if fgvideo_path:
                fgw.close()
            if bgvideo_path:
                bgw.close()
コード例 #14
0
ファイル: primitives.py プロジェクト: saiprakash-c/504
    def process_video(
            self, input_path, cart_path=None, polar_path=None,
            video_path=None):
        '''Performs dense optical flow on the given video.

        Args:
            input_path: the input video path
            cart_path: an optional path to write the per-frame arrays as .npy
                files describing the flow fields in Cartesian (x, y)
                coordinates
            polar_path: an optional path to write the per-frame arrays as .npy
                files describing the flow fields in polar (magnitude, angle)
                coordinates
            video_path: an optional path to write a video that visualizes the
                magnitude and angle of the flow fields as the value (V) and
                hue (H), respectively, of per-frame HSV images
        '''
        # Ensure output directories exist
        if cart_path:
            etau.ensure_basedir(cart_path)
        if polar_path:
            etau.ensure_basedir(polar_path)
        # VideoProcessor ensures that the output video directory exists

        self.reset()
        with etav.VideoProcessor(input_path, out_video_path=video_path) as p:
            for img in p:
                # Compute optical flow
                flow_cart = self.process_frame(img)

                if cart_path:
                    # Write Cartesian fields
                    np.save(cart_path % p.frame_number, flow_cart)

                if not polar_path and not video_path:
                    continue

                # Convert to polar coordinates
                flow_polar = cart_to_polar(flow_cart)

                if polar_path:
                    # Write polar fields
                    np.save(polar_path % p.frame_number, flow_polar)

                if video_path:
                    # Write flow visualization frame
                    p.write(polar_to_img(flow_polar))
コード例 #15
0
def custom_setup(lc, rotate=False):
    '''Sets up custom logging.

    Args:
        lc: a LoggingConfig instance
        rotate: True/False. If True, any existing logs are rotated and
            new messages are written to a new logfile. If False, new messages
            are appended to the existing log (if any). The default is False
    '''
    # Messages to log after setup
    msgs = []

    # Reset logging
    msgs.append("Resetting logging")
    reset()

    # Stdout logging
    if lc.stream_to_stdout:
        stream_handler = logging.StreamHandler(stream=sys.stdout)
        stream_handler.setFormatter(
            logging.Formatter(fmt=lc.stdout_format, datefmt=lc.datefmt))
        stream_handler.setLevel(getattr(logging, lc.stdout_level))
        root_logger.addHandler(stream_handler)
        msgs.append("Logging to stdout at level %s" % lc.stdout_level)

    # File logging
    if lc.filename:
        etau.ensure_basedir(lc.filename)
        if rotate:
            msgs += _rotate_logs(lc.filename)

        file_handler = logging.FileHandler(
            lc.filename, mode="at", encoding=lc.encoding)
        file_handler.setFormatter(
            logging.Formatter(fmt=lc.file_format, datefmt=lc.datefmt))
        file_handler.setLevel(getattr(logging, lc.file_level))
        root_logger.addHandler(file_handler)
        msgs.append("Logging to %s at level %s" % (lc.filename, lc.file_level))

    msgs.append("Logging initialized")

    # Initial logging output
    eta.startup_message()
    for msg in msgs:
        logger.info(msg)
コード例 #16
0
def write_json(obj, path, pretty_print=True):
    '''Writes JSON object to file, creating the output directory if necessary.

    Args:
        obj: is either an object that can be directly dumped to a JSON file or
            an instance of a subclass of Serializable
        path: the output path
        pretty_print: when True (default), the resulting JSON will be outputted
            to be human readable; when False, it will be compact with no
            extra spaces or newline characters
    '''
    if is_serializable(obj):
        obj = obj.serialize()
    s = json_to_str(obj, pretty_print=pretty_print)

    etau.ensure_basedir(path)
    with open(path, "wt") as f:
        f.write(s)
コード例 #17
0
def _perform_convolution(convolution_config):
    '''Performs convolution of an input image with a kernel specified
    by the configuration parameters, and writes the result to the
    path specified by "filtered_image".

    Args:
        convolution_config: the configuration file for the module
    '''
    kernel_type = convolution_config.parameters.kernel_type
    if kernel_type == "x_derivative":
        kernel = _create_x_derivative_kernel()
    elif kernel_type == "y_derivative":
        kernel = _create_y_derivative_kernel()
    elif kernel_type == "sobel_vertical":
        kernel = _create_sobel_vertical_kernel()
    elif kernel_type == "sobel_horizontal":
        kernel = _create_sobel_horizontal_kernel()
    else:
        # this will be the Gaussian kernel
        # (make sure to remove this comment!)
        kernel = _create_gaussian_kernel(
            convolution_config.parameters.gaussian_sigma)

    for data in convolution_config.data:
        in_img = etai.read(data.input_image)
        if convolution_config.parameters.image_type == "grayscale":
            in_img = etai.rgb_to_gray(in_img)
        else:
            # if the image should be a color image, convert the grayscale
            # image to color (simply converts the image into a 3-channel
            # image)
            if etai.is_gray(in_img):
                in_img = etai.gray_to_rgb(in_img)
        if convolution_config.parameters.image_max_range == 1:
            in_img = (in_img.astype(float)) / 255.0

        filtered_image = _convolve(kernel, in_img)
        if data.filtered_matrix:
            etau.ensure_basedir(data.filtered_matrix)
            np.savez(data.filtered_matrix, filtered_matrix=filtered_image)
        if data.filtered_image:
            etau.ensure_basedir(data.filtered_image)
            etai.write(filtered_image, data.filtered_image)
コード例 #18
0
def embed_image(impath):
    '''Embeds the image using VGG-16 and stores the embeddeding as an .npz file
    on disk.

    Args:
        impath: path to an image to embed
    '''
    img = etai.read(impath)
    rimg = etai.resize(img, 224, 224)

    vgg16 = etav.VGG16()
    embedded_vector = vgg16.evaluate([rimg], layer=vgg16.fc2l)[0]

    logger.info("Image embedded to vector of length %d", len(embedded_vector))
    logger.info("%s", embedded_vector)

    outpath = _abspath("out/result_embed_image.npz")
    etau.ensure_basedir(outpath)
    np.savez_compressed(outpath, v=embedded_vector)
    logger.info("Result saved to '%s'", outpath)
コード例 #19
0
ファイル: embed_image.py プロジェクト: saiprakash-c/504
def embed_image(impath):
    '''Embeds the image using VGG-16 and stores the embeddeding as an .npz file
    on disk, using VideoFeaturizer to handle I/O.

    Args:
        impath: path to an image to embed
    '''
    img = etai.read(impath)

    # Invoke the Featurizer using the "with" syntax to automatically handle
    # calling the start() and stop() methods
    with VGG16Featurizer() as vfeaturizer:
        embedding = vfeaturizer.featurize(img)

    logger.info("Image embedded to vector of length %d", len(embedding))
    logger.info("%s", embedding)

    outpath = _abspath("out/result_embed_image.npz")
    etau.ensure_basedir(outpath)
    np.savez_compressed(outpath, v=embedding)
    logger.info("Result saved to '%s'", outpath)
コード例 #20
0
ファイル: models.py プロジェクト: saiprakash-c/504
    def download_model(self, model_path, force=False):
        '''Downloads the model to the given local path.

        If the download is forced, any existing model is overwritten. If the
        download is not forced, the model will only be downloaded if it does
        not already exist locally.

        Args:
            model_path: the path to which to download the model
            force: whether to force download the model. If True, the model is
                always downloaded. If False, the model is only downloaded if
                necessary. The default is False

        Raises:
            ModelError: if model downloading is not currently allowed
        '''
        if force or not os.path.isfile(model_path):
            if not eta.config.allow_model_downloads:
                raise ModelError(
                    "Model downloading is currently disabled. Modify your ETA "
                    "config to change this setting.")
            etau.ensure_basedir(model_path)
            self._download_model(model_path)
コード例 #21
0
def _find_corners(harris_corner_config):
    for data in harris_corner_config.data:
        sobel_horiz = np.load(data.sobel_horizontal_result)["filtered_matrix"]
        sobel_vert = np.load(data.sobel_vertical_result)["filtered_matrix"]
        corner_response, corner_locations = _get_harris_corner(
            sobel_horiz, sobel_vert,
            harris_corner_config.parameters.window_half_size,
            harris_corner_config.parameters.threshold)
        corner_locs_after_sup = non_max_suppression(
            corner_response, harris_corner_config.parameters.non_max_radius)
        if data.corner_locations:
            etau.ensure_basedir(data.corner_locations)
            np.savez(data.corner_locations,
                     corner_locations=corner_locs_after_sup)
        if data.corners_img_before_sup or data.corners_img_after_sup:
            in_img = etai.read(data.input_image)
            if data.corners_img_before_sup:
                corners_viz_before_sup = _visualize_corners(
                    in_img, corner_locations)
                etai.write(corners_viz_before_sup, data.corners_img_before_sup)
            if data.corners_img_after_sup:
                corners_viz_after_sup = _visualize_corners(
                    in_img, corner_locs_after_sup)
                etai.write(corners_viz_after_sup, data.corners_img_after_sup)
コード例 #22
0
 def pickle(self, path):
     '''Saves the instance to disk in a pickle. '''
     etau.ensure_basedir(path)
     with open(path, "wb") as f:
         pickle.dump(self, f)
コード例 #23
0
ファイル: main.py プロジェクト: zfyong/fiftyone
                }
            },
            {
                "$project": {
                    "name": k,
                    "type": v.__class__.__name__[
                        : -len("Field")  # grab field type from the class
                    ].lower(),
                    "data": "$data",
                }
            },
        ]


socketio.on_namespace(StateController("/state"))


if __name__ == "__main__":
    log_path = os.path.join(
        foc.FIFTYONE_CONFIG_DIR, "var", "log", "server.log"
    )
    etau.ensure_basedir(log_path)
    # pylint: disable=no-member
    app.logger.addHandler(logging.FileHandler(log_path, mode="w"))

    parser = argparse.ArgumentParser()
    parser.add_argument("--port", type=int, default=5151)
    args = parser.parse_args()

    socketio.run(app, port=args.port, debug=foc.DEV_INSTALL)