Class OpenCVDnn

All Implemented Interfaces:
AutoCloseable, UriResource, DnnModel

public class OpenCVDnn extends AbstractDnnModel<Mat> implements UriResource
Wrapper for an OpenCV Net, including essential metadata about how it should be used.

The main purpose of this is to support serializing models to JSON... kind of. In truth currently the paths to the original model files are serialized, since (to my knowledge) there is no way to save and reload a Net directly.

Author:
Pete Bankhead
  • Constructor Details

    • OpenCVDnn

      public OpenCVDnn()
  • Method Details

    • buildNet

      public Net buildNet()
      Build the OpenCV Net. This is a lower-level function than buildModel(), which provides more options to query the network architecture but does not incorporate any preprocessing steps.
      Returns:
    • buildModel

      public <T extends Model> T buildModel(OpenCVDnn.ModelType type)
      Build a model, specifying the OpenCVDnn.ModelType.
      Type Parameters:
      T -
      Parameters:
      type -
      Returns:
      See Also:
    • buildModel

      public <T extends Model> T buildModel()
      Build a model. The return type is determined by the OpenCVDnn.ModelType.
      Type Parameters:
      T -
      Returns:
      See Also:
    • initializeModel

      public void initializeModel(Model model)
      Initialize the model with the same preprocessing defined here (i.e. input size, mean, scale, crop, swapRB).
      Parameters:
      model -
    • getName

      public String getName()
      Get a user-readable name for this model, or null if no name is specified.
      Returns:
    • getScale

      public Double getScale()
      Get scale factors to be applied to preprocess input.
      Returns:
      the scale value if specified, or null if default scaling should be used
      See Also:
    • getModelType

      public OpenCVDnn.ModelType getModelType()
      Get the type of the model that would be built with buildModel().
      Returns:
    • getMean

      public Scalar getMean()
      Get means which should be subtracted for preprocessing.
      Returns:
      the mean value if specified, or null if OpenCV's default should be used (likely to be zero)
      See Also:
    • getModelUri

      public URI getModelUri()
      Get the path to the model.
      Returns:
    • getConfigUri

      public URI getConfigUri()
      Get the path to the model configuration, if required.
      Returns:
    • getFramework

      public String getFramework()
      Get the framework used to create the model.
      Returns:
    • builder

      public static OpenCVDnn.Builder builder(String pathModel)
      Create a new builder.
      Parameters:
      pathModel -
      Returns:
    • getURIs

      public Collection<URI> getURIs() throws IOException
      Description copied from interface: UriResource
      Get all the URIs required for this resource. This is often an empty or singleton list.
      Specified by:
      getURIs in interface UriResource
      Returns:
      the required URIs
      Throws:
      IOException
    • updateURIs

      public boolean updateURIs(Map<URI,URI> replacements) throws IOException
      Description copied from interface: UriResource
      Update the specified URIs.

      Warning! This should be used sparingly, particularly for objects that are otherwise immutable. It is intended only for correcting paths that have become invalid (e.g. because of files being relocated or transferred between computers) before first use of the object. It should not be used to manipulate an object after construction. Indeed, some implementations may throw an UnsupportedOperationException if called too late.

      Specified by:
      updateURIs in interface UriResource
      Parameters:
      replacements - replacement map, where the key gives the current URI and the value gives its replacement.
      Returns:
      true if URIs were changed, false otherwise
      Throws:
      IOException
    • getBlobFunction

      public BlobFunction<Mat> getBlobFunction()
      Description copied from class: AbstractDnnModel
      Get the function that can convert one or more OpenCV Mats into a blob supported by the prediction function for the first (or only) input.
      Specified by:
      getBlobFunction in class AbstractDnnModel<Mat>
      Returns:
    • getBlobFunction

      public BlobFunction<Mat> getBlobFunction(String name)
      Description copied from class: AbstractDnnModel
      Get the function that can convert one or more OpenCV Mats into a blob supported by the prediction function for a specified input layer.
      Specified by:
      getBlobFunction in class AbstractDnnModel<Mat>
      Parameters:
      name -
      Returns:
    • getPredictionFunction

      public PredictionFunction<Mat> getPredictionFunction()
      Description copied from class: AbstractDnnModel
      Get the prediction function that can apply a prediction with one or more blobs as input.
      Specified by:
      getPredictionFunction in class AbstractDnnModel<Mat>
      Returns:
    • close

      public void close() throws Exception
      Description copied from interface: DnnModel
      Close this model if it will not be needed again. Subclasses that require cleanup may override this. The default implementation does nothing.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface DnnModel
      Throws:
      Exception