Skip to content

Commit db12921

Browse files
committed
Add new api, setSelectedLabel
1 parent b325025 commit db12921

File tree

8 files changed

+64
-14
lines changed

8 files changed

+64
-14
lines changed

‎app/src/main/java/com/tzutalin/vision/demo/ObjectDetectActivity.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727
import android.view.Menu;
2828
import android.view.MenuItem;
2929
import android.view.Window;
30-
import android.widget.ImageView;
3130
import android.widget.Toast;
3231

32+
import com.dexafree.materialList.card.Card;
33+
import com.dexafree.materialList.card.provider.BigImageCardProvider;
34+
import com.dexafree.materialList.view.MaterialListView;
3335
import com.tzutalin.vision.visionrecognition.ObjectDetector;
3436
import com.tzutalin.vision.visionrecognition.R;
3537
import com.tzutalin.vision.visionrecognition.VisionClassifierCreator;
3638
import com.tzutalin.vision.visionrecognition.VisionDetRet;
37-
import com.dexafree.materialList.card.Card;
38-
import com.dexafree.materialList.card.provider.BigImageCardProvider;
39-
import com.dexafree.materialList.view.MaterialListView;
4039

4140
import java.io.File;
4241
import java.util.ArrayList;
@@ -104,6 +103,7 @@ protected List<VisionDetRet> doInBackground(String... strings) {
104103
mObjectDet = VisionClassifierCreator.createObjectDetector(getApplicationContext());
105104
// TODO: Get Image's height and width
106105
mObjectDet.init(0, 0);
106+
//mObjectDet.setSelectedLabel("person");
107107
} catch (IllegalAccessException e) {
108108
e.printStackTrace();
109109
}

‎cnnlibs/src/main/java/com/tzutalin/vision/visionrecognition/CaffeClassifier.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public abstract class CaffeClassifier<T> {
5555
protected String[] mSynsets;
5656
protected int mImgWidth;
5757
protected int mImgHeight;
58+
private String mSelectedLabel;
5859

5960
/**
6061
*
@@ -103,6 +104,15 @@ public void init(int imgWidth, int imgHeight) {
103104
public void deInit() {
104105
}
105106

107+
108+
public void setSelectedLabel(String label) {
109+
mSelectedLabel = label;
110+
}
111+
112+
public void clearSelectedLabel() {
113+
mSelectedLabel = null;
114+
}
115+
106116
private String[] getSynsetsFromFile(Context context) {
107117
if (!TextUtils.isEmpty(mSynsetPath)) {
108118
File file = new File(mSynsetPath);

‎cnnlibs/src/main/java/com/tzutalin/vision/visionrecognition/ObjectDetector.java‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.Context;
2020
import android.graphics.Bitmap;
2121
import android.graphics.BitmapFactory;
22+
import android.text.TextUtils;
2223
import android.util.Log;
2324

2425
import java.io.File;
@@ -48,7 +49,7 @@ public ObjectDetector(Context context, String modelPath, String wieghtsPath, Str
4849
if (new File(mModelPath).exists() == false ||
4950
new File(mWeightsPath).exists() == false ||
5051
new File(mSynsetPath).exists() == false ) {
51-
throw new IllegalAccessException(" Cannot find model");
52+
throw new IllegalAccessException("ObjectDetector cannot find model");
5253
}
5354
}
5455

@@ -63,6 +64,19 @@ public void deInit() {
6364
super.deInit();
6465
jniRelease();
6566
}
67+
68+
@Override
69+
public void setSelectedLabel(String label) {
70+
super.setSelectedLabel(label);
71+
jniSetSelectedLabel(label);
72+
}
73+
74+
@Override
75+
public void clearSelectedLabel() {
76+
super.clearSelectedLabel();
77+
jniSetSelectedLabel("");
78+
}
79+
6680
/**
6781
* Detect and locate objects according to the given image path
6882
* @param imgPath image path
@@ -73,13 +87,13 @@ public void deInit() {
7387
*/
7488
@Override
7589
public List<VisionDetRet> classifyByPath(String imgPath) {
76-
// TODO : If file path didn't exist
7790
List<VisionDetRet> ret = new ArrayList<VisionDetRet>();
7891

79-
/* BitmapFactory.Options options = new BitmapFactory.Options();
80-
options.inSampleSize = 1;
81-
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
82-
return classify(bitmap);*/
92+
if (TextUtils.isEmpty(imgPath) || new File(imgPath).exists() == false) {
93+
Log.e(TAG, "classifyByPath. Invalid Input path");
94+
return ret;
95+
}
96+
8397
int numObjs = jniClassifyImgByPath(imgPath);
8498
for (int i = 0; i != numObjs; i++) {
8599
VisionDetRet det = new VisionDetRet();
@@ -106,15 +120,23 @@ public List<VisionDetRet> classify(Bitmap bitmap) {
106120
"bitmap size doesn't match initialization");
107121
}*/
108122
List<VisionDetRet> ret = new ArrayList<VisionDetRet>();
123+
124+
// Check input
125+
if (bitmap == null) {
126+
Log.e(TAG, "classify. Invalid Input bitmap");
127+
return ret;
128+
}
129+
109130
storeBitmap(bitmap);
131+
110132
int numObjs = jniClassifyBitmap(_handler);
111133
for (int i = 0; i != numObjs; i++) {
112134
VisionDetRet det = new VisionDetRet();
113135
int success = jniGetDetRet(det, i);
114-
android.util.Log.d(TAG , det.getLabel());
115136
if (success >= 0)
116137
ret.add(det);
117138
}
139+
118140
freeBitmap();
119141
return ret;
120142
}
@@ -136,6 +158,8 @@ private void freeBitmap() {
136158

137159
protected native int jniRelease();
138160

161+
protected native int jniSetSelectedLabel(String label);
162+
139163
protected native int jniClassifyImgByPath(String imgPath);
140164

141165
private native int jniClassifyBitmap(ByteBuffer handler);

‎cnnlibs/src/main/java/com/tzutalin/vision/visionrecognition/SceneClassifier.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import android.content.Context;
2020
import android.graphics.Bitmap;
21+
import android.text.TextUtils;
22+
import android.util.Log;
2123

2224
import java.io.File;
2325
import java.io.FileOutputStream;
@@ -32,6 +34,7 @@
3234
* {@link android.graphics.Bitmap} graphic object.
3335
*/
3436
public class SceneClassifier extends CaffeClassifier<List<VisionDetRet>> {
37+
private static final String TAG = "SceneClassifier";
3538
private static final int MODEL_DIM = 224;
3639
private ByteBuffer _handler = null;
3740

@@ -50,7 +53,7 @@ public SceneClassifier(Context context, String sceneModelPath, String sceneWiegh
5053
if (new File(mModelPath).exists() == false ||
5154
new File(mWeightsPath).exists() == false ||
5255
new File(mSynsetPath).exists() == false ) {
53-
throw new IllegalAccessException(" Cannot find model");
56+
throw new IllegalAccessException("SceneClassifier cannot find model");
5457
}
5558
}
5659

@@ -65,9 +68,14 @@ public SceneClassifier(Context context, String sceneModelPath, String sceneWiegh
6568
*/
6669
@Override
6770
public List<VisionDetRet> classifyByPath(String imgPath) {
68-
float[] propArray = jniClassifyImgByPath(imgPath);
6971
List<VisionDetRet> ret = new ArrayList<VisionDetRet>();
7072

73+
if (TextUtils.isEmpty(imgPath) || new File(imgPath).exists() == false) {
74+
Log.e(TAG, "classifyByPath. Invalid Input path");
75+
return ret;
76+
}
77+
78+
float[] propArray = jniClassifyImgByPath(imgPath);
7179
if (propArray != null) {
7280
Map<String, Float> sortedmap = Utils.sortPrediction(mSynsets, propArray);
7381
int k_size = 10;
@@ -78,6 +86,7 @@ public List<VisionDetRet> classifyByPath(String imgPath) {
7886
break;
7987
}
8088
}
89+
8190
return ret;
8291
}
8392

@@ -92,9 +101,16 @@ public List<VisionDetRet> classifyByPath(String imgPath) {
92101
*/
93102
@Override
94103
public List<VisionDetRet> classify(Bitmap bitmap) {
104+
List<VisionDetRet> ret = new ArrayList<VisionDetRet>();
105+
106+
// Check input
107+
if (bitmap == null) {
108+
Log.e(TAG, "classify. Invalid Input bitmap");
109+
return ret;
110+
}
111+
95112
storeBitmap(bitmap);
96113

97-
List<VisionDetRet> ret = new ArrayList<VisionDetRet>();
98114
float[] propArray = jniClassifyBitmap(_handler);
99115
if (propArray != null) {
100116
Map<String, Float> sortedmap = Utils.sortPrediction(mSynsets, propArray);
-448 KB
Binary file not shown.
-4 KB
Binary file not shown.
-752 KB
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)