Class: DetectFaces

Inherits:
Object
  • Object
show all
Defined in:
lib/detectfaces.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')) ⇒ DetectFaces

Returns a new instance of DetectFaces.



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

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.



12
13
14
# File 'lib/detectfaces.rb', line 12

def faces
  @faces
end

Instance Method Details

#read(fname) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/detectfaces.rb', line 23

def read(fname)

  @fname = fname
  @faces = detect_faces(fname)

  self

end

#to_htmlObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/detectfaces.rb', line 32

def to_html()
  
  s = Base64.strict_encode64(File.open(@fname, "rb").read)
 
  style_faces = @faces.map.with_index do |detail, i|
    "#face%s { left: %spx; top: %spx; width: %spx; height: %spx; }" % \
        [i, *detail]
  end
  
  div_faces = @faces.map.with_index do |detail, i|
    "<div id='face%s'></div>" % i
  end    
  

"<!DOCTYPE html>\n<html>\n<head>\n  <style type=\"text/css\">\n    body {\n      margin: 0;\n      background: url(data:image/jpg;base64,\#{s}) \n      no-repeat top left;\n    }\n    \n    #faces div {position: absolute; border: 3px solid #1c1}\n    \#{style_faces.join(\"\\n      \")}\n  </style>\n</head>\n<body>\n  <div id='faces'>\n    \#{div_faces.join(\"\\n      \")}\n  </div>\n</body>\n</html>\n  HTML\n\nend\n"