added face detection

This commit is contained in:
Louis Heredero 2023-12-07 10:45:50 +01:00
parent 791f6b7b88
commit 6df1a451ec
3 changed files with 48 additions and 2 deletions

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
@ -11,5 +12,7 @@
<orderEntry type="library" name="bridj-0.7.0" level="project" /> <orderEntry type="library" name="bridj-0.7.0" level="project" />
<orderEntry type="library" name="slf4j-api-1.7.2" level="project" /> <orderEntry type="library" name="slf4j-api-1.7.2" level="project" />
<orderEntry type="library" name="webcam-capture-0.3.12" level="project" /> <orderEntry type="library" name="webcam-capture-0.3.12" level="project" />
<orderEntry type="library" name="bytedeco.javacv" level="project" />
<orderEntry type="library" name="bytedeco.javacv.platform" level="project" />
</component> </component>
</module> </module>

View File

@ -0,0 +1,36 @@
package livefilter
import org.bytedeco.javacpp.Loader
import org.bytedeco.javacpp.indexer.UByteRawIndexer
import org.bytedeco.opencv.global.opencv_core.CV_8U
import org.bytedeco.opencv.opencv_core.{Mat, Rect, RectVector}
import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier
import java.io.File
import java.net.URL
object FaceDetection {
val url: URL = new URL("https://raw.github.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt.xml")
val file: File = Loader.cacheResource(url)
val classifierName: String = file.getAbsolutePath
val cascade: CascadeClassifier = new CascadeClassifier(classifierName)
def detectFaces(src: Array[Array[Int]]): Array[Rect] = {
val grayFrame: Mat = new Mat()
val width: Int = src.length
val height: Int = if (width == 0) 0 else src(0).length
grayFrame.create(height, width, CV_8U)
val idx: UByteRawIndexer = grayFrame.createIndexer()
for (x: Int <- 0 until width) {
for (y: Int <- 0 until height) {
idx.put(y, x, src(x)(y))
}
}
val faces: RectVector = new RectVector()
cascade.detectMultiScale(grayFrame, faces)
return faces.get()
}
}

View File

@ -3,6 +3,7 @@ package livefilter
import com.github.sarxos.webcam.WebcamPanel.DrawMode import com.github.sarxos.webcam.WebcamPanel.DrawMode
import com.github.sarxos.webcam.{Webcam, WebcamImageTransformer, WebcamPanel, WebcamResolution} import com.github.sarxos.webcam.{Webcam, WebcamImageTransformer, WebcamPanel, WebcamResolution}
import imagefilters.ImageFilters import imagefilters.ImageFilters
import org.bytedeco.opencv.opencv_core.{Rect, RectVector}
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.awt.{Color, Dimension} import java.awt.{Color, Dimension}
@ -87,8 +88,14 @@ class LiveFilter extends WebcamImageTransformer {
override def transform(image: BufferedImage): BufferedImage = { override def transform(image: BufferedImage): BufferedImage = {
val img: Array[Array[Color]] = toArray(image) val img: Array[Array[Color]] = toArray(image)
// TODO Complete here by changing the assignment with your filters val bw: Array[Array[Int]] = toBW(image)
val filtered: Array[Array[Color]] = ImageFilters.mask(ImageFilters.sepia(ImageFilters.noise(img, 10)), videoMask) val faces: Array[Rect] = FaceDetection.detectFaces(bw)
var filtered: Array[Array[Color]] = img
for (rect: Rect <- faces) {
filtered = ImageFilters.pixelize(filtered, 10, rect.x, rect.y, rect.x+rect.width, rect.y+rect.height)
}
//val filtered: Array[Array[Color]] = ImageFilters.mask(ImageFilters.sepia(ImageFilters.noise(img, 10)), videoMask)
updateImage(filtered, image) updateImage(filtered, image)
return image return image