xxxxxxxxxx
540
// Simplified/DOMless Image Recognition (Classifier) System
// Based on https://editor.p5js.org/ml5/sketches/FeatureExtractor_Image_Classification
// Image recognition using transfer learning. Uses ml5.js, p5.js, MobileNet.
// thank you to Golan and Sophia for helping me work out issues in the program :)
// Tarot Card readings from : https://raw.githubusercontent.com/dariusk/corpora/master/data/divination/tarot_interpretations.json
let featureExtractor;
let classifier;
let video;
// TRAINING PICTURES
let n0 = 0;
let n1 = 0;
let n2 = 0;
let n3 = 0;
let n4 = 0;
let n5 = 0;
let n6 = 0;
let n7 = 0;
let n8 = 0;
let n9 = 0;
//ORDER OF KEYBOARD
let nQ = 0;
let nW = 0;
let nE = 0;
let nR = 0;
let nT = 0;
let nY = 0;
let nU = 0;
let nI = 0;
let nO = 0;
let nP = 0;
let nA = 0;
let nS = 0;
let nD = 0;
let nF = 0;
let nG = 0;
let nH = 0;
let nJ = 0;
let nK = 0;
let nL = 0;
let nZ = 0;
let nX = 0;
let nC = 0;
let nV = 0;
let nSamplesB = 0;
let nN = 0;
let nM = 0;
let bMobileNetIsLoaded = false;
let trainingLoss = NaN;
let currentLabel = "";
let currentConfidence = "";
let fortuneJSON;
let cardPickedText = "PSYCHIC";
let fortuneText = "EYE";
let cautionText = "FORTUNES";
let isDejaPicked = false;
let oldCard = "";
let currCard;
let fortune = "";
let cardChosen = 0;
let fortuneCt;
let shadowArray;
let luckyNum;
let shadowNum;
//let fortuneArg = "";
let psychicWords;
let psychicFluff;
let psychicCaution;
const TRAINING_NOT_STARTED = -1;
const TRAINING_STARTED = 0;
const TRAINING_FINISHED = 1;
let systemStatus = TRAINING_NOT_STARTED;
// what does the psychic say
thinkingJargon = ["Aaaah ", "Ooh that's interesting, ", "I seeee ", "hmmmmm ", "Well, well, well, ", "The spirits tell of "]
fluffingJargon = ["The ancestors say ", "", "", "", "I see you that must ", "The cards say "]
cautionJargon = ["but BEWARE ", "the Cards Caution Against "]
//-----------------------------------------
// asynchronous
function preload() {
let url = 'https://raw.githubusercontent.com/dariusk/corpora/master/data/divination/tarot_interpretations.json';
fortuneJSON = loadJSON(url);
}
//-----------------------------------------
function setup() {
createCanvas(1200, 1200);
print("D:");
print(fortuneJSON);
video = createCapture(VIDEO);
video.size(320, 240);
video.hide(); // hide the video's DOM object.
// Extract the already learned features from MobileNet
featureExtractor = ml5.featureExtractor('MobileNet',
theModelReadyCallbackFunction);
// Create a new classifier using those features,
// and provide it with the video object
const options = {
numLabels: 6
};
classifier = featureExtractor.classification(video, options);
}
//-----------------------------------------
function draw() {
background(0);
tint(255, 255, 255);
image(video, 0, 350, 320, 240);
textAlign(CENTER);
textFont('Georgia', 30)
fill(255);
text(cardPickedText, width/2, height/4);
text(fortuneText, width/2, height/4+60);
text(cautionText, width/2, height/4+120);
drawStatusIndicators();
}
//-----------------------------------------
function drawStatusIndicators() {
let n = 230;
fill('white');
textFont('Georgia', 14)
text("MobileNet done loading: " + bMobileNetIsLoaded, 15, n+20);
text("#A:" + nA, 15, n+35);
text("#B:" + nSamplesB, 15, n+ 50);
text("#C:" + nC, 15, n+65);
text("#D:" + nD, 15, n+80);
text("#E:" + nE, 15, n+95);
text("#F:" + nF, 15, n+110);
text("#G:" + nG, 15, n+125);
text("#H:" + nH, 15, n+140);
text("#I:" + nI, 15, n+155);
text("#J:" + nJ, 15, n+170);
text("#K:" + nK, 15, n+185);
text("#L:" + nL, 15, n+200);
text("#M:" + nM, 15, n+215);
text("#N:" + nN, 15, n+230);
text("#O:" + nO, 15,n+ 245);
text("#P:" + nP, 15,n+ 260);
text("#Q:" + nQ, 15,n+ 275);
text("#R:" + nR, 15,n+ 290);
text("#S:" + nS, 15,n+ 305);
text("#T:" + nT, 15,n+ 320);
text("#U:" + nU, 15, n+335);
if (systemStatus === TRAINING_NOT_STARTED) {
text("TRAINING_NOT_STARTED", 15, n+350);
} else if (systemStatus === TRAINING_STARTED) {
text("TRAINING_STARTED", 15, n+365);
} else if (systemStatus === TRAINING_FINISHED) {
text("TRAINING_FINISHED", 15, n+380);
}
if (systemStatus >= 0) {
text("trainingLoss = " + trainingLoss, 15, n+95);
} else {
if ((nA > 0) && (nSamplesB > 0) && (nC > 0) && (nD > 0) && (nE > 0) && (nF > 0) && (nG > 0)) {// && (nH > 0) && (nI > 0) && (nJ > 0) && (nK > 0) && (nL > 0) && (nM > 0) && (nN > 0) && (nO > 0) && (nP > 0) && (nQ > 0) && (nR > 0) && (nS > 0) && (nT > 0) && (nU > 0)) {
text("Press t to start training", 15, 95);
} else {
text("Add samples by pressing keys a,b,c", 15, n+400);
}
}
if (systemStatus === TRAINING_FINISHED) {
text("Label: " + currentLabel, 15, n+110);
text("Confidence: " + currentConfidence, 15,n+ 125); //nf(currentConfidence,1,6)
}
}
//----------------------------------------- train the program
function keyPressed() {
switch (key) {
// save image reference:
// let imgPath = "/dataset/" + label + "/"
// let filename = imgPath + str(i) + ".jpg"
// classifier.addImage(filename, label);
// let imgPath = "/card01/" + label + "/"
// let filename = imgPath + str(i) + ".jpg"
// classifier.addImage(filename, label);
case 'A': // correlate to each card set letter and number // do by list order
//thank you sophia and izzy for saving instructions
classifier.addImage('A'); // correlate for different cards
nA++;
break;
case 'B': // correlate to each card set letter and number
classifier.addImage('B');
nSamplesB++;
break;
case 'C':
classifier.addImage('C');
nC++;
break;
//
case 'D':
classifier.addImage('D');
nD++;
break;
case 'E':
classifier.addImage('E');
nE++;
break;
case 'F':
classifier.addImage('F');
nF++;
break;
case 'G':
classifier.addImage('G');
nG++;
break;
// case 'H':
// classifier.addImage('H');
// nH++;
// break;
// case 'I':
// classifier.addImage('I');
// nI++;
// break;
// case 'J':
// classifier.addImage('J');
// nJ++;
// break;
// case 'K':
// classifier.addImage('K');
// nK++;
// break;
// case 'L':
// classifier.addImage('L');
// nL++;
// break;
// case 'M':
// classifier.addImage('M');
// nM++;
// break;
// case 'N':
// classifier.addImage('N');
// nN++;
// break;
// case 'O':
// classifier.addImage('O');
// nO++;
// break;
// case 'P':
// classifier.addImage('P');
// nP++;
// break;
// case 'Q':
// classifier.addImage('Q');
// nQ++;
// break;
// case 'R':
// classifier.addImage('R');
// nR++;
// break;
// case 'S':
// classifier.addImage('S');
// nS++;
// break;
// case 'T':
// classifier.addImage('T');
// nT++;
// break;
// case 'U':
// classifier.addImage('U');
// nU++;
// break;
case 't': // trains the program set to "enter"
classifier.train(function(lossValue) {
if (lossValue) {
trainingLoss = lossValue;
systemStatus = TRAINING_STARTED;
} else {
systemStatus = TRAINING_FINISHED;
classifyCurrentVideoFrame();
}
});
break;
}
}
//-----------------------------------------
// Classify the current frame of video.
function classifyCurrentVideoFrame() {
classifier.classify(theGotResultsCallbackFunction);
}
//-----------------------------------------
// What to do when the model is finished loading.
function theModelReadyCallbackFunction() {
bMobileNetIsLoaded = true;
}
//-----------------------------------------
// What we should do when the classifier sends back results
function theGotResultsCallbackFunction(err, results) {
if (err) {
console.error(err); // Display any error
}
if (results && results[0]) {
currentLabel = results[0].label;
currentConfidence = results[0].confidence.toFixed(2);
// Now that we've completed processing yesterframe's results,
// kick off the processing of the current frame.
classifyCurrentVideoFrame();
//predictFuture();
readCard();
// getFortune();
}
}
//================================================================
function readCard() {
currCard = currentLabel;
if (currCard != oldCard) {
isDejaPicked = false;
}
//getFortune();
if (currentLabel == "A") {
cardPickedText = 'PLEASE'
fortuneText = "CHOOSE A"
cautionText = "CARD"
console.log("please take a card");
}
if (currentLabel == "B") {
cardChosen = 0;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "C") {
cardChosen = 1;
if (isDejaPicked == false) {
getFortune();
}
}
//
if (currentLabel == "D") {
cardChosen = 2;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "E") {
cardChosen = 3;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "F") {
cardChosen = 4;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "G") {
cardChosen = 5;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "H") {
cardChosen = 6;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "I") {
cardChosen = 7;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "J") {
cardChosen = 8;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "K") {
cardChosen = 9;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "L") {
cardChosen = 10;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "M") {
cardChosen = 11;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "N") {
cardChosen = 12;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "O") {
cardChosen = 13;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "P") {
cardChosen = 14;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "Q") {
cardChosen = 15;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "R") {
cardChosen = 16;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "S") {
cardChosen = 17;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "T") {
cardChosen = 18;
if (isDejaPicked == false) {
getFortune();
}
}
if (currentLabel == "U") {
cardChosen = 19;
if (isDejaPicked == false) {
getFortune();
}
}
//
oldCard = currCard;
}
//=================================================================
function getFortune() {
// console.log("A: " + fortuneJSON); //da fuk
// console.log(fortuneJSON);
card = cardChosen; // working
// console.log("B: " + card);
psychicWords = random(thinkingJargon);
psychicFluff = random(fluffingJargon);
psychicCaution = random(cautionJargon);
// print("check ");
// print(fortuneJSON);
fortuneCt = fortuneJSON.tarot_interpretations[card].fortune_telling;
shadowArray = fortuneJSON.tarot_interpretations[card].meanings.shadow;
luckyNum = int(random(fortuneCt.length));
shadowNum = int(random(shadowArray.length));
cardPickedText = psychicWords + fortuneJSON.tarot_interpretations[card].name + "..."
fortuneText = psychicFluff + fortuneJSON.tarot_interpretations[card].fortune_telling[luckyNum]
cautionText = psychicCaution + fortuneJSON.tarot_interpretations[card].meanings.shadow[shadowNum]
console.log(psychicWords + fortuneJSON.tarot_interpretations[card].name + "...");
console.log(psychicFluff + fortuneJSON.tarot_interpretations[card].fortune_telling[luckyNum]);
console.log(psychicCaution + fortuneJSON.tarot_interpretations[card].meanings.shadow[shadowNum]);
fortune = fortuneJSON;
isDejaPicked = true;
//}
}