Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from coderbunker/issue#27-eye_tracking
Browse files Browse the repository at this point in the history
Issue#27 eye tracking
  • Loading branch information
julianschmuckli authored Aug 13, 2018
2 parents 55f1d1c + 5063fae commit fafeb0e
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 8 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Give permission to use Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="false"
Expand Down
98 changes: 96 additions & 2 deletions app/src/main/java/com/coderbunker/kioskapp/KioskActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
Expand All @@ -15,25 +17,33 @@
import android.view.Window;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.coderbunker.kioskapp.facerecognition.CameraPreview;
import com.coderbunker.kioskapp.facerecognition.FaceDetectionListener;
import com.coderbunker.kioskapp.lib.HOTP;
import com.coderbunker.kioskapp.lib.TOTP;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;


public class KioskActivity extends Activity {
public class KioskActivity extends Activity implements Observer {

private final Context context = this;
private WebView webView;
private TextView face_detection_score, face_counter_view;
private static String password = "1234";
private static String URL = "";

Expand All @@ -55,6 +65,9 @@ public class KioskActivity extends Activity {

private SharedPreferences prefs;

private Camera mCamera;
private CameraPreview mCameraPreview;

@Override
public void onBackPressed() {
//Do nothing...
Expand All @@ -80,7 +93,7 @@ protected void onCreate(Bundle savedInstanceState) {
prefs = this.getSharedPreferences(
"com.coderbunker.kioskapp", Context.MODE_PRIVATE);

URL = prefs.getString("url", "https://naibaben.github.io/");
URL = prefs.getString("url", "https://coderbunker.github.io/kiosk-web/");
String otp = prefs.getString("otp", null);

if (otp == null) {
Expand All @@ -92,6 +105,8 @@ protected void onCreate(Bundle savedInstanceState) {

//Get the webView and load the URL
webView = findViewById(R.id.webview);
face_detection_score = findViewById(R.id.face_detection_score);
face_counter_view = findViewById(R.id.face_counter);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(final WebView view, String url) {
Expand Down Expand Up @@ -127,6 +142,10 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setAppCacheMaxSize(5000 * 1000 * 1000);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.loadUrl(URL);

Toast.makeText(this, "Loading " + URL, Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -185,6 +204,28 @@ public void run() {
});

numbers = new ArrayList<>();


if (checkCameraHardware(this)) {
mCamera = getCameraInstance();
if (mCamera != null) {

FaceDetectionListener faceDetectionListener = new FaceDetectionListener();
faceDetectionListener.addObserver(this);
mCamera.setFaceDetectionListener(faceDetectionListener);

mCameraPreview = new CameraPreview(this, mCamera);

FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);

mCamera.startPreview();
} else {

}
}


}


Expand Down Expand Up @@ -398,4 +439,57 @@ public void onClick(View v) {
dialog.show();
}

public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
e.printStackTrace();
}
return c; // returns null if camera is unavailable
}

private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}

@Override
protected void onPause() {
super.onPause();
}

private long last_detected = 0;
private long face_current_counter = 0;
private long face_counter = 0;

@Override
public void update(Observable o, Object arg) {
if (o instanceof FaceDetectionListener) {
Camera.Face face = ((Camera.Face) arg);

face_detection_score.setText("Score:" + face.score);

if (face.score >= 85) {
face_current_counter++;
} else {
face_current_counter = 0;
}

if (face_current_counter >= 5 && last_detected < System.currentTimeMillis() + 45000) {
face_counter++;
last_detected = System.currentTimeMillis();
face_current_counter = -5000;
}

face_counter_view.setText("Viewers: " + face_counter);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onClick(View v) {
});

String otp = prefs.getString("otp", null);
String url = prefs.getString("url", "https://naibaben.github.io/");
String url = prefs.getString("url", "https://coderbunker.github.io/kiosk-web/");
int hotp_counter = prefs.getInt("hotp_counter", 0);

editURL.setText(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.coderbunker.kioskapp.facerecognition;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

import static android.content.ContentValues.TAG;

/**
* A basic Camera preview class
*/
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();

startFaceDetection(); // start face detection feature

} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (holder.getSurface() == null) {
// preview surface does not exist
Log.d(TAG, "holder.getSurface() == null");
return;
}

try {
mCamera.stopPreview();

} catch (Exception e) {
// ignore: tried to stop a non-existent preview
Log.d(TAG, "Error stopping camera preview: " + e.getMessage());
}

try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();

startFaceDetection(); // re-start face detection feature

} catch (Exception e) {
// ignore: tried to stop a non-existent preview
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}

public void startFaceDetection() {
// Try starting Face Detection
Camera.Parameters params = mCamera.getParameters();

// start face detection only *after* preview has started
if (params.getMaxNumDetectedFaces() > 0) {
// camera supports face detection, so can start it:
mCamera.startFaceDetection();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.coderbunker.kioskapp.facerecognition;

import android.hardware.Camera;

import java.util.Observable;

public class FaceDetectionListener extends Observable implements Camera.FaceDetectionListener {

@Override
public void onFaceDetection(Camera.Face[] faces, Camera camera) {
if (faces.length > 0) {
for (Camera.Face face : faces) {
try {
/*System.out.println("--------------------------");
System.out.println(face.score);*/
setChanged();
notifyObservers(face);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
50 changes: 46 additions & 4 deletions app/src/main/res/layout/activity_kiosk.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

</WebView>


<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="visible">

</FrameLayout>

<TextView
android:id="@+id/face_detection_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="21dp"
android:layout_marginEnd="30dp"
android:text="TextView"
android:visibility="gone"
tools:layout_editor_absoluteX="758dp"
tools:layout_editor_absoluteY="1123dp" />

<TextView
android:id="@+id/face_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#000"
android:text="0"
android:textColor="#FFF"
android:textSize="18sp"
android:visibility="visible"
tools:layout_editor_absoluteX="758dp"
tools:layout_editor_absoluteY="1123dp" />

</RelativeLayout>


0 comments on commit fafeb0e

Please sign in to comment.