Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jlipps committed Sep 26, 2018
0 parents commit be5d9e5
Show file tree
Hide file tree
Showing 35 changed files with 10,904 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "appium"
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## OSX Deps

```brew install cairo libjpeg```
13 changes: 13 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";


let gulp = require('gulp'),
boilerplate = require('appium-gulp-plugins').boilerplate.use(gulp);

boilerplate({
build: 'test-ai-classifier',
coverage: {
files: ['./test/unit/**/*-specs.js', '!./test/functional/**'],
verbose: true
},
});
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import classifier from './lib/classifier';
export default classifier;
67 changes: 67 additions & 0 deletions lib/classifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import path from 'path';
import Canvas, { Image } from 'canvas';
import labels from './labels';
import * as tf from '@tensorflow/tfjs';

import '@tensorflow/tfjs-node';

const TF_MODEL = path.resolve(__dirname, "..", "..", "model", "model.pb");
const TF_WEIGHTS = path.resolve(__dirname, "..", "..", "model", "weights.json");

const IMG_CHANNELS = 3;

let _cached_model = null;

export async function getModel () {
if (!_cached_model) {
_cached_model = await tf.loadFrozenModel(`file://${TF_MODEL}`, `file://${TF_WEIGHTS}`);
}
return _cached_model;
}

async function canvasFromImage (imgPath) {
let img = new Image();
img.src = imgPath;
let cvs = new Canvas(img.width, img.height);
let ctx = cvs.getContext('2d', {pixelFormat: 'A8'}); // grayscale, I hope
ctx.drawImage(img, 0, 0, img.width, img.height);
return cvs;
}

export async function tensorFromImage (imgPath, height=299, width=299, mean=0,
std=255) {
const canvas = await canvasFromImage(imgPath);
let t = await tf.fromPixels(canvas, IMG_CHANNELS);
t = tf.cast(t, 'float32');
t = t.expandDims(0);
t = tf.image.resizeBilinear(t, [height, width]);
t = tf.div(tf.sub(t, [mean]), [std]);
return t;
}

export async function predictionFromImage (imgPath) {
const model = await getModel();
const t = await tensorFromImage(imgPath);
let pred = model.predict(t);
pred = pred.squeeze();
let confMap = getConfidenceMap(await pred.data());
confMap.sort((a, b) => b[1] - a[1]);
return confMap[0][0];
}

function getConfidenceMap (predArr) {
if (predArr.length !== labels.length) {
throw new Error(`Prediction result array had ${predArr.length} elements ` +
`but labels list had ${labels.length} elements. They ` +
`need to match.`);
}
let map = [];
for (let i = 0; i < labels.length; i++) {
map.push([labels[i], predArr[i]]);
}
return map;
}

export default async function find (driver, label, multiple) {
const model = await getModel();
}
110 changes: 110 additions & 0 deletions lib/labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const LABELS = [
" unclassified",
"add",
"airplane",
"alarm",
"arrow down",
"arrow left",
"arrow right",
"arrow up",
"attach",
"bag",
"barcode",
"battery",
"bluetooth",
"bookmark",
"brightness",
"calculator",
"calendar",
"call",
"camera",
"car",
"cart",
"chart",
"check mark",
"clock",
"close",
"cloud",
"computer",
"contrast",
"credit card",
"crop",
"cursor",
"cut",
"dashboard",
"delete",
"dollar",
"download",
"edit",
"external link",
"eye",
"fab",
"facebook",
"fast forward",
"favorite",
"file",
"filter",
"fingerprint",
"fire",
"flag",
"flashlight",
"folder",
"gift",
"globe",
"gmail",
"google",
"grid",
"headphones",
"home",
"inbox",
"info",
"laptop",
"light bulb",
"link",
"location",
"lock",
"mail",
"map",
"maximize",
"megaphone",
"menu",
"microphone",
"minimize",
"mobile",
"moon",
"music",
"mute",
"notifications",
"overflow menu",
"pinterest",
"play",
"printer",
"profile avatar",
"qr code",
"question",
"refresh",
"reply",
"rewind",
"save",
"search",
"send",
"settings",
"share",
"signal",
"sort",
"tag",
"television",
"thumbs up",
"ticket",
"trash",
"trophy",
"twitter",
"unlock",
"upload",
"user",
"video camera",
"volume",
"warning"
];

export default LABELS;
Binary file added model/group1-shard10of22
Binary file not shown.
Binary file added model/group1-shard11of22
Binary file not shown.
Binary file added model/group1-shard12of22
Binary file not shown.
Binary file added model/group1-shard13of22
Binary file not shown.
Binary file added model/group1-shard14of22
Binary file not shown.
Binary file added model/group1-shard15of22
Binary file not shown.
Binary file added model/group1-shard16of22
Binary file not shown.
Binary file added model/group1-shard17of22
Binary file not shown.
Binary file added model/group1-shard18of22
Binary file not shown.
Binary file added model/group1-shard19of22
Binary file not shown.
Binary file added model/group1-shard1of22
Binary file not shown.
Binary file added model/group1-shard20of22
Binary file not shown.
Binary file added model/group1-shard21of22
Binary file not shown.
Binary file added model/group1-shard22of22
Binary file not shown.
Binary file added model/group1-shard2of22
Binary file not shown.
Binary file added model/group1-shard3of22
Binary file not shown.
Binary file added model/group1-shard4of22
Binary file not shown.
Binary file added model/group1-shard5of22
Binary file not shown.
Binary file added model/group1-shard6of22
Binary file not shown.
Binary file added model/group1-shard7of22
Binary file not shown.
Binary file added model/group1-shard8of22
Binary file not shown.
Binary file added model/group1-shard9of22
Binary file not shown.
Binary file added model/model.pb
Binary file not shown.
1 change: 1 addition & 0 deletions model/weights.json

Large diffs are not rendered by default.

Loading

0 comments on commit be5d9e5

Please sign in to comment.