-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_tracking.py
280 lines (237 loc) · 7.78 KB
/
human_tracking.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
# -*- coding: utf-8 -*-
"""Human tracking.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1hM9QFZZpqvyRM-cJDBIbulwIOy6-S8-U
#Contribution:
- Osose Itua
- Ali Ayub
- Tiffany Ngai
#Set up
"""
from sklearn.cluster import KMeans
from google.colab import drive
import numpy as np
from PIL import Image;
import cv2
from google.colab.patches import cv2_imshow
import os
import torch
import torch.nn
import torchvision
from torchvision import models, transforms
import torch
import torch.nn
import torchvision
from torchvision import models, transforms
from torch.utils.data import Dataset
drive.mount('/content/drive')
path = "/content/drive/MyDrive/Machine learning projects/Human tracking/video.avi"
"""#Preprocessing"""
# Crop sampled frames
roi1_region = (502, 344, 61, 86)
roi2_region = (627, 427, 193, 93)
roi3_region = (522, 654, 90, 85)
roi4_region = (704, 594, 90, 80)
roi5_region = (545, 577, 81, 75)
roi6_region = (460, 700, 52, 80)
X_roi1 = []
X_roi2 = []
X_roi3 = []
X_roi4 = []
X_roi5 = []
X_roi6 = []
for i, icurrentframe in enumerate(range(0, 13501, 1)):
image = cv2.imread('/content/drive/MyDrive/Machine learning projects/Human tracking/video_frames/frame' + str(icurrentframe) + '.jpg');
roi1 = image[344:430, 502:563, :]
roi2 = image[427:520, 627:820, :]
roi3 = image[654:739, 522:612, :]
roi4 = image[594:647, 704:794, :]
roi5 = image[577:652, 545:626, :]
roi6 = image[700:780, 460:512, :]
X_roi1.append(roi1)
X_roi2.append(roi2)
X_roi3.append(roi3)
X_roi4.append(roi4)
X_roi5.append(roi5)
X_roi6.append(roi6)
cv2.imwrite('/content/drive/MyDrive/Machine learning projects/Human tracking/ROI1/Frame' + str(icurrentframe) + '.jpg', roi1);
cv2.imwrite('/content/drive/MyDrive/Machine learning projects/Human tracking/ROI2/Frame' + str(icurrentframe) + '.jpg', roi2);
cv2.imwrite('/content/drive/MyDrive/Machine learning projects/Human tracking/ROI3/Frame' + str(icurrentframe) + '.jpg', roi3);
cv2.imwrite('/content/drive/MyDrive/Machine learning projects/Human tracking/ROI4/Frame' + str(icurrentframe) + '.jpg', roi4);
cv2.imwrite('/content/drive/MyDrive/Machine learning projects/Human tracking/ROI5/Frame' + str(icurrentframe) + '.jpg', roi5);
cv2.imwrite('/content/drive/MyDrive/Machine learning projects/Human tracking/ROI6/Frame' + str(icurrentframe) + '.jpg', roi6);
# Convert list to np array
X_roi1 = np.array(X_roi1)
X_roi2 = np.array(X_roi2)
X_roi3 = np.array(X_roi3)
X_roi4 = np.array(X_roi4)
X_roi5 = np.array(X_roi5)
X_roi6 = np.array(X_roi6)
print(X_roi1.shape)
cuda0 = torch.device('cpu')#('cuda:0')
model = torchvision.models.resnet18(pretrained=True)
newmodel = torch.nn.Sequential(*(list(model.children())[:-1]))
newmodel.to(cuda0)
newmodel.eval()
class getTransformedData(Dataset):
"""transformed dataset
"""
def __init__(self, images, transform=None):
self.train_images = images
self.train_images = np.array(self.train_images)
#if transform is given, we transoform data using
self.transform = transform
def __len__(self):
return 1
def __getitem__(self, index):
image = self.train_images[index]
if self.transform:
image = self.transform(np.uint8(image))
return image
my_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485,.456,.406],
std=[.229,.224,.225])
])
# Generate feature vectors
X_roi1_fv = []
X_roi2_fv = []
X_roi3_fv = []
X_roi4_fv = []
X_roi5_fv = []
X_roi6_fv = []
for i in range(len(X_roi1)):
images = X_roi1[i]
images = my_transforms(images)
images = torch.unsqueeze(images,0)
images = images.to(cuda0)
out = newmodel(images)
out = out.view(-1,)
X_roi1_fv.append(out.detach().numpy())
X_roi1_fv = np.array(X_roi1_fv)
print (X_roi1_fv.shape)
for i in range(len(X_roi2)):
images = X_roi2[i]
images = my_transforms(images)
images = torch.unsqueeze(images,0)
images = images.to(cuda0)
out = newmodel(images)
out = out.view(-1,)
X_roi2_fv.append(out.detach().numpy())
X_roi2_fv = np.array(X_roi2_fv)
print (X_roi2_fv.shape)
for i in range(len(X_roi3)):
images = X_roi3[i]
images = my_transforms(images)
images = torch.unsqueeze(images,0)
images = images.to(cuda0)
out = newmodel(images)
out = out.view(-1,)
X_roi3_fv.append(out.detach().numpy())
X_roi3_fv = np.array(X_roi3_fv)
print (X_roi3_fv.shape)
for i in range(len(X_roi4)):
images = X_roi4[i]
images = my_transforms(images)
images = torch.unsqueeze(images,0)
images = images.to(cuda0)
out = newmodel(images)
out = out.view(-1,)
X_roi4_fv.append(out.detach().numpy())
X_roi4_fv = np.array(X_roi4_fv)
print (X_roi4_fv.shape)
for i in range(len(X_roi5)):
images = X_roi5[i]
images = my_transforms(images)
images = torch.unsqueeze(images,0)
images = images.to(cuda0)
out = newmodel(images)
out = out.view(-1,)
X_roi5_fv.append(out.detach().numpy())
X_roi5_fv = np.array(X_roi5_fv)
print (X_roi5_fv.shape)
for i in range(len(X_roi6)):
images = X_roi6[i]
images = my_transforms(images)
images = torch.unsqueeze(images,0)
images = images.to(cuda0)
out = newmodel(images)
out = out.view(-1,)
X_roi6_fv.append(out.detach().numpy())
X_roi6_fv = np.array(X_roi6_fv)
print (X_roi6_fv.shape)
"""#Clustering"""
# Clustering - ROI1 - TODO: Try sklearn.mixture.GaussianMixture
N_CLUSTER = 2
kmeans = KMeans(n_clusters=N_CLUSTER, random_state=0).fit(X_roi1_fv)
roi1_result = kmeans.labels_
# Clustering - ROI2
N_CLUSTER = 3
kmeans = KMeans(n_clusters=N_CLUSTER, random_state=0).fit(X_roi2_fv)
roi2_result = kmeans.labels_
# Clustering - ROI3
N_CLUSTER = 2
kmeans = KMeans(n_clusters=N_CLUSTER, random_state=0).fit(X_roi3_fv)
roi3_result = kmeans.labels_
# Clustering - ROI4
kmeans = KMeans(n_clusters=N_CLUSTER, random_state=0).fit(X_roi4_fv)
roi4_result = kmeans.labels_
# Clustering - ROI5
kmeans = KMeans(n_clusters=N_CLUSTER, random_state=0).fit(X_roi5_fv)
roi5_result = kmeans.labels_
# Clustering - ROI6
kmeans = KMeans(n_clusters=N_CLUSTER, random_state=0).fit(X_roi6_fv)
roi6_result = kmeans.labels_
#Print a couple images per class for visual inspection
def visualizeClusters(result):
num_clusters = len(np.unique(result))
clusters_ind = [[] for _ in range(num_clusters)]
print(clusters_ind)
for i in range(len(result)):
for icluster in range(num_clusters):
if result[i] == icluster:
clusters_ind[icluster].append(i) # save index
return clusters_ind
def findCov(clusters):
cov = np.zeros((len(clusters),))
for icluster in range(len(clusters)):
cov[icluster] = np.var(feature_vector[clusters[icluster]])
return cov
feature_vector = X_roi1_fv
cluster1 = visualizeClusters(roi1_result)
print(findCov(cluster1))
print(len(cluster1[0]))
print(len(cluster1[1]))
feature_vector = X_roi2_fv
cluster2 = visualizeClusters(roi2_result)
print(findCov(cluster2))
print(len(cluster2[0]))
print(len(cluster2[1]))
print(len(cluster2[2]))
feature_vector = X_roi3_fv
cluster3 = visualizeClusters(roi3_result)
print(findCov(cluster3))
print(len(cluster3[0]))
print(len(cluster3[1]))
feature_vector = X_roi4_fv
cluster4 = visualizeClusters(roi4_result)
print(findCov(cluster4))
print(len(cluster4[0]))
print(len(cluster4[1]))
feature_vector = X_roi5_fv
cluster5 = visualizeClusters(roi5_result)
print(findCov(cluster5))
print(len(cluster5[0]))
print(len(cluster5[1]))
feature_vector = X_roi6_fv
cluster6 = visualizeClusters(roi6_result)
print(findCov(cluster6))
print(len(cluster6[0]))
print(len(cluster6[1]))
frame = 4110
image = cv2.imread('/content/drive/MyDrive/Machine learning projects/Human tracking/Frames/Frame' + str(frame) + '.jpg');
cv2_imshow(image)