Home > Blockchain >  Convert OpenCV Mat in Java to NumPy Array in Scala
Convert OpenCV Mat in Java to NumPy Array in Scala

Time:11-02

I have a sample codebase in Scala where I use OpenCV and ScalaPy for doing some image classification. Here is the codebit:

def loadImage(imagePath: String): Image = {
  // 0. Load the image and extract class label where a path to the image is assumed to be
  // /path/to/dataset/{class}/{image}.jpg
  val matrix: Mat = imread(imagePath)
  val label = imagePath.split("")

  // 1. Run the loaded image through the preprocessors, resulting in a feature vector
  //val preProcessedImagesWithLabels = Seq(new ImageResizePreProcessor(appCfg)).map(preProcessor => (preProcessor.preProcess(matrix), label))
  val preProcessedImagesWithLabels = Seq(new ImageResizePreProcessor(appCfg)).map(preProcessor => preProcessor.preProcess(matrix))
  np.asarray(preProcessedImagesWithLabels)
}

It fails however for the reason that it cannot find an implicit converter for NumPy:

[error] /home/joesan/Projects/Private/ml-projects/object-classifier/src/main/scala/com/bigelectrons/animalclassifier/ImageLoader.scala:10:34: not found: type NumPy
[error]   val np = py.module("numpy").as[NumPy]

What is to be expected in addition to the imports?

  "me.shadaj" %% "scalapy-numpy" % "0.1.0",
"me.shadaj" %% "scalapy-core" % "0.5.0",

CodePudding user response:

Try with latest "dev" version of scalapy-numpy: 0.1.0 6-14ca0424

So change the sbt build in:

libraryDependencies  = "me.shadaj" %% "scalapy-numpy" % "0.1.0 6-14ca0424"

I try in ammonite this script:

import me.shadaj.scalapy.numpy.NumPy 
import me.shadaj.scalapy.py 

val np = py.module("numpy").as[NumPy] 

An it seems to find the NumPy as expected

  • Related