137 lines
3.6 KiB
JavaScript
137 lines
3.6 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import {client} from '../../helpers/idb.js';
|
|
import {PROJECT_ROOT, MAX_SESSION_TIME, TAGS} from '../../R.js';
|
|
import {argmaxThresh} from '../../helpers/utils.js';
|
|
import pickRandomFile from './pickRandomFile.js';
|
|
import UserSession from '../../models/UserSession.js';
|
|
|
|
|
|
/** @typedef {import('../../models/IDBSession.js')} IDBSession */
|
|
|
|
/**
|
|
* @typedef {Object} FileMat
|
|
* @property {Array<number>} mat Matrix of floats
|
|
* @property {number} nums Number of users that have seen this challenge
|
|
* @property {number} obj The object in the image
|
|
*/
|
|
|
|
/** @type {FileMat} */
|
|
const defaultMat = {
|
|
mat: Array(16).fill(0.5),
|
|
nums: 0,
|
|
obj: 0,
|
|
};
|
|
|
|
/**
|
|
* Get file matrix
|
|
* @param {string} image
|
|
* @return {FileMat}
|
|
*/
|
|
function getMat(image) {
|
|
const mats = JSON.parse(
|
|
fs.readFileSync(
|
|
path.join(PROJECT_ROOT, 'dev', 'mat.json'), {encoding: 'utf8'}));
|
|
|
|
return mats[image] || defaultMat;
|
|
}
|
|
|
|
/**
|
|
* Update the image mat
|
|
* @param {FileMat} imageMat
|
|
* @param {Array<number>} userMat
|
|
* @param {string} image
|
|
*/
|
|
function updateMat(imageMat, userMat, image) {
|
|
const updatedMat = Object.assign([], imageMat.mat);
|
|
for (let i = 0; i < imageMat.mat.length; i++) {
|
|
const delta = 0.1;
|
|
if (userMat[i] === 1) {
|
|
if (updatedMat[i] + delta > 1) {
|
|
updatedMat[i] = 1.0;
|
|
} else {
|
|
updatedMat[i] += delta;
|
|
}
|
|
} else {
|
|
if (updatedMat[i] < delta) {
|
|
updatedMat[i] = 0.0;
|
|
} else {
|
|
updatedMat[i] -= delta;
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('OLD MAT: ', imageMat.mat);
|
|
console.log('NEW MAT: ', updatedMat);
|
|
|
|
imageMat.nums += 1;
|
|
imageMat.mat = updatedMat;
|
|
|
|
const mats = JSON.parse(
|
|
fs.readFileSync(
|
|
path.join(PROJECT_ROOT, 'dev', 'mat.json'), {encoding: 'utf8'}));
|
|
mats[image] = imageMat;
|
|
|
|
fs.writeFileSync(
|
|
path.join(PROJECT_ROOT, 'dev', 'mat.json'),
|
|
JSON.stringify(mats, null, 2), {encoding: 'utf8'});
|
|
}
|
|
|
|
/**
|
|
* @param {string} sessionId
|
|
* @param {Array<number>} userMat
|
|
* @return {Promise}
|
|
*/
|
|
export default function(sessionId, userMat) {
|
|
return new Promise((resolve, reject)=>{
|
|
client.get(sessionId, (err, resp)=>{
|
|
if (err) return reject(err);
|
|
if (!resp) return reject(new Error('Session expired'));
|
|
|
|
/** @type {IDBSession} */
|
|
const result = JSON.parse(resp);
|
|
|
|
const imageMat = getMat(result.image);
|
|
const trueArgmax = argmaxThresh(imageMat.mat, 0.6).join(',');
|
|
const userArgmax = argmaxThresh(userMat, 1).join(',');
|
|
|
|
updateMat(imageMat, userMat, result.image);
|
|
|
|
console.log('trueArgmax', trueArgmax);
|
|
console.log('userArgmax', userArgmax);
|
|
|
|
const delta = imageMat.nums * 0.05;
|
|
|
|
if (userArgmax !== trueArgmax) {
|
|
// TODO: Decrease the score based on mat dispersion
|
|
// High variance = small reduction
|
|
// Low variance = high reduction
|
|
result.score -= delta;
|
|
} else {
|
|
result.score += delta;
|
|
}
|
|
|
|
pickRandomFile().then((image)=>{
|
|
// TODO: Make me dynamic
|
|
const newImageTag = 1;
|
|
const update = JSON.stringify(
|
|
Object.assign(result, {
|
|
image: image,
|
|
obj: newImageTag,
|
|
}));
|
|
|
|
client.setex(sessionId, MAX_SESSION_TIME,
|
|
update, (err)=>{
|
|
if (err) return reject(err);
|
|
const session = new UserSession();
|
|
session.setSessionId(sessionId);
|
|
session.setWebsiteKey(result.websiteKey);
|
|
session.setImageTag(TAGS[newImageTag]);
|
|
|
|
resolve(session);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|