首页 > 解决方案 > OpenCV 和 NumPy 集成的编译错误

问题描述

我正在尝试使用 ScalaPy 库将 OpenCV 矩阵加载为 NumPy 数组,但看起来它缺少将 Matrx 转换为 NumPy 数组的隐式编写器。这是示例代码:

首先我的进口:

import me.shadaj.scalapy.numpy.NumPy
import me.shadaj.scalapy.py
import org.bytedeco.opencv.global.opencv_imgcodecs.imread
import org.bytedeco.opencv.opencv_core.Mat

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

def loadImage(imagePath: String) = {
  // 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) // Fails here as expected
}

这是我面临的错误:

[error] /home/joesan/Projects/Private/ml-projects/object-classifier/src/main/scala/com/bigelectrons/animalclassifier/ImageLoader.scala:22:7: Symbol 'type me.shadaj.scalapy.py.Writer' is missing from the classpath
[error] This symbol is required by 'value me.shadaj.scalapy.numpy.NumPy.writer'.
[error] Make sure that type Writer is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'NumPy.class' was compiled against an incompatible version of me.shadaj.scalapy.py.
[error]       np.asarray(preProcessedImagesWithLabels)
[error]       ^
[error] /home/joesan/Projects/Private/ml-projects/object-classifier/src/main/scala/com/bigelectrons/animalclassifier/ImageLoader.scala:22:17: could not find implicit value for parameter writer: me.shadaj.scalapy.py.Writer[org.bytedeco.opencv.opencv_core.Mat]
[error]       np.asarray(preProcessedImagesWithLabels)
[error]                 ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 5 s, completed Nov 1, 2021 9:40:45 AM

我面临两个错误。我是否应该有一个知道如何将 Mat 转换为 NumPy 数组的隐式编写器实现?有任何想法吗?

标签: pythonnumpyopencvscalapy

解决方案


推荐阅读