visageSDK
FaceDetectDemo/app/src/main/java/com/visagetechnologies/facedetectdemo/ImagesActivity.java
1 package com.visagetechnologies.facedetectdemo;
2 
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 
8 import android.Manifest;
9 import android.app.Activity;
10 import android.content.Intent;
11 import android.content.pm.PackageManager;
12 import android.content.res.AssetManager;
13 import android.database.Cursor;
14 import androidx.core.app.ActivityCompat;
15 import androidx.core.content.ContextCompat;
16 import android.util.Log;
17 import android.view.View;
18 import android.view.WindowManager;
19 import android.net.Uri;
20 import android.os.Bundle;
21 import android.provider.MediaStore;
22 import android.widget.Button;
23 
24 
28 public class ImagesActivity extends Activity
29 {
30  private static final String TAG = "ImagesActivity";
31 
32  private static int RESULT_LOAD_IMAGE = 1;
33  private boolean doRestart = false;
34  final int MY_PERMISSIONS_REQUEST_STORAGE = 1;
35 
36  boolean recognition = false;
37  boolean detection = false;
38 
39  Intent intentRecognition;
40 
44  @Override
45  public void onCreate(Bundle savedInstanceState)
46  {
47  super.onCreate(savedInstanceState);
48  copyAssets(getFilesDir().getAbsolutePath());
49  setContentView(R.layout.main);
50  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
51 
52  Button buttonDetectClick = (Button)findViewById(R.id.btnDetect);
53  buttonDetectClick.setOnClickListener(new View.OnClickListener() {
54  public void onClick(View v) {
55  recognition = false; detection = true;
57  }
58  });
59 
60  Button buttonRecognitionClick = (Button)findViewById(R.id.btnRecognize);
61  buttonRecognitionClick.setOnClickListener(new View.OnClickListener() {
62  public void onClick(View v) {
63  intentRecognition = new Intent(v.getContext(), RecognitionActivity.class);
64  recognition = true; detection = false;
66  }
67  });
68  }
69 
72  public void handlePermissions() {
73  if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED
74  && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VIDEO) != PackageManager.PERMISSION_GRANTED)
75  {
76  ActivityCompat.requestPermissions(this,
77  new String[]{
78  Manifest.permission.READ_EXTERNAL_STORAGE,
79  Manifest.permission.READ_MEDIA_IMAGES,
80  Manifest.permission.READ_MEDIA_VIDEO,
81  },
82  MY_PERMISSIONS_REQUEST_STORAGE);
83  }
84  else
85  {
86  if (recognition == true)
87  StartRecognition();
88  else if (detection == true)
89  StartDetection();
90 
91  }
92  }
95  @Override
96  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
97  switch (requestCode) {
98  case MY_PERMISSIONS_REQUEST_STORAGE: {
99  // If request is cancelled, the result arrays are empty.
100 
101  if (grantResults[0] == PackageManager.PERMISSION_DENIED) { // READ_EXTERNAL_STORAGE API < 33
102  for (int i = 1; i < grantResults.length; ++i) { // API > 33
103  if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
104  Log.e(TAG, "Failed to get permission: " + permissions[i]);
105  return;
106  }
107  }
108  }
109 
110  if (recognition == true)
111  StartRecognition();
112  else if (detection == true)
113  StartDetection();
114 
115  return;
116  }
117  }
118  }
119 
120  public void StartDetection()
121  {
122  Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
123  startActivityForResult(i, RESULT_LOAD_IMAGE);
124  }
125 
126  public void StartRecognition()
127  {
128  Bundle bundle = new Bundle();
129  bundle.putString("image", "/data/data/com.visagetechnologies.facedetectdemo/files/Famous Actors/match/AngelinaJolie.jpg");
130  bundle.putInt("type", 1);
131 
132  intentRecognition.putExtras(bundle);
133 
134  startActivity(intentRecognition);
135  }
136 
140  @Override
141  protected void onActivityResult(int requestCode, int resultCode, Intent data)
142  {
143  super.onActivityResult(requestCode, resultCode, data);
144  if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
145  Uri selectedImage = data.getData();
146 
147  Intent intent = new Intent(this, DetectorActivity.class);
148 
149  Bundle bundle = new Bundle();
150  bundle.putString("imageURI", selectedImage.toString());
151  if (detection)
152  bundle.putInt("type", 0);
153  else
154  bundle.putInt("type", 1);
155 
156  intent.putExtras(bundle);
157  startActivity(intent);
158 
159  }
160  else
161  {
162  finish();
163  }
164 
165  }
166 
172 public void copyFile(String rootDir, String filename) {
173  AssetManager assetManager = this.getAssets();
174 
175  InputStream in = null;
176  OutputStream out = null;
177  try
178  {
179  in = assetManager.open("trackerdata/" + filename);
180  String newFileName = rootDir + "/" + filename;
181  File file = new File(newFileName);
182  if(!file.exists())
183  {
184  out = new FileOutputStream(newFileName);
185 
186  byte[] buffer = new byte[1024];
187  int read;
188  while ((read = in.read(buffer)) != -1)
189  {
190  out.write(buffer, 0, read);
191  }
192  out.flush();
193  out.close();
194  out = null;
195  }
196  in.close();
197  in = null;
198  }
199  catch (Exception e)
200  {
201  Log.e("FaceDetectDemo", e.getMessage());
202  }
203 }
204 
209 public void copyAssets(String rootDir) {
210  AssetManager assetManager = this.getAssets();
211 
212  String assets[] = null;
213  try {
214  assets = assetManager.list("trackerdata");
215 
216  for (String asset : assets) {
217  Log.i("FaceDetectDemo", rootDir + File.separator + asset);
218  try {
219  if (!asset.contains("vfa") && !asset.contains("vfr")
220  && !asset.contains("vft") && !asset.contains("Famous Actors"))
221  copyFile(rootDir, asset);
222  } catch (Exception e) {
223  Log.e("FaceDetectDemo", e.getMessage());
224  }
225  }
226  } catch (Exception e) {
227  Log.e("FaceDetectDemo", e.getMessage());
228  }
229 
230  // create dirs
231  final String[] dirs = {
232  "Famous Actors",
233  "Famous Actors/gallery",
234  "Famous Actors/match",
235  "vfa",
236  "vfa/ad",
237  "vfa/ed",
238  "vfa/gd",
239  "vfr",
240  "vft",
241  "vft/er",
242  "vft/fa",
243  "vft/ff",
244  "vft/fm",
245  "vft/pr"
246  };
247 
248  for (String dirname : dirs) {
249  try {
250  File dir = new File(rootDir + File.separator + dirname);
251  if (!dir.exists()) dir.mkdir();
252  } catch (Exception e) {
253  Log.e("FaceDetectDemo", e.getMessage());
254  }
255  }
256 
257  // copy files
258  final String[] files = {
259  "Famous Actors/gallery/AngelinaJolie.jpg",
260  "Famous Actors/gallery/AudreyHepburn.jpg",
261  "Famous Actors/gallery/BruceLee.jpg",
262  "Famous Actors/gallery/MattDamon.jpg",
263  "Famous Actors/gallery/RobertDeNiro.jpg",
264  "Famous Actors/gallery/ScarlettJohansson.jpg",
265  "Famous Actors/match/AngelinaJolie.jpg",
266  "Famous Actors/match/AudreyHepburn.jpg",
267  "Famous Actors/match/BruceLee.jpg",
268  "Famous Actors/match/MattDamon.jpg",
269  "Famous Actors/match/RobertDeNiro.jpg",
270  "Famous Actors/match/ScarlettJohansson.jpg",
271  "vfr/fr.tflite",
272  "vft/er/efa.lbf",
273  "vft/er/efc.lbf",
274  "vft/fa/aux_file.bin",
275  "vft/fa/d1qy.tflite",
276  "vft/fa/d2.tflite",
277  "vft/ff/ff.tflite",
278  "vft/fm/candide3.fdp",
279  "vft/fm/candide3.wfm",
280  "vft/fm/jk_300.fdp",
281  "vft/fm/jk_300.wfm",
282  "vft/fm/jk_300.cfg",
283  "vft/fm/jk_300_detector.cfg",
284  "vft/fm/jk_300_wEars.fdp",
285  "vft/fm/jk_300_wEars.wfm",
286  "vft/fm/jk_300_wEars.cfg",
287  "vft/pr/pr.tflite"
288  };
289 
290  for (String filename : files) {
291  try {
292  Log.i("FaceDetectDemo", rootDir + File.separator + filename);
293  copyFile(rootDir, filename);
294  } catch(Exception e) {
295  Log.e("FaceDetectDemo", e.getMessage());
296  }
297  }
298 }
299 
300 static {
301  System.loadLibrary("c++_shared");
302  }
303 
304 }
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
Method invoked when the user responds to permission request.
void onCreate(Bundle savedInstanceState)
Implementation of onCreate method provided by Activity interface.
void copyFile(String rootDir, String filename)
Utility method called to copy required file to trackerdata folder.
void copyAssets(String rootDir)
Utility method called to create required directories and initiate copy of all assets required for tra...
Activity called to initiate and control detection.
void onActivityResult(int requestCode, int resultCode, Intent data)
Called when user selects image.