xxxxxxxxxx
let readyToRock = false
let fileInputButton
let inputImage
let result=-1 //[confidence that this is banana]
//centre box for the content
let boxSize
let xCenter
let yCenter
let x0
let y0
let x1
let y1
let modelJson
let modelWeights
function inputFileHandler(file) {
if (file.type === 'image') {
inputImage = createImg(file.data, '');
inputImage.hide();
//console.log(inputImage)
mobileNet.classify(inputImage, handleResult);
} else {
console.log('[Insert a rude comment about the input not being an image]');
}
}
let mobileNet;
let classifier;
function modelReady(err, model) {
if (err) {
console.error(err);
} else {
//console.log('MobileNet ready');
classifier.load('./model.json',customModelReady)
}
}
function customModelReady(err,model){
if (err) {
console.error(err);
} else {
//console.log('Custom model ready');
mobileNet.classify(inputImage, handleResult);
}
}
function logResult(err, res) {
if (err) {
console.log('oopsie');
console.error(err);
} else {
console.log(res);
}
}
function handleResult(err, res) {
if (err) {
console.log('oopsie');
console.error(err);
} else {
result = 0;
for (i = 0; i < res.length; i++) {
if (res[i].label === "banana") {
result = res[i].confidence
i = res.length
}
}
}
}
let testBanana
function preload() {
testBanana = createImg('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Banana-Single.jpg/1162px-Banana-Single.jpg','','anonymous')
}
function setup() {
testBanana.hide()
mobileNet = ml5.featureExtractor('MobileNet',modelReady)
classifier=mobileNet.classification()
inputImage = testBanana
//fileInputButton = createFileInput(inputFileHandler);
//calculate element positions
boxSize = min(windowWidth, windowHeight)
xCenter = windowWidth / 2
yCenter = windowHeight / 2
x0 = xCenter - boxSize / 2
y0 = yCenter - boxSize / 2
x1=x0+boxSize
y1=y0+boxSize
let c=createCanvas(windowWidth, windowHeight);
c.drop(inputFileHandler)
background('#FFF200');
textAlign(CENTER, CENTER);
textSize(32);
}
function draw() {
clear()
back()
title()
bananaScore()
bananaImage()
sendBanana()
}
let bananaMessages = ["Wow! Ultimate Banana!", "Yes Banana!", "Eh, Banana?", "No Banana..."];
function bananaScore() {
fill(0);
let message
let modified
if(result<0){message='Loading...'}else{
modified=(result-0.5)*2
if (modified > 0.9) {
message = bananaMessages[0]
} else if (modified > 0.5) {
message = bananaMessages[1]
} else if (modified > 0) {
message = bananaMessages[2]
} else {
message = bananaMessages[3]
}
message+='\nBananacity: '+floor(modified*10000)}
text(message, xCenter, y0+boxSize/5);
}
function bananaImage() {
let dSize = boxSize/2;
let dx0= xCenter-dSize/2
let dy0= yCenter-dSize/3
stroke(0)
strokeWeight(10)
rect(dx0,dy0,dSize,dSize)
strokeWeight(0)
if (inputImage) {
let sSize = min(inputImage.width, inputImage.height)
let sX = (inputImage.width - sSize) / 2
let sY = (inputImage.height - sSize) / 2
image(inputImage, dx0, dy0, dSize, dSize, sX, sY, sSize, sSize);
}
}
function sendBanana(){
fill(0)
text('Send Banana!\n(Drag and Drop)',xCenter,y1-boxSize/12)
//let xButton=xCenter-fileInputButton.width/2
//let yButton=y1-boxSize/16
//fileInputButton.position(xButton, yButton);
}
function title(){
textSize(60);
fill(0);
textStyle(BOLD);
let message='Banananator'
text(message, xCenter-100, y0+boxSize/12);
translate(xCenter+180,y0+boxSize/12)
textSize(80);
rotate(-0.3)
fill('orange');
text(3000, 0, 0);
rotate(0.3)
translate(-(xCenter+180),-(y0+boxSize/12))
textStyle(NORMAL);
textSize(32);
}
function back() {
background('#FFF023');
fill('#FFFFFFFF')
rect(x0, y0, boxSize, boxSize)
}