@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_face_recognition);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    initializeGraphicComponents();

    route = new Route();

    SupportMapFragment mapFragment =
        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentMap);
    mapFragment.getMapAsync(this);

    mLocationRequest = Utilities.createLocationRequest(getResources());

    // ATTENTION: This "addApi(AppIndex.API)"was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    if (mGoogleApiClient == null) {
      mGoogleApiClient =
          new GoogleApiClient.Builder(this)
              .addApi(LocationServices.API)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(AppIndex.API)
              .build();
    }
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    gSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    textToSpeech =
        new TextToSpeech(
            getApplicationContext(),
            new TextToSpeech.OnInitListener() {
              @Override
              public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                  textToSpeech.setLanguage(Locale.ENGLISH);
                }
              }
            });

    bt = new Bluetooth(this, mHandler);
    connectService();

    ivAddToRoute.setOnLongClickListener(this);

    openCvCameraView = (CameraBridgeViewBase) findViewById(R.id.jcvFaceDetection);
    openCvCameraView.setCvCameraViewListener(this);

    /*handler1.postDelayed(*/
    runnable =
        new Runnable() {
          public void run() {
            try {
              int listSize = facesInASecond.size();
              int trues = Collections.frequency(facesInASecond, true);
              // int falses = Collections.frequency(facesInASecond, false);

              final double faceRatio = ((double) trues / (double) listSize) * (double) 100;

              if (faceRatio > 65) {
                faceDetected = true;
                if (!textToSpeech.isSpeaking())
                  textToSpeech.speak("Please get out the way", TextToSpeech.QUEUE_FLUSH, null);

              } else {
                faceDetected = false;
              }

              runOnUiThread(
                  new Runnable() // start actions in UI thread
                  {

                    @Override
                    public void run() {
                      if (faceDetected)
                        if (!command.equals("STOP")) {
                          Utilities.setDirectionImage("STOP", ivDirection, bt);
                          command = "STOP";
                        }
                      if (!(faceRatio == Double.NaN))
                        tvFaceRatio.setText(
                            "Face ratio: " + String.format("%.02f", faceRatio) + "%");
                      else tvFaceRatio.setText("Face ratio: " + 0 + "%");
                    }
                  });

              Log.d("RATIO", "" + faceRatio);
              Log.d("face state", " " + faceDetected);
              facesInASecond.clear();
            } catch (Exception e) {
              //  Log.e("ERROR", e.printStackTrace() + "");
              e.printStackTrace();
            }
            handler1.postDelayed(this, 1000); // 1 second
          }
        };

    handler1.postDelayed(runnable, 1000);

    /// lathos topothetisi den kerdizw kati apo to thread
    directionThread =
        new Thread(
            new Runnable() {

              @Override
              public void run() {
                while (!Thread.interrupted())
                  try {
                    Thread.sleep(
                        Preferences.loadPrefsInt(
                            "COMMUNICATION_LOOP_REPEAT_TIME", 300, getApplicationContext()));
                    runOnUiThread(
                        new Runnable() // start actions in UI thread
                        {

                          @Override
                          public void run() {
                            if (robotLocation != null
                                && (!route.isEmpty())
                                && running
                                && textToSpeech != null) {

                              if (!faceDetected)
                                command =
                                    Utilities.giveDirection(
                                        compassBearingDegrees,
                                        ivDirection,
                                        ivCompass,
                                        route,
                                        robotLocation,
                                        getApplicationContext(),
                                        command,
                                        mMap,
                                        getResources(),
                                        tvDistance,
                                        textToSpeech,
                                        bt,
                                        false);

                              // END OF PATH REACHED - FINISH PROGRAM
                              if (command.equals("FINISH")) {
                                mMap.clear();
                                tvDistance.setText("---m");
                                if (running)
                                  running =
                                      Utilities.playStopButtonHandler(
                                          route, running, ivPlayStop, getApplicationContext());

                                route.clearRoute();

                                Utilities.setDirectionImage("STOP", ivDirection, bt);
                              }
                            }
                          }
                        });
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.e(TAG, "run: Direction Thread interrupted");
                    break;
                  }
              }
            });

    directionThread.start();
  }