-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry_detect.py
More file actions
287 lines (200 loc) · 8.97 KB
/
entry_detect.py
File metadata and controls
287 lines (200 loc) · 8.97 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import datetime
import sys, time
sys.path.append("../catkin_ws/src/scr_control/scripts/lights")
sys.path.append("../catkin_ws/src/scr_control/scripts/time_of_flight")
import SCR_OctaLight_client as light_control
import SCR_TOF_client as tof
def tof_pixel_check(tof_id, loop_count=10, freq=0.00001, threshold=500, bar_chart=True, grid=True, bar_fn="ToF_bar_chart.png", grid_fn="ToF_grid_figure.png"):
tof_data_list = tof.get_distances(tof_id)
change_freq_matrix = [[[] for j in range(len(tof_data_list[0]))] for i in range(len(tof_data_list))]
# store the current data in the ToF matrix
tof_origin = tof_data_list
#time.sleep(0.1)
# detect the new data from the ToF sensor in the loop
# and return the changes of data
for itr_number in range(0, loop_count):
max_change = 0
max_change_i = 0
max_change_j = 0
max_change_output = ""
# create a new matrix to store the changes of data
data_change_matrix = [[0]*len(tof_data_list[0]) for i in range(len(tof_data_list))]
# read the new data matrix
tof_data_list = tof.get_distances(int(tof_id))
# calculate and store the changes
for i in range(0, len(tof_data_list)):
for j in range(0, len(tof_data_list[0])):
data_change_matrix[i][j] = abs(tof_data_list[i][j] - tof_origin[i][j])
if itr_number != 0:
(change_freq_matrix[i][j]).append(data_change_matrix[i][j])
tof_origin = tof_data_list
#time.sleep(freq)
# calcualte the average changes for each pixel
mean_change = []
for i in range(0, len(change_freq_matrix)):
for j in range(0, len(change_freq_matrix[0])):
each_pixel_change = []
mean_change_one_pixel = sum(change_freq_matrix[i][j])/len(change_freq_matrix[i][j])
maximum_change = max(change_freq_matrix[i][j])
each_pixel_change.append(mean_change_one_pixel)
each_pixel_change.append(maximum_change)
each_pixel_change.append(i)
each_pixel_change.append(j)
mean_change.append(each_pixel_change)
# sort the mean_change matrix
sorted_mean_change = sorted(mean_change, key=lambda x: -x[0])
# Plot the bar chart
if bar_chart:
# plot the maximum_change and mean_change amoung these pixels
plot_max_list_x = []
plot_max_list_y = []
for d in sorted_mean_change:
plot_max_list_x.append(str(d[2])+str(d[3]))
plot_max_list_y.append(int(d[1])) # insert the max_changes
plt.bar(plot_max_list_x, plot_max_list_y)
# Add labels
plt.xlabel('Pixel locations')
plt.ylabel('Average changes')
plt.title('Average changes of ToF sensor {}'.format(tof_id))
# Save the chart
plt.savefig(bar_fn)
# Plot the grid
if grid:
# Create a new martix to store the max changes values
max_change_matrix = [[0 for j in range(20)] for i in range(25)]
for i in range(0, len(change_freq_matrix)):
for j in range(0, len(change_freq_matrix[0])):
max_change_matrix[i][j] = max(change_freq_matrix[i][j])
# Create a new figure and axis object
fig, ax = plt.subplots()
# Plot the matrix as an image
im = ax.imshow(max_change_matrix, cmap='coolwarm')
# Add a colorbar
cbar = ax.figure.colorbar(im, ax=ax)
# Loop over the matrix and add text annotations
for i in range(25):
for j in range(20):
text = ax.text(j, i, max_change_matrix[i][j],
ha="center", va="center",
fontsize = 5,
fontweight='bold' if max_change_matrix[i][j] >= threshold else 'normal',
color="black" if max_change_matrix[i][j] < threshold else "white")
# Set the axis labels
ax.set_xticks(range(20))
ax.set_yticks(range(25))
# Set the tick labels
ax.set_xticklabels(range(1, 26))
ax.set_yticklabels(range(1, 21))
# Set the axis labels
ax.set_xlabel("Column")
ax.set_ylabel("Row")
# Set the title
ax.set_title("ToF Matrix Visualization")
# Add the threshold value as text outside the matrix
ax.text(1.32, 1.0, "Threshold:\n {}".format(threshold),
transform=ax.transAxes,
fontsize=10,
ha='left',
va='center')
# Save the figure to a file
plt.savefig(grid_fn)
# Return the matrix of seleted pixel
selected_pixel = np.zeros((25,20))
for i in range(0, len(max_change_matrix)):
for j in range(0, len(max_change_matrix[0])):
if max_change_matrix[i][j] < threshold:
selected_pixel[i, j] = True
else:
selected_pixel[i, j] = False
return selected_pixel
def plot_data(tof_id):
tof_data_mat = tof.get_distances(int(tof_id))
# Create a new figure and axis object
fig, ax = plt.subplots()
# Plot the matrix as an image
im = ax.imshow(tof_data_mat, cmap='coolwarm')
# Add a colorbar
cbar = ax.figure.colorbar(im, ax=ax)
# Loop over the matrix and add text annotations
for i in range(25):
for j in range(20):
text = ax.text(j, i, tof_data_mat[i][j],
ha="center", va="center",
fontsize = 4)
# Set the axis labels
ax.set_xticks(range(20))
ax.set_yticks(range(25))
# Set the tick labels
ax.set_xticklabels(range(1, 21))
ax.set_yticklabels(range(1, 26))
# Set the axis labels
ax.set_xlabel("Column")
ax.set_ylabel("Row")
# Set the title
ax.set_title("ToF Data Matrix")
# Save the figure to a file
plt.savefig("ToF #{} Data".format(tof_id))
def entry_detect(start=False):
if start == False:
return None
while(1):
# read the new data
tof_data_list = tof.get_distances(0)
trigger = False
for pixel in tof_data_list[24]:
if pixel <= 2500:
trigger = True
break
if trigger == False:
continue
# If the data read by door-side pixels is lower than 2500
# This means someone may get into the room.
# Then, in 1 second period, detect the change of each pixel,
# If there are more than 5 pixels have a change lower than
# 700, the lights will turn on.
tof_data_origin = tof_data_list # Store the old values
start_time = time.time() # Get the start time
pixel_change_count = 0 # Count the number of pixels with a high change
while (time.time() - start_time) < 1: # Keep running in 1 second
tof_data_list = tof.get_distances(0) # Update the values
# create a new matrix to store the changes of data
data_change_matrix = np.zeros((25,20))
# calculate and store the changes
for i in range(20, 25):
for j in range(0, 20):
change = tof_data_list[i][j] - tof_data_origin[i][j]
data_change_matrix[i, j] = change
if(change <= -600):
pixel_change_count += 1
if pixel_change_count >= 5:
end_time = time.time()
return True, end_time-start_time
tof_data_origin = tof_data_list
if __name__ == "__main__":
pixel_pick_mat = tof_pixel_check(int(sys.argv[1]),bar_chart=False)
print("System Initialized")
plot_data(int(sys.argv[1]))
#print(light_control.get_lights())
# Open the file for writing
with open("output.txt", "w") as log_file:
while(1):
turn_on_lights, time_used = entry_detect(True)
if turn_on_lights:
#print(time_used)
current_time = datetime.datetime.now()
log_file.write("Turn on the lights at: {}\n\n".format(current_time))
'''
light_control.cct(0, 2, 3500, 1000)
light_control.cct(0, 0, 3500, 1000)
light_control.cct(1, 1, 3500, 1000)
light_control.cct(2, 0, 3500, 1000)
light_control.cct(2, 2, 3500, 1000)
light_control.cct(3, 0, 3500, 500)
light_control.cct(3, 2, 3500, 500)
'''
#test = tof.get_distances(int(0))
#print(len(test), len(test[0]))