1 package com.visagetechnologies.showcasedemo;
3 import android.content.Intent;
4 import android.content.res.AssetManager;
5 import android.content.res.Configuration;
6 import android.graphics.Typeface;
7 import android.os.Bundle;
8 import android.os.Handler;
10 import androidx.activity.result.ActivityResultLauncher;
11 import androidx.activity.result.contract.ActivityResultContracts;
12 import androidx.fragment.app.FragmentActivity;
13 import androidx.core.view.GravityCompat;
14 import androidx.drawerlayout.widget.DrawerLayout;
15 import androidx.appcompat.widget.SwitchCompat;
16 import android.util.Log;
17 import android.view.KeyEvent;
18 import android.view.View;
19 import android.view.ViewGroup;
20 import android.view.WindowManager;
21 import android.widget.ImageView;
22 import android.widget.LinearLayout;
23 import android.widget.ProgressBar;
24 import android.widget.TextView;
27 import java.io.FileOutputStream;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.lang.ref.WeakReference;
31 import java.util.ArrayList;
33 import static java.lang.Math.min;
35 public class MainActivity
extends FragmentActivity
36 implements View.OnClickListener {
38 private static final String TAG =
"CameraSample";
40 private SwitchCompat[] switchCompat;
41 private final Integer[] switchIDs = {
55 private final Integer[] emotionProgressIDs = {
65 private final Integer[] emotionProgressTextIDs = {
75 private final int[] displayValues = {
76 VisageWrapper.DISPLAY_FEATURE_POINTS,
77 VisageWrapper.DISPLAY_IRIS,
78 VisageWrapper.DISPLAY_SPLINES,
79 VisageWrapper.DISPLAY_GAZE,
80 VisageWrapper.DISPLAY_AXES,
82 VisageWrapper.DISPLAY_WIRE_FRAME,
84 VisageWrapper.DISPLAY_AGE,
85 VisageWrapper.DISPLAY_GENDER,
86 VisageWrapper.DISPLAY_EMOTIONS
89 Integer numSwitches = switchIDs.length;
90 private TrackingFragment currentFragmentForTracking;
91 private DrawerLayout mDrawerLayout;
92 private VisageWrapper visageWrapper = null;
94 private LinearLayout analyserResults;
95 private LinearLayout appBar;
97 private int ACTIVE_DISPLAY = 0;
98 private final Handler mHandler =
new Handler();
100 boolean mediaFragmentPermissionGranted =
false;
101 boolean mediaFragmentPermissionDenied =
false;
103 public ActivityResultLauncher<String[]> mediaFragmentPermissionLouncher;
106 System.loadLibrary(
"VisageWrapper");
110 public void onCreate(Bundle savedInstanceState) {
111 super.onCreate(savedInstanceState);
112 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
113 WindowManager.LayoutParams.FLAG_FULLSCREEN);
115 visageWrapper = VisageWrapper.get(
this);
117 visageWrapper.AllocateResources();
119 copyAssets(getFilesDir().getAbsolutePath());
120 currentFragmentForTracking = visageWrapper.isTrackingFromMedia() ? TrackingFragment.Media : TrackingFragment.Camera2;
122 if (null == savedInstanceState) {
123 this.getSupportFragmentManager()
125 .replace(R.id.fragment_container,
126 (currentFragmentForTracking == TrackingFragment.Camera2) ? Camera2Fragment.get() : MediaFragment.get(
this))
130 mediaFragmentPermissionLouncher = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(), isGranted -> {
131 if (isGranted.get(MediaFragment.READ_PERMISSIONS[0])
132 || (isGranted.get(MediaFragment.READ_PERMISSIONS[1]) && isGranted.get(MediaFragment.READ_PERMISSIONS[2])))
134 mediaFragmentPermissionGranted =
true;
138 Log.e(TAG,
"Not enough privileges");
139 mediaFragmentPermissionDenied =
true;
145 private void initializeViews() {
146 setContentView(R.layout.activity_main);
150 attachDisplaySwitches();
151 attachViewListeners();
153 updateButtonsVisibility();
155 initializeAnalyserDisplay();
157 mDrawerLayout = findViewById(R.id.drawer_layout);
159 appBar = findViewById(R.id.app_bar);
161 setFontFamily(mDrawerLayout,
"bariol_regular");
162 setFontFamily(mDrawerLayout,
"bariol_bold");
164 mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
166 public void onDrawerSlide(View drawerView,
float slideOffset) {
171 public void onDrawerOpened(View drawerView) {
176 public void onDrawerClosed(View drawerView) {
177 int orientation = getResources().getConfiguration().orientation;
178 if (orientation == Configuration.ORIENTATION_LANDSCAPE)
179 SlideAnimationUtil.slideInFromRight(getApplicationContext(), appBar);
181 SlideAnimationUtil.slideInFromTop(getApplicationContext(), appBar);
185 public void onDrawerStateChanged(
int newState) {
192 public void onConfigurationChanged(Configuration newConfiguration) {
193 super.onConfigurationChanged(newConfiguration);
194 boolean[] switchChecked =
new boolean[switchCompat.length];
195 for (
int i = 0; i < switchChecked.length; i++) {
196 switchChecked[i] = switchCompat[i].isChecked();
201 for (
int i = 0; i < min(switchCompat.length, switchChecked.length); i++) {
202 switchCompat[i].setChecked(switchChecked[i]);
205 updateTrackingDisplay();
206 updateAnalyserDisplay();
210 public void onResume() {
212 updateTrackingDisplay();
213 updateAnalyserDisplay();
217 public void onPause() {
222 public void onDestroy() {
224 visageWrapper.DeallocateResources();
225 visageWrapper.onDestroy();
229 public void onClick(View v) {
230 int orientation = getResources().getConfiguration().orientation;
234 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
235 mDrawerLayout.openDrawer(GravityCompat.END);
236 SlideAnimationUtil.slideOutToRight(
this, appBar);
238 mDrawerLayout.openDrawer(GravityCompat.START);
239 SlideAnimationUtil.slideOutToTop(
this, appBar);
243 case R.id.openGallery:
244 if (!visageWrapper.isTrackingFromMedia())
245 changeFragmentForTracking();
247 visageWrapper.onPause();
248 var mediaFragment = MediaFragment.get(
this);
250 getSupportFragmentManager().beginTransaction()
251 .setReorderingAllowed(
false)
252 .replace(R.id.fragment_container, mediaFragment)
256 updateButtonsVisibility();
259 case R.id.openCamera:
260 if (visageWrapper.isTrackingFromMedia())
261 changeFragmentForTracking();
263 updateButtonsVisibility();
266 case R.id.switchCamera:
267 Camera2Fragment camera2Fragment = Camera2Fragment.get();
269 if (camera2Fragment != null)
270 camera2Fragment.switchCamera();
273 case R.id.feature_points:
275 case R.id.gaze_tracking:
278 updateTrackingDisplay();
279 updateAnalyserDisplay();
285 updateTrackingDisplay();
286 updateAnalyserDisplay();
295 toggleRefineLandmarks();
308 public void copyFile(String rootDir, String filename) {
309 AssetManager assetManager = this.getAssets();
311 InputStream in = null;
312 OutputStream out = null;
314 String newFileName = rootDir + File.separator + filename;
315 File file =
new File(newFileName);
317 if (!file.exists()) {
318 in = assetManager.open(
"trackerdata/" + filename);
319 out =
new FileOutputStream(newFileName);
321 byte[] buffer =
new byte[4 * 1024];
323 while ((read = in.read(buffer)) != -1) {
324 out.write(buffer, 0, read);
333 }
catch (Exception e) {
334 Log.e(TAG, e.getMessage());
343 public void copyAssets(String rootDir) {
344 AssetManager assetManager = this.getAssets();
346 String assets[] = null;
348 assets = assetManager.list(
"trackerdata");
350 for (String asset : assets) {
351 Log.i(TAG, rootDir + File.separator + asset);
353 if (!asset.contains(
"vfa") && !asset.contains(
"vfr")
354 && !asset.contains(
"vft")) copyFile(rootDir, asset);
355 }
catch (Exception e) {
356 Log.e(TAG, e.getMessage());
359 }
catch (Exception e) {
360 Log.e(TAG, e.getMessage());
364 final String[] dirs = {
378 for (String dirname : dirs) {
380 File dir =
new File(rootDir + File.separator + dirname);
381 if (!dir.exists()) dir.mkdir();
382 }
catch (Exception e) {
383 Log.e(TAG, e.getMessage());
388 final String[] files = {
400 "vft/fa/aux_file.bin",
401 "vft/fa/d1qy.tflite",
404 "vft/fm/candide3.fdp",
405 "vft/fm/candide3.wfm",
409 "vft/fm/jk_300_detector.cfg",
410 "vft/fm/jk_300_wEars.fdp",
411 "vft/fm/jk_300_wEars.wfm",
412 "vft/fm/jk_300_wEars.cfg",
416 for (String filename : files) {
418 Log.i(TAG, rootDir + File.separator + filename);
419 copyFile(rootDir, filename);
420 }
catch (Exception e) {
421 Log.e(TAG, e.getMessage());
427 public void onActivityResult(
int requestCode,
int resultCode, Intent data) {
428 super.onActivityResult(requestCode, resultCode, data);
431 public void changeFragmentForTracking() {
432 currentFragmentForTracking = currentFragmentForTracking == TrackingFragment.Camera2 ? TrackingFragment.Media : TrackingFragment.Camera2;
434 getSupportFragmentManager().beginTransaction()
435 .setReorderingAllowed(
false)
436 .replace(R.id.fragment_container,
437 currentFragmentForTracking == TrackingFragment.Camera2 ? Camera2Fragment.get() : MediaFragment.get(
this))
442 private void attachTrackerView() {
443 ViewGroup parent = findViewById(R.id.parentMain);
444 View trackerView = visageWrapper.getTrackerView();
446 if (trackerView.getParent() != null)
447 ((ViewGroup) trackerView.getParent()).removeView(trackerView);
449 parent.addView(trackerView, 1);
452 private void attachViewListeners() {
453 findViewById(R.id.openMenu).setOnClickListener(
this);
454 findViewById(R.id.openCamera).setOnClickListener(
this);
455 findViewById(R.id.openGallery).setOnClickListener(
this);
456 findViewById(R.id.switchCamera).setOnClickListener(
this);
458 ViewGroup trackerOptions = findViewById(R.id.tracker_options);
460 if (trackerOptions == null)
463 for (
int index = 0; index < trackerOptions.getChildCount(); ++index) {
464 TextView nextChild = (TextView) trackerOptions.getChildAt(index);
465 nextChild.setOnClickListener(
this);
469 private void attachDisplaySwitches() {
470 switchCompat =
new SwitchCompat[numSwitches];
472 for (
int i = 0; i < numSwitches; i++) {
473 switchCompat[i] = findViewById(switchIDs[i]);
478 private void initializeAnalyserDisplay() {
479 analyserResults = findViewById(R.id.analyser_results);
481 TextView ageTV = findViewById(R.id.age_tv);
482 ImageView genderIV = findViewById(R.id.gender_iv);
483 TextView genderTV = findViewById(R.id.gender_tv);
485 ProgressBar[] emotionProgressBar =
new ProgressBar[VisageWrapper.NUM_EMOTIONS];
486 TextView[] emotionTV =
new TextView[VisageWrapper.NUM_EMOTIONS];
488 TextView instructionMessageTV = findViewById(R.id.instructionM_tv);
490 for (
int i = 0; i < VisageWrapper.NUM_EMOTIONS; i++) {
491 emotionProgressBar[i] = findViewById(emotionProgressIDs[i]);
492 emotionTV[i] = findViewById(emotionProgressTextIDs[i]);
495 mHandler.postDelayed(
new DoneRunnable(ageTV, genderIV, genderTV, emotionProgressBar, emotionTV, instructionMessageTV, mHandler), 20);
498 private void updateAnalyserDisplay() {
499 if (analyserResults == null)
502 int orientation = getResources().getConfiguration().orientation;
504 if ((ACTIVE_DISPLAY & VisageWrapper.DISPLAY_AGE) +
505 (ACTIVE_DISPLAY & VisageWrapper.DISPLAY_GENDER) +
506 (ACTIVE_DISPLAY & VisageWrapper.DISPLAY_EMOTIONS)
508 if (analyserResults.getVisibility() == View.GONE) {
509 if (orientation == Configuration.ORIENTATION_LANDSCAPE)
510 SlideAnimationUtil.slideInFromLeft(
this, analyserResults);
512 SlideAnimationUtil.slideInFromBottom(
this, analyserResults);
515 if (analyserResults.getVisibility() == View.VISIBLE) {
516 if (orientation == Configuration.ORIENTATION_LANDSCAPE)
517 SlideAnimationUtil.slideOutToLeft(
this, analyserResults);
519 SlideAnimationUtil.slideOutToBottom(
this, analyserResults);
524 private void toggleEars() {
525 if (visageWrapper != null)
527 visageWrapper.toggleEars();
531 private void toggleIris() {
532 if (visageWrapper != null)
534 visageWrapper.toggleIris();
538 private void toggleRefineLandmarks() {
539 if (visageWrapper != null)
541 if (((SwitchCompat)findViewById(R.id.vnn)).isChecked())
543 visageWrapper.toggleRefineLandmarks(
true);
544 ((TextView)findViewById(R.id.vnn)).setText(
"High");
548 visageWrapper.toggleRefineLandmarks(
false);
549 ((TextView)findViewById(R.id.vnn)).setText(
"Low");
554 public void initAnalysis() {
555 visageWrapper.initAnalyser();
558 public void updateTrackingDisplay() {
559 if (visageWrapper == null)
562 ACTIVE_DISPLAY = VisageWrapper.DISPLAY_POINT_QUALITY;
563 for (
int i = 0; i < switchCompat.length; i++) {
564 if (switchCompat[i] == null)
566 if (switchCompat[i].isChecked())
567 ACTIVE_DISPLAY += displayValues[i];
571 SetDisplayOptions(ACTIVE_DISPLAY);
574 void updateButtonsVisibility() {
575 if (currentFragmentForTracking == TrackingFragment.Camera2) {
577 findViewById(R.id.switchCamera).setVisibility(View.VISIBLE);
578 findViewById(R.id.openCamera).setVisibility(View.GONE);
581 findViewById(R.id.switchCamera).setVisibility(View.GONE);
582 findViewById(R.id.openCamera).setVisibility(View.VISIBLE);
586 private enum TrackingFragment {
591 private static final class DoneRunnable
implements Runnable {
592 private final WeakReference<TextView> mAgeTV;
593 private final WeakReference<ImageView> mGenderIV;
594 private final WeakReference<TextView> mGenderTV;
596 private final WeakReference[] mEmotionPB =
new WeakReference[VisageWrapper.NUM_EMOTIONS];
597 private final WeakReference[] mEmotionTV =
new WeakReference[VisageWrapper.NUM_EMOTIONS];
598 private final WeakReference<TextView> mInstructionMesssage;
599 private final WeakReference<Handler> mHandlerRef;
603 private float[] emotions;
604 private boolean showInstructionMessage;
606 DoneRunnable(TextView ageTV, ImageView genderIV, TextView genderTV, ProgressBar[] emotionPB, TextView[] emotionsTV, TextView instructionMessageTV, Handler handler) {
608 mAgeTV =
new WeakReference<>(ageTV);
609 mGenderIV =
new WeakReference<>(genderIV);
610 mGenderTV =
new WeakReference<>(genderTV);
612 for (
int i = 0; i < VisageWrapper.NUM_EMOTIONS; i++) {
613 mEmotionPB[i] =
new WeakReference<>(emotionPB[i]);
614 mEmotionTV[i] =
new WeakReference<>(emotionsTV[i]);
617 mInstructionMesssage =
new WeakReference<>(instructionMessageTV);
619 mHandlerRef =
new WeakReference<>(handler);
624 final TextView ageTV = mAgeTV.get();
627 age = VisageWrapper.GetAge();
630 ageTV.setText(
" - ");
632 ageTV.setVisibility(View.VISIBLE);
633 ageTV.setText(String.valueOf(Math.round(age)));
637 final ImageView genderIV = mGenderIV.get();
638 final TextView genderTV = mGenderTV.get();
640 if (genderIV != null && genderTV != null) {
641 gender = VisageWrapper.GetGender();
644 genderIV.setVisibility(View.GONE);
645 genderTV.setVisibility(View.VISIBLE);
647 }
else if (gender == 0) {
648 genderIV.setImageResource(R.drawable.female);
649 genderIV.setVisibility(View.VISIBLE);
650 genderTV.setVisibility(View.INVISIBLE);
652 genderIV.setImageResource(R.drawable.male);
653 genderIV.setVisibility(View.VISIBLE);
654 genderTV.setVisibility(View.INVISIBLE);
659 emotions = VisageWrapper.GetEmotions();
660 final ProgressBar[] emotionPB =
new ProgressBar[VisageWrapper.NUM_EMOTIONS];
661 final TextView[] emotionTV =
new TextView[VisageWrapper.NUM_EMOTIONS];
663 for (
int i = 0; i < VisageWrapper.NUM_EMOTIONS; i++) {
665 emotionPB[i] = (ProgressBar) mEmotionPB[i].
get();
666 emotionTV[i] = (TextView) mEmotionTV[i].
get();
668 if (emotionPB[i] != null) {
670 if (emotions[i] == -1) {
671 emotionPB[i].setVisibility(View.INVISIBLE);
673 emotionPB[i].setVisibility(View.VISIBLE);
674 emotionPB[i].setProgress(Math.round(emotions[i] * 100));
679 if (emotionTV[i] != null) {
680 String display = Math.round(emotions[i] * 100) +
" %";
682 if (emotions[i] == -1) {
683 emotionTV[i].setText(
" - ");
685 emotionTV[i].setVisibility(View.VISIBLE);
686 emotionTV[i].setText(display);
691 final TextView instructionMessageTV = mInstructionMesssage.get();
693 if (instructionMessageTV != null) {
694 showInstructionMessage = VisageWrapper.ShowInstruction();
696 if (showInstructionMessage) {
697 instructionMessageTV.setVisibility(View.VISIBLE);
699 instructionMessageTV.setVisibility(View.INVISIBLE);
703 final Handler handler = mHandlerRef.get();
705 handler.postDelayed(
this, 20);
709 public boolean onKeyDown(
int keycode, KeyEvent event) {
710 if (keycode == KeyEvent.KEYCODE_BACK) {
711 moveTaskToBack(
true);
713 return super.onKeyDown(keycode, event);
716 private void setFontFamily(ViewGroup viewGroup, String fontName) {
717 ArrayList<View> textViews = getViewsByTag(viewGroup, fontName);
719 Typeface m_typeFace = Typeface.createFromAsset(getAssets(),
"fonts/" + fontName +
".otf");
721 for (View view : textViews) {
722 if (view instanceof TextView) {
723 TextView textView = (TextView) view;
724 textView.setTypeface(m_typeFace);
730 private static ArrayList<View> getViewsByTag(ViewGroup root, String tag) {
731 ArrayList<View> views =
new ArrayList<View>();
732 final int childCount = root.getChildCount();
733 for (
int i = 0; i < childCount; i++) {
734 final View child = root.getChildAt(i);
735 if (child instanceof ViewGroup) {
736 views.addAll(getViewsByTag((ViewGroup) child, tag));
739 final Object tagObj = child.getTag();
740 if (tagObj != null && tagObj.equals(tag)) {