Class: BlurFaces

Inherits:
Object
  • Object
show all
Defined in:
lib/blurfaces.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname = nil, haar_xml_file: File.join(File.dirname(__FILE__),\ 'haarcascade_frontalface_alt.xml')) ⇒ BlurFaces

Returns a new instance of BlurFaces.



13
14
15
16
17
18
19
20
# File 'lib/blurfaces.rb', line 13

def initialize(fname=nil, haar_xml_file: File.join(File.dirname(__FILE__),\
     'haarcascade_frontalface_alt.xml'))

  @detector = OpenCV::CvHaarClassifierCascade::load haar_xml_file

  read(fname) if fname 

end

Instance Attribute Details

#facesObject

Returns the value of attribute faces.



11
12
13
# File 'lib/blurfaces.rb', line 11

def faces
  @faces
end

Instance Method Details

#blur(strength: 8) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/blurfaces.rb', line 22

def blur(strength: 8)

  @faces.each do |x, y, width, height|

    region = @img.dispatch(x, y, width, height, 'RGB')
    face_img = Magick::Image.constitute(width, height, "RGB", region)

    @img.composite!(face_img.gaussian_blur(0, strength), x, y, 
      Magick::OverCompositeOp)

  end

end

#read(fname) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/blurfaces.rb', line 36

def read(fname)

  @fname = fname
  @faces = detect_faces(fname)
  @img = Magick::Image.read(fname)[0]

  self

end

#save(fname) ⇒ Object



46
47
48
# File 'lib/blurfaces.rb', line 46

def save(fname)
  @img.write(fname)
end