-
Notifications
You must be signed in to change notification settings - Fork 4
/
kb_haar_old.py
91 lines (71 loc) · 2.32 KB
/
kb_haar_old.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import rectangle
class HaarKB(object):
def __init__(self, history_length = 10, min_overlapping = 0.5):
# The history is a simple sequence of frames
self.history = []
self.history_length = history_length
self.min_overlapping = min_overlapping
def update(self, frame):
self.add_frame(frame)
if self.too_many_outliers() or not self.overlapping():
self.reset()
def faces_found(self):
"""
Count the number of detected faces in the
frame history.
"""
num_faces = sum([1 for (h,f) in self.history if f])
return num_faces
def overlapping(self):
if len(self.history) < 2:
return True
current = self.history[-1] # Last frame
father = self.history[-2] # Second to last frame
if current == None or father == None:
return True
# Unpack the hand from the frames
current_hand, current_face = current
father_hand, father_face = father
overlapping = rectangle.intersect_percentage(current_hand, father_hand)
return overlapping >= self.min_overlapping
def too_many_outliers(self):
# Check if the history is long enough for further analysis
if len(self.history) < 2:
return False
# Frames must intersect by a certain percentage to
# be valid. Only one outlier is allowed.
current = self.history[-1]
father = self.history[-2]
# Say if we have more than one outlier
return current == None and father == None
def add_frame(self, frame):
# Store the frame in knowledge base
self.history.append(frame)
# Only keep the last few frames
self.history = self.history[-self.history_length:]
def conf_face_detect(self):
f = self.faces_found()
def conf_hand_detect(self):
return len(self.history) / float(self.history_length)
def get_confidence(self):
"""
Determine the confidence by a number
of influential factors which are
- number of consecutive overlapping frames.
- faces found found during history
Basically, the confidence should be high, if we have
a consistent result over many frames.
"""
# TODO: WEIGHTED AVERAGE
conf = self.conf_hand_detect()
return conf
def reset(self):
self.history = []
if __name__ == "__main__":
# Test
kb = KnowledgeBase()
kb.update(1)
kb.update(1)
kb.update(None)
kb.update(None)
print kb.get_confidence()