-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCluster2FindAnomalies.py
More file actions
160 lines (143 loc) · 6.22 KB
/
Cluster2FindAnomalies.py
File metadata and controls
160 lines (143 loc) · 6.22 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
# coding: utf-8
# In[155]:
import matplotlib
matplotlib.use('Agg')
import math
import matplotlib.pyplot as plt
import numpy
import scipy.cluster.hierarchy as hcluster
from sklearn.preprocessing import StandardScaler
import pandas as pd
import glob
import os
import numpy as np
import cv2
import sys
vid_name = sys.argv[1]
# In[182]:
# Velocity Trace - Fast Cars
for f in glob.glob('projections/' + vid_name + '_*.csv'):
# Get Video ID
vid_id = f.split("projections/")[1].split("_")[0]
# Read in Data
data = pd.read_csv(f, header=None)
if "fast_cars" in f:
continue
data.columns = ["Vid", "Object", "C"]
cluster_var = ['C']
save_name = "_fast_cars"
data_group_name = "_withVelocity.csv"
data_cols = ['cog_x','cog_y','frame','height','obj','velocity','width','x','y']
elif "slow_cars" in f:
continue
data.columns = ["Vid", "Object", "C"]
cluster_var = ['C']
save_name = "_slow_cars"
data_group_name = "_withVelocity.csv"
data_cols = ['cog_x','cog_y','frame','height','obj','velocity','width','x','y']
elif "maintain_velocity" in f:
data.columns = ["Vid", "Object", "C1", "C2"]
cluster_var = ['C1', 'C2']
save_name = "_maintain_velocity"
data_group_name = "_withVelocity.csv"
data_cols = ['cog_x','cog_y','frame','height','obj','velocity','width','x','y']
avg_c1 = data['C1'].mean()
avg_c2 = data['C2'].mean()
data = data[(data['C1'] > 30) & (data['C2'] < 30)]
elif "to_shoulder" in f or "to_lane" in f:
continue
data.columns = ["Vid", "Object", "w", "tau"]
cluster_var = ['w', 'tau']
save_name = "_to_shoulder" if "to_shoulder" in f else "to_lane"
data_group_name = "_withLanes.csv"
data_cols = ['height', 'width', 'y','x','cog_y','cog_x', 'obj', 'frame', 'lane']
elif "in_lane" in f:
continue
data.columns = ["Vid", "Object", "tau"]
cluster_var = ['tau']
save_name = "_in_lane"
data_group_name = "_withLanes.csv"
data_cols = ['height', 'width', 'y','x','cog_y','cog_x', 'obj', 'frame', 'lane']
else:
continue
# Cluster
X = np.asarray(data[cluster_var]).reshape((len(data[cluster_var[0]]), len(cluster_var)))
clusters = hcluster.fclusterdata(X, 1.5, criterion="distance")
# Save Cluster Plot and Cluster Results
data_results = data
data_results['Cluster'] = clusters
if len(cluster_var) == 1:
if save_name == "_in_lane":
plt.scatter(range(0, len(data['tau'])) ,data['tau'].values, c=clusters)
plt.xlabel('t')
plt.ylabel('tau')
else:
plt.scatter(range(0, len(data['C'])) ,data['C'].values, c=clusters)
plt.xlabel('t')
plt.ylabel('C')
else:
if data_group_name == "_withVelocity.csv":
plt.scatter(data['C1'].values, data['C2'].values, c=clusters)
plt.xlabel('C1')
plt.ylabel('C2')
else:
plt.scatter(data['w'].values, data['tau'].values, c=clusters)
plt.xlabel('w')
plt.ylabel('tau')
plt.savefig("cluster_plots/" + vid_id + save_name + ".png")
# Find Smallest Cluster
data_group = data[['Object', 'Cluster']].groupby(['Cluster']).agg(['count'])
data_group['Cluster'] = data_group.index
# Find Smallest Cluster if Velocity
if data_group_name == "_withVelocity.csv" and save_name != "_maintain_velocity":
smallest_cluster = [data_group.sort_values([('Object', 'count')])['Cluster'].iloc[0]]
# Get Object IDs
anomaly_objs = data['Object'][data['Cluster'].isin(smallest_cluster)]
elif save_name == "_in_lane":
# Get Object IDs
# anomaly_objs = data['Object'][data['tau'] > 0]
avg_tau = data['tau'].mean()
avg_std = data['tau'].std()
anomaly_objs = data['Object'][data['tau'] > (avg_tau + avg_std/2.0)]
elif save_name == "_maintain_velocity":
# avg_c1 = data['C1'].mean()
# avg_c2 = data['C2'].mean()
# data = data[(data['C1'] > avg_c1) & data['C2'] > avg_c2]
data_group = data[['Object', 'Cluster']].groupby(['Cluster']).agg(['count'])
data_group['Cluster'] = data_group.index
avg_cluster_size = data_group[('Object', 'count')].mean()
std_cluster_size = data_group[('Object', 'count')].std()
smallest_cluster = data_group[data_group[('Object', 'count')] < math.ceil(avg_cluster_size/2.0)]['Cluster'].values
# Get Object IDs
anomaly_objs = data['Object'][data['Cluster'].isin(smallest_cluster)]
else:
# Find Smaller than Average Clusters if Lane
avg_cluster_size = data_group[('Object', 'count')].mean()
std_cluster_size = data_group[('Object', 'count')].std()
smallest_cluster = data_group[data_group[('Object', 'count')] < abs(avg_cluster_size - (std_cluster_size/2.0))]['Cluster'].values
# Get Object IDs
anomaly_objs = data['Object'][data['Cluster'].isin(smallest_cluster)]
# Get X, Y, Height, Length, and Frame IDs
anomaly_obj_data = pd.read_csv("data/" + str(vid_id) + data_group_name, header=None)
anomaly_obj_data.columns = data_cols
anomaly_obj_data = anomaly_obj_data[['obj', 'x', 'y', 'height', 'width', 'frame']]
anomaly_obj_data = anomaly_obj_data[anomaly_obj_data['obj'].isin(anomaly_objs)]
# Save File with Object ID, X, Y, Height, Length, Frame IDs
anomaly_obj_data.to_csv("anomalies/" + str(vid_id) + save_name + ".csv")
# Draws Bounding Boxes
frames = anomaly_obj_data['frame'].unique()
for i in frames:
image_name = str(vid_id) + "/" + str(i) + '.png'
print image_name
img = cv2.imread(image_name)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
anomaly_cars_in_frame = anomaly_obj_data[anomaly_obj_data['frame'] == int(i)]
for index, row in anomaly_cars_in_frame.iterrows():
x1 = row['x']
y1 = row['y']
h = row['height']
w = row['width']
cv2.rectangle(img, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), (0, 0, 255), 3)
labeled_image_name = "anomaly_pics_mv/" + vid_id + "_" + str(i) + save_name + ".png"
cv2.imwrite(labeled_image_name, img)
# In[ ]: