Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Mar 21, 2017
0 parents commit 39dfa7e
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 Houston Engineering, Inc., http://www.houstoneng.com

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "cordova-plugin-tensorflow",
"version": "0.0.1",
"description": "TensorFlow for Cordova",
"cordova": {
"id": "cordova-plugin-tensorflow",
"platforms": [
"android"
]
},
"repository": {
"type": "git",
"url": "https://github.com/heigeo/cordova-plugin-tensorflow.git"
},
"keywords": [
"ai",
"inference",
"classification",
"imagerecognition",
"neuralnetworks",
"machinelearning",
"tensorflow",
"inception",
"ecosystem:cordova",
"cordova-android"
],
"engines": [
{
"name": "cordova-android",
"version": ">=5.1.0"
}
],
"author": "Houston Engineering, Inc.",
"license": "MIT",
"bugs": {
"url": "https://github.com/heigeo/cordova-plugin-tensorflow/issues"
},
"homepage": "https://github.com/heigeo/cordova-plugin-tensorflow#readme"
}
32 changes: 32 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-tensorflow" version="0.0.1">
<name>TensorFlow for Cordova</name>
<description>Cordova/PhoneGap wrapper for TensorFlow's image recognition binary library.</description>
<license>MIT</license>
<keywords>cordova,device</keywords>

<dependency id="cordova-plugin-file-transfer" />
<dependency id="cordova-plugin-zip" />

<js-module src="www/tensorflow.js" name="tensorflow">
<clobbers target="TensorFlow" />
</js-module>

<platform name="android">
<config-file target="config.xml" parent="/*">
<feature name="TensorFlow">
<param name="android-package" value="io.wq.tensorflow.TensorFlow" />
</feature>
</config-file>

<source-file src="src/android/TensorFlow.java" target-dir="src/io/wq/tensorflow" />
<source-file src="src/android/tf_libs/Classifier.java" target-dir="src/org/tensorflow/demo" />
<source-file src="src/android/tf_libs/TensorFlowImageClassifier.java" target-dir="src/org/tensorflow/demo" />
<source-file src="src/android/tf_libs/libandroid_tensorflow_inference_java.jar" target-dir="libs" />
<source-file src="src/android/tf_libs/armeabi-v7a/libtensorflow_inference.so" target-dir="libs/armeabi" />

</platform>

</plugin>

95 changes: 95 additions & 0 deletions src/android/TensorFlow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.wq.tensorflow;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

import android.util.Base64;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;

import org.tensorflow.demo.TensorFlowImageClassifier;
import org.tensorflow.demo.Classifier.Recognition;
import org.tensorflow.demo.Classifier;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import android.media.ThumbnailUtils;

public class TensorFlow extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("loadModel")) {
this.loadModel(
args.getString(0),
args.getString(1),
args.getString(2),
args.getInt(3),
args.getInt(4),
(float) args.getDouble(5),
args.getString(6),
args.getString(7),
callbackContext
);
return true;
} else if (action.equals("classify")) {
this.classify(args.getString(0), args.getString(1), callbackContext);
return true;
} else {
return false;
}
}


private Map<String,Classifier> classifiers = new HashMap();
private Map<String,Integer> sizes = new HashMap();

private void loadModel(String modelName, String modelFile, String labelFile,
int inputSize, int imageMean, float imageStd,
String inputName, String outputName,
CallbackContext callbackContext) {
classifiers.put(modelName, TensorFlowImageClassifier.create(
cordova.getActivity().getAssets(),
modelFile,
labelFile,
inputSize,
imageMean,
imageStd,
inputName,
outputName
));
sizes.put(modelName, inputSize);
callbackContext.success();
}

private void classify(String modelName, String image, CallbackContext callbackContext) {
byte[] imageData = Base64.decode(image, Base64.DEFAULT);
Classifier classifier = classifiers.get(modelName);
int size = sizes.get(modelName);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
Bitmap cropped = ThumbnailUtils.extractThumbnail(bitmap, size, size);
List<Recognition> results = classifier.recognizeImage(cropped);
JSONArray output = new JSONArray();
try {
JSONObject info = new JSONObject();
info.put("title", "Total");
info.put("confidence", results.size());
output.put(info);
for (Recognition result : results) {
JSONObject record = new JSONObject();
record.put("title", result.getTitle());
record.put("confidence", result.getConfidence());
output.put(record);
}
} catch (JSONException e) {
}
callbackContext.success(output);
}


}
Loading

0 comments on commit 39dfa7e

Please sign in to comment.