-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_samples.py
More file actions
370 lines (320 loc) · 15.3 KB
/
make_samples.py
File metadata and controls
370 lines (320 loc) · 15.3 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from threading import Thread
import cv2, time
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
from engine import train_one_epoch, evaluate
import utils
import transforms as T
import os
import numpy as np
import torch
import torch.utils.data
from PIL import Image
def get_instance_segmentation_model(num_classes):
# load an instance segmentation model pre-trained on COCO
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
# get the number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
# now get the number of input features for the mask classifier
in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
hidden_layer = 256
# and replace the mask predictor with a new one
model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
hidden_layer,
num_classes)
return model
def get_transform(train):
transforms = []
# converts the image, a PIL image, into a PyTorch Tensor
transforms.append(T.ToTensor())
if train:
# during training, randomly flip the training images
# and ground-truth for data augmentation
transforms.append(T.RandomHorizontalFlip(0.5))
return T.Compose(transforms)
class VideoStreamWidget(object):
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(.01)
def show_frame(self):
# Display frames in main program
cv2.imshow('frame', self.frame)
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
cv2.destroyAllWindows()
exit(1)
device = torch.device('cuda')
class MaskRCNN_maniuplator(object):
def __init__(self, video_stream):
self.video_stream_widget=video_stream
self.masks = None
self.new_mask_available = False
self.mask_accuracy = 0.5
# our dataset has two classes only - background and person
num_classes = 2
# get the model using our helper function
self.model = get_instance_segmentation_model(num_classes)
# move model to the right device
self.model.to(device)
resume = 'best_checkpoint_new.tar'
checkpoint = torch.load(resume)
self.model.load_state_dict(checkpoint['state_dict'])
self.model.eval()
# print("=> loaded checkpoint '{}' (epoch {})" .format(resume, checkpoint['epoch']))
def start(self):
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
counter = 0
start = time.time()
while True:
if self.video_stream_widget.capture.isOpened():
if self.video_stream_widget.status:
counter +=1
curr = time.time()
if(curr - start > 1):
print("Current FPS: ", counter /(curr - start))
counter = 0
start = time.time()
photo = cv2.cvtColor(self.video_stream_widget.frame, cv2.COLOR_BGR2RGB)
result_feed = photo *(1.0/255.0)
result_feed = result_feed.transpose(2,1,0)
with torch.no_grad():
prediction = self.model([torch.as_tensor(result_feed, dtype=torch.float32).to(device)])
# pix = img.mul(255).permute(1, 2, 0).byte().numpy()
mask = prediction[0]['masks'].permute(1, 0, 3, 2)[0].cpu().detach().numpy()
boxes = prediction[0]['boxes'].cpu().detach().numpy()
scores = prediction[0]['scores'].cpu().detach().numpy()
# print(boxes[0])
# print(mask.shape[0])
if(mask.shape[0] > 0):
self.masks = mask[0][int(boxes[0][0]):int(boxes[0][2]),int(boxes[0][1]):int(boxes[0][3])]
self.new_mask_available = True
# print(self.masks.shape)
# cv2.rectangle(self.masks, (boxes[0][1], boxes[0][0]), (boxes[0][3], boxes[0][2]), 255, 5)
self.out_mask = mask[0]
cv2.rectangle(self.out_mask, (boxes[0][1], boxes[0][0]), (boxes[0][3], boxes[0][2]), 1, 5)
# for j in range(boxes.shape[0]):
# print(boxes[j])
# if(scores[j] > 0.9):
# cv2.rectangle(self.out_mask, (boxes[j][1], boxes[j][0]), (boxes[j][3], boxes[j][2]), 1, 5)
# for j in range(1, mask.shape[0]):
# # if(scores[j] > self.mask_accuracy):
# self.masks = self.masks + mask[j]
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score
from itertools import chain
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from pynput.keyboard import Key, Controller
import math
keyboard = Controller()
currentValue = 7
valuesMap = {
1: (Key.right,),
2: (Key.right, Key.up),
3: (Key.right, Key.down),
4: (Key.left,),
5: (Key.left, Key.up),
6: (Key.left, Key.down),
7: (Key.up, '/'),
8: (Key.down, '/'),
9: (Key.right, '/'),
10: (Key.left, '/'),
}
class Main_Classificator(object):
def __init__(self, maskrcnn_manipulator_object):
self.maskrcnn_manipulator_object = maskrcnn_manipulator_object
data, labels = self.readImages()
features = self.getFeatures(data)
self.mean = np.mean(features, axis=0)
self.std = np.std(features, axis=0)
features = self.normalize(features)
self.pca = PCA(n_components=0.95)
features = self.pca.fit_transform(features)
print(self.pca.explained_variance_ratio_)
x_tr, x_tst, y_tr, y_tst = train_test_split(features, labels, test_size = 0.3)
self.model_SVC = SVC(kernel='linear')
self.model_SVC.fit(x_tr, y_tr)
Z = self.model_SVC.predict(x_tst)
print(confusion_matrix(y_tst, Z))
print(accuracy_score(y_tst, Z, normalize=True))
def start(self):
self.thread = Thread(target=self.classify, args=())
self.thread.daemon = True
self.thread.start()
def processImage(self, imgdata, purple = False, HSVdata = None):
hsv_light_purple = np.array([100, 100, 100])
hsv_dark_purple = np.array([120, 200, 200])
if purple:
hsv_light_purple = np.array([155, 110, 0])
hsv_dark_purple = np.array([180, 255, 255])
elif HSVdata is not None:
hsv_light_purple = np.array(HSVdata[0:3])
hsv_dark_purple = np.array(HSVdata[3:6])
hsv = cv2.cvtColor(cv2.resize(imgdata, (640, 360)), cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, hsv_light_purple, hsv_dark_purple)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
if(len(contours) == 0):
return np.zeros((30, 30))
longest_contour = contours[0]
for c in contours:
if(len(c) > len(longest_contour)):
longest_contour = c
H1_contours = np.zeros_like(mask)
cv2.drawContours(H1_contours, [longest_contour], 0, 255, -1)
# cv2.drawContours(H low_contours, contours, -1, (0,255,0), 2, hierarchy=hierarchy, maxLevel = 1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
dilation = cv2.dilate(H1_contours ,kernel, iterations = 3)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8,8))
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
contours, hierarchy = cv2.findContours(closing, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours_poly = [None]*len(contours)
boundRect = [None]*len(contours)
centers = [None]*len(contours)
radius = [None]*len(contours)
for i, c in enumerate(contours):
contours_poly[i] = cv2.approxPolyDP(c, 3, True)
boundRect[i] = cv2.boundingRect(contours_poly[i])
centers[i], radius[i] = cv2.minEnclosingCircle(contours_poly[i])
crop_img = closing[int(boundRect[-1][1]):int(boundRect[-1][1])+boundRect[-1][3], \
int(boundRect[-1][0]):int(boundRect[-1][0])+boundRect[-1][2]]
return cv2.resize(crop_img, (60, 60))
def readImages(self):
images = []
labels = []
for index, data in enumerate(os.walk("data/")):
if(len(data[2]) is not 0):
print(index, data[0], data[1])
for img in data[2]:
imgdata = cv2.imread(data[0] + "/" + img)
# photo = cv2.cvtColor(imgdata, cv2.COLOR_BGR2RGB)
# result_feed = photo *(1.0/255.0)
# result_feed = result_feed.transpose(2,1,0)
# with torch.no_grad():
# prediction = self.maskrcnn_manipulator_object.model([torch.as_tensor(result_feed, dtype=torch.float32).to(device)])
# # pix = img.mul(255).permute(1, 2, 0).byte().numpy()
# mask = prediction[0]['masks'].permute(1, 0, 3, 2)[0].cpu().detach().numpy()
# boxes = prediction[0]['boxes'].cpu().detach().numpy()
# scores = prediction[0]['scores'].cpu().detach().numpy()
# print(scores)
# out_mask = mask[0]
# if(mask.shape[0] > 0):
# for j in range(boxes.shape[0]):
# if(scores[j] > 0.9):
# cv2.rectangle(out_mask, (boxes[j][1], boxes[j][0]), (boxes[j][3], boxes[j][2]), 1, 5)
# for j in range(1, mask.shape[0]):
# if(scores[j] > 0.9):
# out_mask = out_mask + mask[j]
# imgdata[out_mask > 0.5] = 255
# cv2.imshow("tmpwindow", imgdata)
# key = cv2.waitKey(-1)
# if key == ord('q'):
# cv2.destroyAllWindows()
# exit(1)
outImg = self.processImage(imgdata, purple=True)
images.append(outImg)
labels.append(index)
return images, labels
def getFeatures(self,data):
contours = [ cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[0] for img in data]
c_area = np.array([cv2.contourArea(c[-1]) for c in contours])
c_len = np.array([cv2.arcLength(c[-1],True) for c in contours])
Moments = [cv2.moments(img) for img in data]
HuMoments = [cv2.HuMoments(mom) for mom in Moments]
centres =[(M["m10"]/M["m00"], M["m01"]/M["m00"]) for M in Moments]
Moments=np.array([list(moment.values()) for moment in Moments])
HuMoments=np.array([[-math.log(abs(hu)) for hu in chain.from_iterable(moment)] for moment in HuMoments])
centres=np.array(centres)
metadata = np.vstack((c_area, c_len)).T
metadata = np.concatenate((centres, Moments, HuMoments), axis = 1)
return metadata
def normalize(self, data):
return ( data - self.mean) / self.std
def classify(self):
# while True:
if self.maskrcnn_manipulator_object.new_mask_available:
self.maskrcnn_manipulator_object.new_mask_available = False
resized_mask = cv2.resize(self.maskrcnn_manipulator_object.masks, (100, 100))
ret,thresh1 = cv2.threshold(resized_mask,self.maskrcnn_manipulator_object.mask_accuracy,1,cv2.THRESH_BINARY)
chunkFeatures = self.normalize(self.getFeatures([thresh1.astype(np.uint8)]))
chunkFeatures = self.pca.transform(chunkFeatures)
results = self.model_SVC.predict(chunkFeatures)
print(results)
# unique, counts =np.unique(results, return_counts=True)
# for value, numberOfOccurences in zip(unique, counts):
# if numberOfOccurences >= maxNumberOfFrames:
# if(value != mycv):
# print(value)
# mycv = value
# if(gameStarted):
# applyMove(value)
if __name__ == '__main__':
imageNumber = {}
for i in range(1,11):
imageNumber[i] = 100
try:
os.mkdir("data")
for i in range(1,11):
os.mkdir("data/" + str(i))
except FileExistsError as identifier:
print("directories already exist")
video_stream_widget = VideoStreamWidget()
maskrcnn_calculation = MaskRCNN_maniuplator(video_stream_widget)
# main_classificator = Main_Classificator(maskrcnn_calculation)
def on_trackbar(val):
maskrcnn_calculation.mask_accuracy = val / 100
window_name = "test"
cv2.namedWindow(window_name)
cv2.createTrackbar("pick accuracy", window_name , int(maskrcnn_calculation.mask_accuracy * 100), 100, on_trackbar)
while True:
try:
frame = np.array(video_stream_widget.frame)
if(maskrcnn_calculation.masks is not None):
cv2.imshow("mask", maskrcnn_calculation.masks)
# main_classificator.classify()
frame[maskrcnn_calculation.out_mask > maskrcnn_calculation.mask_accuracy] = 255
cv2.imshow(window_name, frame)
c = cv2.waitKey(0)
print(c)
cv2.imshow(window_name, frame)
if c == ord('q'):
video_stream_widget.capture.release()
cv2.destroyAllWindows()
exit(1)
elif c >=49 and c <= 58:
mystring = "data/" + str(c - 48) + "/" + str(imageNumber[c - 48]) + ".png"
imageNumber[c - 48] +=1
cv2.imwrite(mystring, video_stream_widget.frame)
elif c == 48:
mystring = "data/" + str(c - 38) + "/" + str(imageNumber[c - 38]) + ".png"
imageNumber[c - 38] +=1
cv2.imwrite(mystring, video_stream_widget.frame)
else:
cv2.imshow(window_name, frame)
key = cv2.waitKey(1)
if key == ord('q'):
video_stream_widget.capture.release()
cv2.destroyAllWindows()
exit(1)
elif key == ord(' '):
maskrcnn_calculation.start()
# main_classificator.start()
except AttributeError:
pass