Feat: UserflowDataGatherer send correct datas; Feat: UserflowPredictorWebserver receive data and create userflowmap
This commit is contained in:
parent
6feb5af4d0
commit
9946f8c057
26 changed files with 3977 additions and 22 deletions
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
121
Predictor/PredictorNeuralNetwork/Predictor.py
Normal file
121
Predictor/PredictorNeuralNetwork/Predictor.py
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
from keras.models import Sequential
|
||||
from keras.layers import Activation, Dropout, Flatten, Dense
|
||||
from keras import backend as K
|
||||
from keras.preprocessing.sequence import pad_sequences
|
||||
from keras.preprocessing.image import ImageDataGenerator
|
||||
from keras.layers import Conv2D, MaxPooling2D
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.image as mpimg
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
img_width, img_height = 150, 150
|
||||
train_data_dir = './UserflowPredictorSystem/predictor/datas/train'
|
||||
validation_data_dir = './UserflowPredictorSystem/predictor/datas/test'
|
||||
nb_train_samples = 2000
|
||||
nb_validation_samples = 800
|
||||
epochs = 100
|
||||
batch_size = 16
|
||||
|
||||
|
||||
if K.image_data_format() == 'channels_first':
|
||||
input_shape = (3, img_width, img_height)
|
||||
else:
|
||||
input_shape = (img_width, img_height, 3)
|
||||
|
||||
def create_model():
|
||||
model = Sequential()
|
||||
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
|
||||
model.add(Conv2D(32, (3, 3)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
|
||||
model.add(Conv2D(64, (3, 3)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(MaxPooling2D(pool_size=(2, 2)))
|
||||
|
||||
model.add(Flatten())
|
||||
model.add(Dense(64))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Dense(1))
|
||||
model.add(Activation('sigmoid'))
|
||||
|
||||
|
||||
model.compile(loss='binary_crossentropy',
|
||||
optimizer='rmsprop',
|
||||
metrics=['accuracy'])
|
||||
return model
|
||||
|
||||
def train_model(model):
|
||||
# this is the augmentation configuration we will use for training
|
||||
train_datagen = ImageDataGenerator(
|
||||
rescale=1. / 255,
|
||||
shear_range=0.2,
|
||||
zoom_range=0.2,
|
||||
horizontal_flip=True)
|
||||
|
||||
# this is the augmentation configuration we will use for testing:
|
||||
# only rescaling
|
||||
test_datagen = ImageDataGenerator(rescale=1. / 255)
|
||||
|
||||
train_generator = train_datagen.flow_from_directory(
|
||||
train_data_dir,
|
||||
target_size=(img_width, img_height),
|
||||
batch_size=batch_size,
|
||||
class_mode='binary')
|
||||
|
||||
validation_generator = test_datagen.flow_from_directory(
|
||||
validation_data_dir,
|
||||
target_size=(img_width, img_height),
|
||||
batch_size=batch_size,
|
||||
class_mode='binary')
|
||||
|
||||
model.fit_generator(
|
||||
train_generator,
|
||||
steps_per_epoch=nb_train_samples // batch_size,
|
||||
epochs=epochs,
|
||||
validation_data=validation_generator,
|
||||
validation_steps=nb_validation_samples // batch_size)
|
||||
|
||||
model.save_weights('./UserflowPredictorSystem/first_try2.h5')
|
||||
return model
|
||||
|
||||
|
||||
|
||||
def load_trained_model(weights_path):
|
||||
model = create_model()
|
||||
model.load_weights(weights_path)
|
||||
return model
|
||||
|
||||
|
||||
def predict(number, model):
|
||||
img = cv2.imread("./UserflowPredictorSystem/predictor/datas/test/" + str(number) + ".jpg")
|
||||
im = mpimg.imread("./UserflowPredictorSystem/predictor/datas/test/" + str(number) + ".jpg")
|
||||
plt.imshow(im)
|
||||
img = cv2.resize(img, (img_width,img_height))
|
||||
img = img.reshape(1, img_width, img_height, 3)
|
||||
res = model.predict(img)
|
||||
if res == 1:
|
||||
print('DOG')
|
||||
else:
|
||||
print('CAT')
|
||||
|
||||
model = create_model()
|
||||
model = train_model(model)
|
||||
|
||||
import os
|
||||
os.getcwd()
|
||||
|
||||
trained_model = load_trained_model("./UserflowPredictorSystem/first_try2.h5")
|
||||
trained_model.summary()
|
||||
import random
|
||||
predict(random.randint(1,12500), trained_model)
|
||||
predict('lolly', model)
|
||||
|
||||
|
||||
|
||||
print(np.argmax(loaded_model.predict(img)))
|
||||
1
Predictor/PredictorNeuralNetwork/Readme.md
Normal file
1
Predictor/PredictorNeuralNetwork/Readme.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
.
|
||||
BIN
Predictor/PredictorNeuralNetwork/weights/first_try.h5
Normal file
BIN
Predictor/PredictorNeuralNetwork/weights/first_try.h5
Normal file
Binary file not shown.
BIN
Predictor/PredictorNeuralNetwork/weights/first_try2.h5
Normal file
BIN
Predictor/PredictorNeuralNetwork/weights/first_try2.h5
Normal file
Binary file not shown.
12
Predictor/PredictorWebService/.eslintrc.yaml
Normal file
12
Predictor/PredictorWebService/.eslintrc.yaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# .eslintrc.yaml
|
||||
---
|
||||
extends: airbnb-base
|
||||
env:
|
||||
node: true
|
||||
mocha: true
|
||||
es6: true
|
||||
parser: typescript-eslint-parser
|
||||
parserOptions:
|
||||
sourceType: module
|
||||
ecmaFeatures:
|
||||
modules: true
|
||||
3
Predictor/PredictorWebService/.vscode/settings.json
vendored
Normal file
3
Predictor/PredictorWebService/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"git.ignoreLimitWarning": true
|
||||
}
|
||||
3129
Predictor/PredictorWebService/package-lock.json
generated
Normal file
3129
Predictor/PredictorWebService/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
30
Predictor/PredictorWebService/package.json
Normal file
30
Predictor/PredictorWebService/package.json
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"name": "predictorservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "main.js",
|
||||
"dependencies": {
|
||||
"@types/express": "^4.16.1",
|
||||
"body-parser": "^1.18.3",
|
||||
"canvas": "^2.4.1",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.16.4",
|
||||
"nodemon": "^1.18.10",
|
||||
"ws": "^6.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^11.13.0",
|
||||
"@types/ws": "^6.0.1",
|
||||
"ts-node": "^8.0.3",
|
||||
"typescript": "^3.4.2",
|
||||
"typescript-eslint-parser": "^22.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -w",
|
||||
"dev": "ts-node ./src/main.ts",
|
||||
"start": "nodemon ./dist/main.js",
|
||||
"prod": "npm run build && npm run start"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
}
|
||||
9
Predictor/PredictorWebService/src/main.ts
Normal file
9
Predictor/PredictorWebService/src/main.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { PredictorWebService } from "./predictor-web-service/PredictorWebService";
|
||||
|
||||
function main() {
|
||||
const predictorWebService = new PredictorWebService('/', 4000, 4100);
|
||||
predictorWebService.startExpress();
|
||||
predictorWebService.startWebSocket();
|
||||
}
|
||||
|
||||
main();
|
||||
|
|
@ -0,0 +1,126 @@
|
|||
import express from 'express';
|
||||
import * as WebSocket from 'ws';
|
||||
import * as http from 'http';
|
||||
import cors from 'cors';
|
||||
import * as bodyParser from "body-parser";
|
||||
import fs from 'fs';
|
||||
import {Data} from "../../../../DataGatherer/src/data/Data";
|
||||
import {createCanvas, Image} from "canvas";
|
||||
|
||||
export class PredictorWebService {
|
||||
|
||||
private portWebSocket: number;
|
||||
private portApi: number;
|
||||
private url: string;
|
||||
private app: express.Application;
|
||||
private httpServer: http.Server;
|
||||
private wss: WebSocket.Server;
|
||||
|
||||
constructor(url: string, portApi: number, portWebSocket: number) {
|
||||
this.url = url;
|
||||
this.portApi = portApi;
|
||||
this.portWebSocket = portWebSocket;
|
||||
}
|
||||
|
||||
public startWebSocket() {
|
||||
this.httpServer = http.createServer(this.app);
|
||||
this.wss = new WebSocket.Server({port: this.portWebSocket});
|
||||
|
||||
|
||||
this.wss.on('connection', (ws: WebSocket) => {
|
||||
ws.on('message', (message: string) => {
|
||||
console.log('received: %s', message);
|
||||
ws.send(`Hello, you sent -> ${message}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public startExpress() {
|
||||
const that = this;
|
||||
this.app = express();
|
||||
this.app.use(cors());
|
||||
this.app.use(bodyParser.json());
|
||||
this.app.use(bodyParser.urlencoded({ extended: false }));
|
||||
|
||||
this.app.post('/predict', function (req, res) {
|
||||
function generateFakeResponse() {
|
||||
return Array.from({length: 20}, () => Math.random().toPrecision(2));
|
||||
}
|
||||
console.log('Request prediction');
|
||||
res.send(generateFakeResponse());
|
||||
});
|
||||
|
||||
this.app.post('/trainData', function (req, res) {
|
||||
console.log('Received data');
|
||||
const data: Array<Data> = req.body.map(d => new Data(d.name, d.data, d.size, d.timestamp));
|
||||
console.dir(data);
|
||||
const image = data.find(val => {
|
||||
return val.getName() === 'screen';
|
||||
});
|
||||
const mouseClicks = data.filter(val => {
|
||||
return val.getName() === 'click';
|
||||
});
|
||||
const mouseMovements = data.filter(val => {
|
||||
return val.getName() === 'mousemove';
|
||||
});
|
||||
const keyboard = data.filter(val => {
|
||||
return val.getName() === 'keydown';
|
||||
});
|
||||
if(image){
|
||||
const canvas = createCanvas(image.getSize().width, image.getSize().height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
that.printImage(img, ctx);
|
||||
that.printMouseClick(mouseClicks, ctx);
|
||||
that.printMouseMove(mouseMovements, ctx);
|
||||
that.printKeyboard(keyboard, ctx);
|
||||
const base64data = canvas.toBuffer();
|
||||
fs.writeFile('./trainingDatas/' + image.getTimestamp() + '.png', base64data, 'base64', function(err){
|
||||
if (err) throw err;
|
||||
console.log('File saved.');
|
||||
res.send('ok - image received');
|
||||
})
|
||||
};
|
||||
img.onerror = err => { throw err };
|
||||
img.src = image.getData();
|
||||
|
||||
|
||||
} else {
|
||||
res.send('ok - only datas');
|
||||
}
|
||||
});
|
||||
|
||||
this.app.listen(this.portApi, () => {
|
||||
console.log('PredictorWebService is up and running on port: %d', this.portApi);
|
||||
});
|
||||
}
|
||||
|
||||
printMouseClick(mouseclicks: Array<Data>, ctx: any) {
|
||||
ctx.strokeStyle = 'rgba(219, 10, 91, 0.5)';
|
||||
ctx.lineWidth = 5;
|
||||
ctx.beginPath();
|
||||
for(const move of mouseclicks){
|
||||
ctx.strokeRect(move.getData().x, move.getData().y,10, 10);
|
||||
}
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
printMouseMove(mousemoves: Array<Data>, ctx: any) {
|
||||
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
|
||||
ctx.lineWidth = 5;
|
||||
ctx.beginPath();
|
||||
for(const move of mousemoves){
|
||||
ctx.lineTo(move.getData().x, move.getData().y);
|
||||
}
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
printKeyboard(keyboard: Array<Data>, ctx: any) {
|
||||
|
||||
}
|
||||
|
||||
printImage(image: Image, ctx: any){
|
||||
ctx.drawImage(image, 0, 0);
|
||||
}
|
||||
}
|
||||
20
Predictor/PredictorWebService/tsconfig.json
Normal file
20
Predictor/PredictorWebService/tsconfig.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"sourceMap": true,
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"resolveJsonModule": true,
|
||||
"outDir": "dist",
|
||||
"experimentalDecorators": true,
|
||||
"target": "es5",
|
||||
"jsx": "react",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es6"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"compileOnSave": true
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue