-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifierCorrection.py
236 lines (183 loc) · 5.59 KB
/
classifierCorrection.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
import os
import cv2 as cv
import face_recognition
import numpy as np
import copy
import torch
import torch.nn as nn
import MLtracking as ml
import pyautogui
import matplotlib.pyplot as plt
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# To make examples:
# 1. take eye pic
# 2. run models on it
# 3. record their predictions for N cycles
# 4. write down the N guesses and the actual looking position
# # X classifiers
# sixn = ml.sixnine().to(device)
# sixn.load_state_dict(torch.load("xModels/69good.plt",map_location=device))
# sixn.eval()
#
# sevent = ml.venty().to(device)
# sevent.load_state_dict(torch.load("xModels/70test.plt",map_location=device))
# sevent.eval()
#
#
# # Y classifiers
# fiv = ml.eightdeep().to(device)
# fiv.load_state_dict(torch.load("yModels/54x1.plt",map_location=device))
# fiv.eval()
#
#
#
# def ensembleX(im): # 58 accuracy
# modList = [sixn,sevent]
# sumn = 0
# for mod in modList:
# sumn += mod(im).item()
# return sumn / len(modList)
def maxAndMin(featCoords,mult = 1):
adj = 10/mult
listX = []
listY = []
for tup in featCoords:
listX.append(tup[0])
listY.append(tup[1])
maxminList = np.array([min(listX)-adj,min(listY)-adj,max(listX)+adj,max(listY)+adj])
# print(maxminList)
return (maxminList*mult).astype(int), (np.array([sum(listX)/len(listX)-maxminList[0], sum(listY)/len(listY)-maxminList[1]])*mult).astype(int)
#Preps a color pic of the eye for input into the CNN
def process(im):
left_eye = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
left_eye = cv.resize(left_eye, dsize=(100, 50))
# Display the image - DEBUGGING ONLY
cv.imshow('frame', left_eye)
top = max([max(x) for x in left_eye])
left_eye = (torch.tensor([[left_eye]]).to(dtype=torch.float,
device=device)) / top
return left_eye
#
# # def makeExamples():
# webcam = cv.VideoCapture(0)
# frameShrink = 0.15
# mvAvgx = []
# mvAvgy = []
# scale = 10
# margin = 200
#
# pixelLocation = 10
# pixelRecord = []
# xRecord = []
# err = []
# counter = 0
# while pixelLocation < 1400:
# ret, frame = webcam.read()
# smallframe = cv.resize(copy.deepcopy(frame), (0, 0), fy=frameShrink,
# fx=frameShrink)
# smallframe = cv.cvtColor(smallframe, cv.COLOR_BGR2GRAY)
#
# feats = face_recognition.face_landmarks(smallframe)
#
#
#
# if len(feats) > 0:
#
# leBds, leCenter = maxAndMin(feats[0]['left_eye'],
# mult=1 / frameShrink)
# left_eye = frame[leBds[1]:leBds[3], leBds[0]:leBds[2]]
#
# left_eye = process(left_eye)
#
# x = ensembleX(left_eye) * 1440
# y = fiv(left_eye).item() * 900
# pyautogui.moveTo(pixelLocation,450)
#
#
#
# pixelRecord.append(pixelLocation)
# xRecord.append(x)
# err.append(abs(x-pixelLocation))
# counter += 1
#
# if counter % 5 == 0:
# pixelLocation += 50
#
#
#
# if cv.waitKey(1) & 0xFF == ord('q'):
# break
def eyetrack(frameShrink = 0.15):
webcam = cv.VideoCapture(0)
o = open("corrections.txt","r+")
for i in range(40,1440,1440//10):
for j in range(0,900,900//10):
o.write(str(i/1440) + "," + str(j/900))
print(i,j)
pyautogui.moveTo(i,j)
for k in range(10):
ret, frame = webcam.read()
smallframe = cv.resize(copy.deepcopy(frame), (0, 0), fy=frameShrink, fx=frameShrink)
smallframe = cv.cvtColor(smallframe, cv.COLOR_BGR2GRAY)
feats = face_recognition.face_landmarks(smallframe)
if len(feats) > 0:
leBds, leCenter = maxAndMin(feats[0]['left_eye'], mult=1/frameShrink)
left_eye = frame[leBds[1]:leBds[3], leBds[0]:leBds[2]]
left_eye = process(left_eye)
x = ensembleX(left_eye)
y = fiv(left_eye).item()
o.write("," + str(x) + "," + str(y))
if cv.waitKey(1) & 0xFF == ord('q'):
o.close()
break
o.write("\n")
o.close()
# eyetrack()
def benchmark(set):
offx = 0
offy = 0
for thing in set:
offset = abs(thing[0] - fick(thing[2:4]))
offx += offset[0]
# offy += offset[1]
return offx/len(set)*1440#,offy/len(set)*900
class lin(torch.nn.Module):
def __init__(self):
super(lin, self).__init__()
self.linear1 = torch.nn.Linear(2, 1)
# self.linear2 = torch.nn.Linear(10, 2)
def forward(self, x):
x = self.linear1(x)
# x = self.linear2(x)
return x
filename = 'corrections.txt'
fick = lin()
criterion = torch.nn.MSELoss(size_average = False)
# optimizer = torch.optim.SGD(fick.parameters(), lr = 0.01)
optimizer = torch.optim.Adam(fick.parameters(), lr=0.001)
numEpochs = 50
# def loadData(filename):
labels = []
datum = []
f = open(filename, "r")
test = [x.strip().split(",") for x in f.readlines()]
test = [[float(x) for x in y] for y in test]
train = []
for thing in test:
while len(thing) > 4:
train.append([thing[0],thing[1],thing.pop(2),thing.pop(2)])
test = torch.tensor(test)
train = torch.tensor(train)
for i in range(numEpochs):
print(i)
for exm in train:
pred = fick.forward(exm[2:4])
loss = criterion(pred, exm[0])
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i%10 == 0:
fick.eval()
print(benchmark(train))
print(benchmark(test))
fick.train()