-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmode2.py
165 lines (108 loc) · 4.18 KB
/
mode2.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Learning
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
import kivy.core.image
import kivy.uix.image
#from kivy.core.image import Image
from kivy.graphics import *
from lib.utils import ZoneOfInterest, AlphaWidget
import lib.config, lib.kwargs
# Configuration
MAX_POINTS = 400 # max points to discover the background
RATIO = 0.8 # ratio of theses points to jump the next mode
TIMEOUT_DELAY = 60 # before it jumps to next mode if not touched
########################################################################
class Learning(Widget):
def __init__(self, **kwargs):
lib.kwargs.set_kwargs(self, **kwargs)
super(Learning, self).__init__(**kwargs)
# Widget position
self.pos = (lib.config.viewport[self.clientIdIndex][0], 0)
self.width = lib.config.viewport[self.clientIdIndex][1]
self.background_path = "images/bgs/" + str(self.clientIdIndex + 1) + ".jpg"
self.threshold_reachedMessageSent = False
self.last_touch = 0
self.points = []
# ZoneOfInterest
self.shapes = []
for zi in lib.config.zones_of_interest[self.clientIdIndex]:
self.shapes.append( ZoneOfInterest( img=kivy.core.image.Image(zi[0]),
pos=zi[1],
alpha_index=1.0) )
rez = lib.config.viewport[self.clientIdIndex]
self.max_points = (MAX_POINTS * RATIO / 1024) * abs(rez[0] - rez[1])
# basis
def start(self):
print "Learning start() called"
self.add_shapes()
self.last_touch = Clock.get_time()
Clock.schedule_interval(self.checkTimeout, 5)
def stop(self):
print "Learning stop() called"
self.reset()
Clock.unschedule(self.checkTimeout)
def fadein(self):
pass
def fadeout(self):
pass
def reset(self):
self.threshold_reachedMessageSent = False
self.points = []
self.canvas.clear()
# Custom methods
def checkIfModeIsCompleted(self):
if len(self.points) >= self.max_points:
if not self.threshold_reachedMessageSent:
print "Max point reached with ", str(len(self.points))
# self.reset() # should be resetted when server send top command
self.controller.sendMessage("threshold_reached") # go to next mode
self.threshold_reachedMessageSent = True
def draw_ellipse(self, touch):
# Use this instead of "append" if you are displaying Point cloud.
# self.points += touch.pos
self.points.append(touch.pos)
# required, this fix a performance issue.
self.canvas.clear()
with self.canvas:
StencilPush()
# One easy solution
# Point(pointsize=20., points=self.points)
# This is slower than Point but still usable
# Todo: improve perfs by discarding position tuple already existing in the array.
for pos in self.points:
diameter = 100
Ellipse(pos=(pos[0] - diameter / 2, pos[1] - diameter / 2), size=(diameter, diameter))
StencilUse()
kivy.uix.image.Image(source=self.background_path, size=(1024,768), color=[1,1,1,1], pos=(0,0))
StencilPop()
self.add_shapes()
def add_shapes(self):
for shape in self.shapes:
self.canvas.add(shape.object)
def checkTimeout(self, dt=False):
if abs(Clock.get_time() - self.last_touch) > TIMEOUT_DELAY:
print "Timeout on mode 2, sending message to switch mode."
self.controller.sendMessage("threshold_reached")
# Custom Callbacks
# Kivy Callbacks
def on_touch_down(self, touch):
self.draw_ellipse(touch)
def on_touch_move(self, touch):
self.draw_ellipse(touch)
def on_touch_up(self, touch):
self.last_touch = Clock.get_time()
self.checkIfModeIsCompleted()
########################################################################
if __name__ == '__main__':
class LearningApp(App):
def build(self):
base = Widget()
learn = Learning()
base.add_widget(learn)
learn.start()
return base
LearningApp().run()