Coder Social home page Coder Social logo

paperclip-facecrop's Introduction

Paperclip::FaceCrop

Paperclip::FaceCrop is a Paperclip processor that is aware of the faces found on the image so that they aren't cropped or aren't shown too small while generating the thumbnails. It can use the OpenCV library, the Face.com or LambdaLabs web service(or all three at the same time) for the facial recognition.

Requirements:

OpenCV

If you want to use OpenCV on your own server, you need to install:

In case you get the error message /ext/opencv/cverror.cpp:143: error: ‘CV_GpuCufftCallError’ was not declared in this scope while installing the ruby binding, checkout the OpenCV_2.2 branch or just remove the line 143 from /ext/opencv/cverror.cpp

Face.com

In order to use the Face.com service, you will also need to register in order to get your api key and api secret for your application.

lambdal.com

In order to use the lambdal.com service, you will also need to register in order to get your api key for your application.

Installation:

  • Add to your application Gemfile

        gem 'paperclip-facecrop'
    
  • Type

        bundle install
    
  • Write an initializer setting the detectors configuration (initializers/paperclip.rb for example):

OpenCV

Set the path of the haarcascade filters:

      Paperclip::FaceCrop.detectors = {
        'OpenCV' =>  { 
          :face => %w(/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml)
        }
      }

You can use more than one filter to try more accurate searches:

      Paperclip::FaceCrop.detectors = {
        'OpenCV' =>  { 
          :face => %w(/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml
                      /usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
                      /usr/local/share/opencv/haarcascades/haarcascade_profileface.xml)
        }
      }

In order to try to avoid some false positives, you can also specify other classifiers to detect other parts of the face. In that case, only the found areas that contain parts like a mouth, an eye or a nose will be considered a face:

      Paperclip::FaceCrop.detectors = {
        'OpenCV' =>  { 
          :face => %w(/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml
                      /usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
                      /usr/local/share/opencv/haarcascades/haarcascade_profileface.xml),
          
          :parts => %w(/usr/local/share/opencv/haarcascades/haarcascade_mcs_nose.xml
                       /usr/local/share/opencv/haarcascades/haarcascade_mcs_lefteye.xml
                       /usr/local/share/opencv/haarcascades/haarcascade_mcs_righteye.xml)
        }
      }

Face.com

      Paperclip::FaceCrop.detectors = {
        'FaceCom' => { :api_key => "<YOUR API KEY>", :api_secret => "<YOUR API SECRET>"}
      }    

lambdal.com

      Paperclip::FaceCrop.detectors = {
        'LambdaLabs' => {:mashape_authorization => "<YOUR API KEY>"}
      }  

Usage:

Just specify your image styles as usual and set :face_crop as the processor:

class Image < ActiveRecord::Base

  has_attached_file :attachment, 
      :styles => {:thumbnail => "200x125#"}, 
      :processors => [:face_crop]
end

In case no faces were found, it will behave simply as the Paperclip::Thumbnail processor

You can also set the debug mode to draw on the image the detected regions:

Paperclip::FaceCrop.debug = Rails.env.development?

Each detector will draw the found regions in different colors(Face.com detector in red and OpenCV in green)

Credits:

Copyright (c) 2011 Borja Martín Sánchez de Vivar [email protected] - http://dagi3d.net, released under the MIT license
The photo used as example belongs to Jesper Rønn-Jensen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.