Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
We all know, how cool an android app looks, when it can detect our face or track if our eyes are closed or open. It becomes way more cooler when the app can even detect if we are smiling, reading on phone or not looking atĀ it.
Wellā¦
I believe.. whatever does appeals me, simply makes me buildĀ it!
Sorry, tried a āThe Dark Knightā punĀ :)
So let us make an android eye tracking and face detection app using Google VisionĀ API.
From Google:
Cloud Vision API enables developers to understand the content of an image by encapsulating powerful machine learning models in an easy-to-use REST API. It quickly classifies images into thousands of categories (such as, āsailboatā), detects individual objects and faces within images, and reads printed words contained within images. You can build metadata on your image catalog, moderate offensive content, or enable new marketing scenarios through image sentiment analysis.
Here we will be making an android app that can track our face, and detect if our eyes are closed orĀ open.
Sounds cool? Itās not much hardĀ even.
So letās dive into understanding how this would work in a simple flowĀ chart.
Our android app ā Uses Camera ā Detects face ā Starts some operation ā Checks if eyes of the viewer is open ā Continues the operation ā If eyes are closed ā Stop the operation.
This is the basic idea for our android app. For learning purposes we will just be doing this much in our app, but much more advanced features can be added using Google VisionĀ API.
The operation that weād be carrying out in our app is playing a simple video. Well, I chose a video from PIXAR Animation.
You can find the full source code for the appĀ here.
Start a new Android project in your IDE like Android Studio or anyĀ other.
Go to the manifest file and add the following permission, as for this app we need the permission to use front Camera of theĀ device.
<uses-permission android:name="android.permission.CAMERA" />
We also have to import Google Vision API in our android app, so head to the Build Gradle for app, mostly written as build.gradle (Module:Ā app).
You will see a file with text that resembles this:
apply plugin: 'com.android.application'android { compileSdkVersion 28 defaultConfig { applicationId "com.pd.trackeye" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.google.android.gms:play-services:12.0.1'}
If it looks like this, then youāre at the correct file destination. Copy the whole code above and paste it onĀ that.
Youāll get a top-bar pop-up stating to sync files. Click on Sync Now and wait for the project toĀ build.
After you get all green OK signs in your Build log, you can moveĀ ahead.
Now letās write some android code that will do the magic. Head to the MainActivity of the file, and declare following variables, likeĀ this:
private static final String TAG = "MainActivity";VideoView videoView;EditText textView;//For looking logsArrayAdapter adapter;ArrayList<String> list = new ArrayList<>();CameraSource cameraSource;
These lines of code should be beneath the line that states something likeĀ this:
public class MainActivity extends AppCompatActivity {
Now letās do the magic code inside the onCreate() method that you will see on the sameĀ file.
Letās first add something in our code that lets the user grant permission to add the android app, as without that our app will crash and notĀ work.
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); Toast.makeText(this, "Grant Permission and restart app", Toast.LENGTH_SHORT).show();}else { videoView = findViewById(R.id.videoView); textView = findViewById(R.id.textView); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list); videoView.setVideoURI(Uri.parse("android.re
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.