Class: Facer::Faces

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

Overview

Base class to query the api on faces and tags info All the queries are in the “simplest form” of a single image/single user query In this version of the gem I assume to use “single face” images You can provide images as a file path or a public url, the library will treat them accordingly

Instance Method Summary collapse

Constructor Details

#initialize(linker) ⇒ Faces

Initialize the class passing a convenient linker reference



8
9
10
# File 'lib/facer/faces.rb', line 8

def initialize(linker)
  @linker=linker
end

Instance Method Details

#associate(image, user, namespace, force_train = false, detector = :Normal) ⇒ Object

Associace a face image to a user. I assume the image contains a single face



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/facer/faces.rb', line 88

def associate(image,user,namespace, force_train = false, detector = :Normal)
  img=detect(image,detector)
  if(img && img["tags"].length == 1)
    tag=img["tags"][0]
    if(tag["attributes"]["face"] && tag["attributes"]["face"]["value"].to_s == "true")
      tid=tag["tid"]
      ret=@linker.call("tags","save",{"tids" => tid, "uid" => "#{user}@#{namespace}"})
      if(ret["status"].to_sym == :success)
        if(force_train)
          train(user,namespace)
        end
        return {"tid" => ret["saved_tags"][0]["tid"], "infos" => tag}
      end
    end
  end
  return nil
end

#associate_multi(images, user, namespace, force_train = false, detector = :Normal) ⇒ Object

Associace many images to a user. I assume every images contains a single face



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/facer/faces.rb', line 74

def associate_multi(images,user,namespace, force_train = false, detector = :Normal)
  out=[]
  images.each do |image|
    resp=associate(image,user,namespace,false,detector) || {"tid" => nil, "infos" => nil}
    resp["image"]=image
    out += resp
  end
  if(force_train)
     train(user,namespace)
  end
  return out
end

#deassociate(tid, user, namespace, force_train = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/facer/faces.rb', line 106

def deassociate(tid,user,namespace, force_train = false)
   ret=@linker.call("tags","remove",{"tids" => tid})
   if(ret["status"].to_sym == :success)
    if(force_train)
      train(user,namespace)
    end
    return true
   end
   return false
end

#deassociate_multi(tids, user, namespace, force_train = false) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/facer/faces.rb', line 117

def deassociate_multi(tids,user,namespace, force_train = false)
  out=[]
  tids.each do |tid|
    out += {"tid" => tid, "success" => deassociate(tid,user,namespace,false,detector)}
  end
  if(force_train)
     train(user,namespace)
  end
  return out
end

#detect(image, detector = :Normal) ⇒ Object

Detect if the provided image contains a face and, if yes, return face’s informations



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/facer/faces.rb', line 13

def detect(image,detector = :Normal)
  ret={}
  if(URI.parse(image).is_a?(URI::HTTP))
    ret=@linker.call("faces","detect",{"urls" => image, "detector" => detector.to_s})
  else
    ret=@linker.call("faces","detect",{"detector" => detector.to_s},{"upload" => image})
  end
  if(ret["status"].to_sym == :success && ret["photos"].length == 1)
    return ret["photos"][0]
  end
  return nil
end

#recognize(image, namespace, users = "", detector = :Normal) ⇒ Object

Attempt to recognize a user in a image end, if so, provide user and face infos



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/facer/faces.rb', line 47

def recognize(image,namespace,users = "",detector = :Normal)
  if(users.empty?)
    users="all@#{namespace}"
  end
  ret={}
  if(URI.parse(image).is_a?(URI::HTTP))
    ret=@linker.call("faces","recognize",{"urls" => image, "uids" => users, "detector" => detector.to_s, "namespace" => namespace})
  else
    ret=@linker.call("faces","recognize",{"uids" => users, "detector" => detector.to_s, "namespace" => namespace},{"upload" => image})
  end 
  if(ret["status"].to_sym == :success && ret["photos"].length == 1)
    img=ret["photos"][0]
    if(img["tags"].length == 1)
       tag=img["tags"][0]
       if(tag["uids"] && tag["uids"].length>0)
         return tag["uids"][0]
       else
         return {"confidence" => 0, "uid" => nil}
       end
    else
       return {"confidence" => -1, "uid" => nil}
    end
  end
  return nil
end

#train(user, namespace) ⇒ Object

Force train a user against the images/tags library



27
28
29
30
# File 'lib/facer/faces.rb', line 27

def train(user,namespace)
  ret=@linker.call("faces","train",{"uids" => "#{user}@#{namespace}"})
  return ret["status"].to_sym == :success
end

#train_all(namespace) ⇒ Object

Force train all users in a namespace (veeeery long operation)



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/facer/faces.rb', line 33

def train_all(namespace)
  namespaces=@linker..private_namespaces?
  if(!namespaces || !namespaces[namespace])
    return false
  end
  users=namespaces[namespace].users?
  success=true
  users.each do |user|
    success &= train(user,namespace)
  end
  return success
end