Face Censoring Menggunakan OpenCV

FaceCensoring

Malam tahun baru, keisengan muncul kembali. Kali ini akibat membaca berita tentang salah satu stasiun TV yang menayangkan korban (?) sebuah insiden pesawat yang baru-baru ini terjadi (my deep condolences for the incident …). Terlepas dari masalah kode etik jurnalisme, kali ini saya akan membahas bagaimana melakukan censoring object yang kerap digunakan oleh banyak stasiun TV. Biasanya, metode censoring ini digunakan untuk menyamarkan wajah seseorang yang tidak ingin disebarluaskan identitasnya, wajah anak-anak kecil yang muncul di sebuah berita (di TV Jepang biasanya banyak terjadi hal ini), atau tersangka kasus kriminal tertentu.

Dengan menggunakan OpenCV, hal di atas dapat dilakukan dengan (sangat) mudah (dapat dibuat dalam 5 menit !!). Kita akan mencoba menampilkan video camera, mendeteksi wajah, dan melakukan censoring. Berikut ini adalah potongan programnya :

Disclaimer : dimohon untuk tidak menyalahgunakan program berikut untuk Tugas Akhir/Skripsi/ Tugas Kuliah anda !!! 😀

/*
 * Copyright (c) 2014. Igi Ardiyanto <my_first_name(at)ugm(dot)ac(dot)id>.
 * Released to public domain under terms of the BSD Simplified license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the organization nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 */

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include

// routine for pixelating/censoring the detected face
void pixelate_face(cv::Mat& original, const cv::Rect& face_i, int num_division=8)
{
   int size_h = face_i.height/num_division;
   int size_w = face_i.width/num_division;
   for (int i = face_i.tl().x; i < face_i.br().x; i += size_w)
   {
      for (int j = face_i.tl().y; j < face_i.br().y; j += size_h)
      {
         cv::Rect r = cv::Rect(i, j, size_w, size_h) & cv::Rect(0, 0, original.cols, original.rows);
         cv::Mat temp(original, r);
         temp.setTo(cv::mean(original(r)));
      }
   }
}

int main(int argc, const char *argv[])
{
   // Prepare the cascade classifier
   // You can also use "haarcascade_frontalface_alt.xml"
   std::string face_cascade_name = "lbpcascade_frontalface.xml";
   cv::CascadeClassifier haar_cascade;
   if(!haar_cascade.load(face_cascade_name)){
      std::cerr << "Cascade Classifier model cannot be opened." << std::endl;
      return -1;
   }
   // Get a handle to the Video device:
   int deviceId = 0;
   cv::VideoCapture cap(deviceId);
   // Check if we can use this device at all:
   if(!cap.isOpened()) {
      std::cerr << "Capture Device ID " << deviceId << "cannot be opened." << std::endl;       return -1;    }    // Hold the frame from the Video device:    cv::Mat frame;    for(;;) {       cap >> frame;
      // Grab the current frame:
      cv::Mat original = frame.clone();
      // Convert to grayscale:
      cv::Mat gray;
      cv::cvtColor(original, gray, CV_BGR2GRAY);
      // Find the faces in the frame:
      std::vector< cv::Rect_ > faces;
      haar_cascade.detectMultiScale(gray, faces);
      // Process the face and pixelate it !!
      for(int i = 0; i < (int)faces.size(); i++) {
         // Process face by face:
         cv::Rect face_i = faces[i];
         // Censor all faces on the original image!
         pixelate_face(original, face_i);
      }
      // Show the result:
      cv::imshow("Face Censoring", original);
      // Display it:
      char key = (char) cv::waitKey(20);
      // Exit on escape:
      if(key == 27)
         break;
   }
   return 0;
}

Program di atas diuji dengan menggunakan Visual Studio Express 2010 dan OpenCV 2.4.8, yang hasilnya adalah sebagai berikut

FaceCensoring
Sangat mudah bukan ??

About the Author

Igi Ardiyanto
Administrator