-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
326 lines (269 loc) · 11.4 KB
/
__main__.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
Script utilizing code in overtaking package
"""
#! /usr/bin/python3
# --------- modularity ----------
import os
import statistics
import matplotlib.pyplot as plt
import networkx as nx
from scipy import stats
from src import box, constants, util
from src import modularity as mod
dflist = util.get_box_files(constants.DATA_HOME, "txt")
util.ensure_date_in_filenames(dflist)
dflist = util.get_box_files(constants.DATA_HOME, "txt")
all_ot_events = []
all_oc_events = []
ot_lds = []
oc_lds = []
for csv_file in dflist:
print(csv_file, flush=True)
date_string = csv_file.split("/")[-1][:8]
if date_string == "20240330":
continue
csvr = util.read_csv(csv_file)
ldata = box.make_ldata(csvr) # CSV data as a list
# presses
press_starts, press_lengths = box.get_press_lengths_and_starts(ldata)
for pl in press_lengths:
if pl < 10:
total_num_OC += 1
b_partitions, a_partitions, b_modularities, a_modularities = mod.get_partitions(ldata, press_starts, press_lengths)
oc_events = mod.get_oc_events(date_string, ldata, press_starts, a_partitions, a_modularities)
all_oc_events.extend(oc_events)
# ot_events = mod.get_ot_events(date_string, ldata, press_starts, b_partitions, b_modularities)
# all_ot_events.extend(ot_events)
# util.write_to_csv_file(os.path.join("data", "ot_events.csv"), all_ot_events)
util.write_to_csv_file(os.path.join("data", "oc_events.csv"), all_oc_events)
"""
print("Number of OT events:", len(ot_events))
lds = [ote[-2] for ote in all_ot_events]
lds_trimmed_means = [stats.trim_mean(ld, 0.1)-20 for ld in lds]
lds_trimmed_mins = []
for ld in lds:
a = round(0.1*len(ld))
ld.sort()
m = min(ld[a:])-20 # adjust for position of box (not on the outer side of the rider)
lds_trimmed_mins.append(m)
lds_trimmed_mins.sort()
count100 = 0
count150 = 0
for ld in lds_trimmed_mins:
if ld < 150:
count150 += 1
if ld < 100:
count100 += 1
print("count100 =", count100)
print("count150 =", count150)
print("median =", statistics.median(lds_trimmed_mins))
# run some basic stats & figs
# plt.hist(lds_trimmed_mins, bins=50)
# plt.show()
"""
"""
# --------- vehicle recognition from video ----------
import os
from ultralytics import YOLO
import pandas as pd
from src import detect
IN_FOLDER = "data/trainingPix"
OUT_FOLDER = "out"
model = YOLO("models/yolov8x.pt") # load a pretrained model (xlarge) from ultralytics (YOLOv8)
YOLO_LABELS = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
# our_labels = {2: 'car', 3: 'motorcycle', 5: 'bus', 6: 'train', 7: 'truck'}
# https://docs.ultralytics.com/modes/predict/#inference-arguments
results = model.predict(source=IN_FOLDER, save=True, save_conf=True, project=OUT_FOLDER, name="annotated_pics", classes=[2,3,5,6,7])
gt_box = [356.90081787109375, 509.1054992675781, 590.26416015625, 710.7196655273438]
lst = []
print("num results:", len(results))
for result in results:
boxes = result.boxes.cpu().numpy()
for box in boxes:
cls = int(box.cls[0])
path = result.path
class_name = YOLO_LABELS[cls]
conf = int(box.conf[0]*100)
# 'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right
# 'xywh' : boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
# 'cxcywh' : boxes are represented via centre, width and height, cx, cy being center of box, w, h being width and height.
bx = box.xyxy.tolist()
mudguard = 0
print(bx, gt_box)
if detect.IoU(bx[0], gt_box) > 0.5:
mudguard = 1
df = pd.DataFrame({'mudguard': mudguard, 'class_name': class_name, 'class_id': cls, 'confidence': conf, 'box_coord': bx, 'path': path})
lst.append(df)
df = pd.concat(lst)
# Save the predicted text file to disk
df.to_csv(os.path.join(OUT_FOLDER,"predicted_labels.csv"), index=False)
os.rename(os.path.join(OUT_FOLDER,"predicted_labels.csv"), os.path.join("data", "predicted_labels.csv"))
"""
"""
# --------------- code related to the box --------------
from collections import Counter
import csv
import os
import ast
import json
import matplotlib.pyplot as plt
import networkx as nx
from src import box, constants, radar, util, detect
# ------------
# address lat. distances that are too small, e.g. in the following event
# d = [510, 203, 205, 134, 200, 200, 200, 198, 195, 195, 78, 195, 195, 193, 60, 40, 129, 193, 193, 144, 198, 195, 116, 195, 195, 195, 160, 195, 152]
# IDEA: convert the list to a graph: each lat dist is a vertex. If two
# vertices are close to each other (e.g. <40cm), add an edge between
# them. Then detect a community with Lovain algorithm based on
# modularity measure. Keep the biggest component.
my_events = box.read_events_from_csvfile("data/events.csv")
ot_events = [e for e in my_events if e[0] == 1] # overtaking only
k = 0
skipped = 0
skipped_edges = 0
min_overtaking_dist = []
for event in ot_events:
distances = event[-1] # list of lat. distances for that event
# print(".", end='', flush=True)
if len(distances) == 1:
print(event)
# louvain can't handle 1 vertex graph
skipped += 1
k += 1
continue
nodes = range(len(distances))
G = nx.Graph()
G.add_nodes_from(nodes)
for i in nodes:
for j in nodes:
# self-loops mess it up
if i != j and abs(distances[i] - distances[j]) < 40:
G.add_edge(i,j)
if len(G.edges()) == 0:
# can't divide by 0
skipped_edges += 1
k += 1
continue
c = nx.community.louvain_communities(G)
lengths_c = [len(x) for x in c]
ix = lengths_c.index(max(lengths_c))
dists = []
nds = []
for i in c[ix]:
dists.append(distances[i])
nds.append(i)
min_overtaking_dist.append(min(dists))
#plt.hist(min_overtaking_dist, alpha=0.5, label='overtaking', bins=50)
#plt.savefig("figures/OT_filtered_events-hist.png")
# generate scatter plots for each OT event highlighting the lat.dist. values we keep
plt.scatter(nodes, distances, c='b')
plt.scatter(nds, dists, c='r')
plt.ylim([100,500])
plt.savefig(os.path.join("out", "figs", str(event[3])+"_"+str(event[4])+"_"+str(event[5])+".png"))
plt.clf()
k += 1
print()
print("skipped:", skipped)
print("skipped (no edges):", skipped_edges)
print("total:", k)
min_overtaking_dist.sort()
print("min OT distances for each event, sorted:\n", min_overtaking_dist)
"""
"""
# ----------------- press lengths --------------
# Load or compile tally of press lengths (bin size = 1)
PRESSES_JSON = "data/press_lengths_counts.json"
PRESSES_PNG = "figures/press_lengths_hist.png"
# Create a tally of press lengths
# (to see if there is a clear distinction between short and long presses)
if os.path.exists(PRESSES_JSON):
print("Using press lengths (historgram) from: ", PRESSES_JSON)
with open(PRESSES_JSON, encoding="utf-8") as f:
data = f.read()
d = ast.literal_eval(data)
print(d)
else:
print("File ", PRESSES_JSON, "does not exit. Compiling info and creating the file.")
my_events = box.collate_events()
press_lengths = [event[2] for event in my_events[1:]] # press length sits at index 2 in an event
c = Counter(press_lengths)
with open(PRESSES_JSON, "w", encoding="utf-8") as outfile:
json.dump(c, outfile)
# while at it, generate the Figure with histogram
plt.hist(press_lengths, bins=range(1,max(press_lengths)), label='press lengths')
plt.legend(loc='upper right')
plt.savefig(PRESSES_PNG)
my_events = box.collate_events()
for event in my_events:
print(event)
print("Found", len(my_events), "events.")
"""
"""
# -------------------------- TEST box.py ----------------------------
# extract events from the data
import os
from src import box, util
# [1, 2, 9, 20230112, '19:48:17', 94962, 1, [365]]
# [1, 4, 29, 20221227, '14:33:53', 387031, 1, [243]]
# [1, 0, 28, 20221224, '11:02:56', 123439, 1, [144]]
# [1, 1, 9, 20221218, '09:29:42', 2104, 1, [370]]
# [1, 0, 37, 20230119, '08:58:13', 155376, 1, [462]]
# [1, 3, 8, 20230219, '10:05:38', 164892, 1, [368]]
# [1, 3, 9, 20230219, '10:42:52', 211805, 1, [485]]
# [1, 0, 31, 20230219, '12:59:49', 384818, 1, [71]]
# [1, 3, 8, 20230305, '09:01:37', 168094, 1, [386]]
# [1, 0, 38, 20230318, '12:08:04', 95322, 1, [91]]
# [1, 0, 38, 20230318, '12:42:53', 139226, 1, [106]]
my_events = box.collate_events()
util.write_to_csv_file(os.path.join("data", "events.csv"), my_events)
for event in my_events:
if event[-2] == 0:
print(event)
# plot hist of minimum overtaking distances (one for each identified event)
# min_overtaking_dist = []
# min_oncoming_dist = []
# for event in my_events[1:]:
# if event[0] == 1:
# min_overtaking_dist.append(min(event[-1]))
# elif event[0] == -1:
# min_oncoming_dist.append(min(event[-1]))
# plt.hist(min_overtaking_dist, alpha=0.5, label='overtaking', bins=50)
# plt.hist(min_oncoming_dist, alpha=0.5, label='oncoming', bins=50)
# plt.legend(loc='upper right')
# plt.savefig("figures/OT-vs-OC_events-hist.png")
"""
"""
# -------------------------- TEST fit.py ----------------------------
import datetime
from src import fit, util, constants
filenames_list = util.get_box_files(constants.DATA_HOME, "fit")
ride_durations_wahoo = []
ride_distances_wahoo = []
for filename in filenames_list:
print(filename)
msgs = fit.fit_file_messages(filename)
session = fit.fit_get_session(msgs)
if "Wahoo" in filename:
ride_durations_wahoo.append(session[0]["total_elapsed_time"])
ride_distances_wahoo.append(session[0]["total_distance"])
print("total durations (wahoo):", str(datetime.timedelta(seconds = sum(ride_durations_wahoo))))
print("total distances (wahoo):", sum(ride_distances_wahoo))
"""
"""
# -------------------------- TEST radar.py ----------------------------
from src import radar
#radar.radar_decode()
all_events = radar.radar_unload()
radar.radar_events_to_csv("radar_out.csv", all_events)
#radar.plot_radar_data()
"""
"""
# -------------------------- TEST util.py ----------------------------
for f in util.get_box_files(constants.DATA_HOME, "TXT"):
print(f)
print()
util.ensure_date_in_filenames(util.get_box_files(constants.DATA_HOME))
print("Added date to filenames whenever it was missing:")
for f in util.get_box_files(constants.DATA_HOME):
print(f)
"""