diff --git a/Lab11.iml b/Lab11.iml index c703763..fe799c4 100644 --- a/Lab11.iml +++ b/Lab11.iml @@ -3,6 +3,7 @@ + @@ -11,5 +12,7 @@ + + \ No newline at end of file diff --git a/src/livefilter/FaceDetection.scala b/src/livefilter/FaceDetection.scala new file mode 100644 index 0000000..ee28618 --- /dev/null +++ b/src/livefilter/FaceDetection.scala @@ -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() + } +} diff --git a/src/livefilter/LiveFilter.scala b/src/livefilter/LiveFilter.scala index 08bc521..167f008 100644 --- a/src/livefilter/LiveFilter.scala +++ b/src/livefilter/LiveFilter.scala @@ -3,6 +3,7 @@ package livefilter import com.github.sarxos.webcam.WebcamPanel.DrawMode import com.github.sarxos.webcam.{Webcam, WebcamImageTransformer, WebcamPanel, WebcamResolution} import imagefilters.ImageFilters +import org.bytedeco.opencv.opencv_core.{Rect, RectVector} import java.awt.image.BufferedImage import java.awt.{Color, Dimension} @@ -87,8 +88,14 @@ class LiveFilter extends WebcamImageTransformer { override def transform(image: BufferedImage): BufferedImage = { val img: Array[Array[Color]] = toArray(image) - // TODO Complete here by changing the assignment with your filters - val filtered: Array[Array[Color]] = ImageFilters.mask(ImageFilters.sepia(ImageFilters.noise(img, 10)), videoMask) + val bw: Array[Array[Int]] = toBW(image) + 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) return image