-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_challenge.py
More file actions
143 lines (125 loc) · 4.22 KB
/
mouse_challenge.py
File metadata and controls
143 lines (125 loc) · 4.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
#importamos todas las librerias correspondientes
import lock
import os
import numpy as np
from tensorflow import keras
from laberintoCondiff import laberinto
def arrayentero(array,dim, num=None):
Y_array=[]
X_array=[]
#trasformamos el array en float
array=np.array(array,dtype='float16')
#guardamos la dimension de la ventana en una variable
ventana=dim
if num is not None:
#if array[0]==0:
# array=np.delete(array,0)
#return [[np.median(array),np.mean(array),np.std(array)]], [num]
#recorremos todos los elementos del array hasta N-longitud de la ventana y lo guardamos en un nuevo array y generamos el array de etiquetas con el num
for i in range(0,len(array)-ventana):
X_array.append(array[i:ventana+i])
Y_array.append([num])
if len(X_array)==0:
try:
aux=np.zeros(shape=[ventana,2])
aux[0:len(array)]=array
X_array.append(aux)
except:
aux=np.zeros(shape=[ventana])
aux[0:len(array)]=array
X_array.append(aux)
Y_array.append([num])
Y_array=np.array(Y_array)
return X_array,Y_array
else:
#recorremos todos los elementos del array hasta N-longitud de la ventana y lo guardamos en un nuevo array
for i in range(0,len(array)-ventana):
X_array.append(array[i:ventana+i,:])
if len(X_array)==0:
try:
aux=np.zeros(shape=[ventana,3])
aux[0:len(array)]=array
X_array.append(aux)
except:
aux=np.zeros(shape=[ventana])
aux[0:len(array)]=array
X_array.append(aux)
return X_array
props_dict = {}
DEBUG_MODE = True
# funcion init devuelve un 0 que es valido ya que suponemos que el usuario va a tener siempre 8un teclado con el que pueda escribir
def init(props):
global props_dict
print("Python: starting challenge init()")
#cargamos el json que le pasemos y lo guardamos en la variable global
props_dict = props
return 0
def executeChallenge():
print("Python: starting executeChallenge()")
#comprobamos las variables de entorno y cogemos el de SECUREMIRROR_CAPTURES
dataPath = os.environ['SECUREMIRROR_CAPTURES']
print ("storage folder is :",dataPath)
#abrimos lock
lock.lockIN("keystroke")
metodo=2
#ejecutamos el codigo de captura de datos
if metodo==1:
datos=laberinto()
aux=np.zeros(shape=[4000,2])
aux[0:len(datos)]=datos
aux=aux/640
datos=np.array([aux])
else:
ventana=900
datos=laberinto()
datos=arrayentero(datos,ventana)
"""
aux=np.zeros(shape=[4000,2])
aux[0:len(datos)]=datos"""
datos=np.array(datos)
datos=datos/640
#seleccionamos la dimension de la ventana
#tratamos los datos
#cargamos el modelo
#############################
#cambiar la ruta si se pasa por el json
url=props_dict["url"]
new_model = keras.models.load_model(url+'path_to_my_model.h5')
#cerramos el lock
lock.lockOUT("mouse_Dinamics")
#predecimos la categoria de los nuevo datos
new_predictions = new_model.predict(datos)
print(new_predictions)
print(np.argmax(new_predictions, axis=1))
cad=np.argmax(new_predictions, axis=1)
categorias=np.load("categorias.npy")
#modo empresarial
if props_dict['metodo']=='empresarial':
cat=np.where(cad==categorias[0])
if len(cat)!=0:
cad=0
elif props_dict['metodo']=='parental':
cat=np.where(cad==categorias[0])
if len(cat)!=0:
cad=0
else:
cad=1
"""
if props_dict['metodo']==2:
if cad>7:
cad=1
else:
cad=0
"""
#y generamos el resultado
cad="%d"%(cad)
key = bytes(cad,'utf-8')
key_size = len(key)
result = (key, key_size)
print("result:", result)
return result
# esta parte del codigo no se ejecuta a no ser que sea llamada desde linea de comandos
if __name__ == "__main__":
midict = {"url":"./","metodo":'parental'}
init(midict)
executeChallenge()