在scala中使用opencv4.8.0进行人脸识别与比较

问题描述 投票:0回答:1

我正在开发OpenCv4.8.0库比较两个面孔(在图像中)无论它们是否属于同一个人。

我使用scala(版本2.13.4)、sbt(版本1.9.6)和Intellij idea 作为IDE。

这是我的代码

import org.opencv.core.{MatOfDMatch, MatOfKeyPoint, _}
import org.opencv.imgcodecs.Imgcodecs
import org.opencv.features2d.{DescriptorMatcher, SIFT}
import scala.collection.convert.ImplicitConversions.`list asScalaBuffer`
import scala.collection.JavaConverters._ 

object FaceRecognition extends App {

  System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

  val selfieImage = Imgcodecs.imread("/home/image1.jpg")
  val driverLicenseImage = Imgcodecs.imread("/home/image2.jpg")

  val sift = SIFT.create()
  val keyPoints1 = new MatOfKeyPoint()
  val descriptors1 = new Mat()
  val keyPoints2 = new MatOfKeyPoint()
  val descriptors2 = new Mat()

  sift.detectAndCompute(selfieImage, new Mat(), keyPoints1, descriptors1)
  sift.detectAndCompute(driverLicenseImage, new Mat(), keyPoints2, descriptors2)

  val matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE)
  val matches = new java.util.LinkedList[MatOfDMatch]
  matcher.knnMatch(descriptors1, descriptors2, matches, 2)


  val goodMatchesList = matches.toList
    .filter { m =>
      m.toArray()(0).distance < 0.75 * m.toArray()(1).distance
    }
    .map { m =>
      m.toArray()(0)
    }

  val javaGoodMatchesList = goodMatchesList.asJava
  val filteredMatches = new MatOfDMatch()
  filteredMatches.fromList(javaGoodMatchesList)
  val similarityScore = filteredMatches.size().height / keyPoints1.size().height

  println(s"Similarity Score: $similarityScore")

  val similarityThreshold = 0.7
  if (similarityScore > similarityThreshold) {
    println("The images are likely of the same person.")
  } else {
    println("The images are likely of different persons.")
  }
}

如果图像中的面孔完全相同,上面的代码工作正常,但是当我用当前图像和 2,3 年前的照片测试它时,它会将它们检测为不同的图像,尽管它们完全相同。

我改变了similarityThreshold

的值
val goodMatchesList = matches.toList
        .filter { m =>
          m.toArray()(0).distance < 0.75 * m.toArray()(1).distance
        }
        .map { m =>
          m.toArray()(0)
        }

我还更改了比较值(0.75),但其行为仍然相同。

指导我如何实现上述功能?我需要训练一些模型吗(如果是的话我该怎么做)?

scala opencv intellij-idea face-recognition
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.