title: Deserializing shape_predictor_68_face_landmarks.dat only for once for facial landmark detection in real time in android studio
body: <p>I am trying to detect facial landmark using opencv ,dlib library in android studio.
  I can capture image and detect landmarks from the image.
  But I am facing problem when I am trying to detect facial landmark in real time.</p>
  
  <p>Because I am deserializing shape_predictor_68_face_landmarks.dat file for each frame.</p>
  
  <p><code>onCameraFrame method of MainActivity.Java</code></p>
  
  <pre><code>@Override
  public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
  {
      matInput =inputFrame.rgba();
  
          NativeClass.LandmarkDetection(matInput.getNativeObjAddr(), matOutput.getNativeObjAddr());
  
      return matOutput;
  }
  </code></pre>
  
  <p><code>NativeClass.Java</code></p>
  
  <pre><code>package com.example.user.drowsinessdetection;
  
  
  public class NativeClass {
      public native static String getMessage();
      public native static void LandmarkDetection(long addrInput,long addrOutput);
  }
  </code></pre>
  
  <p>The cpp file for Landmark detection:</p>
  
  <p><code>com_example_user_drowsinessdetection_NativeClass.cpp</code></p>
  
  <pre><code>#include &lt;com_example_user_drowsinessdetection_NativeClass.h&gt;
  #include &lt;dlib/geometry/rectangle.h&gt;
  
  
  JNIEXPORT jstring JNICALL 
  Java_com_example_user_drowsinessdetection_NativeClass_getMessage
      (JNIEnv *env, jclass){
  env-&gt;NewStringUTF("JNI message");
  }
  
  JNIEXPORT void JNICALL
  Java_com_example_user_drowsinessdetection_NativeClass_LandmarkDetection
      (JNIEnv *env, jclass thiz, jlong addrInput, jlong addrOutput){
      Mat&amp; image = *(Mat*)addrInput;
      Mat&amp; dst = *(Mat*)addrOutput;
  
      faceDetectionDlib(image, dst);
  }
  
  
  
  void faceDetectionDlib(Mat&amp; img, Mat&amp; dst){
  
      try {
          frontal_face_detector detector = get_frontal_face_detector();
          shape_predictor pose_model;
         deserialize("storage/emulated/0/shape_predictor_68_face_landmarks.dat")&gt;&gt;pose_model;
          cv_image&lt;bgr_pixel&gt;cimg(img);
  
          std::vector&lt;dlib::rectangle&gt; faces = detector(cimg);
          std::vector&lt;full_object_detection&gt; shapes;
          int k=faces.size();
  
          for(unsigned long i = 0;i &lt; k; ++i)
              shapes.push_back(pose_model(cimg,faces[i]));
  
          dst = img.clone();
          renderToMat(shapes, dst);
         }
      catch (serialization_error&amp; e)
      {
          cout&lt;&lt;endl&lt;&lt;e.what()&lt;&lt;endl;
      }
  
  }
  
  void renderToMat(std::vector&lt;full_object_detection&gt;&amp; dets, Mat&amp; dst){
  Scalar color;
  int sz = 3,l;
  color = Scalar(0,255,0);
  //chin line
  l=dets.size();
  for(unsigned long idx = 0; idx &lt; l; idx++) {
  
      //left eye
      for (unsigned long i = 37; i &lt;= 41; ++i)
          cv::line(dst, Point(dets[idx].part(i).x(), dets[idx].part(i).y()),
                   Point(dets[idx].part(i - 1).x(), dets[idx].part(i - 1).y()), color, sz);
      cv::line(dst, Point(dets[idx].part(36).x(), dets[idx].part(36).y()),
               Point(dets[idx].part(41).x(), dets[idx].part(41).y()), color, sz);
      //right eye
      for (unsigned long i = 43; i &lt;= 47; ++i)
          cv::line(dst, Point(dets[idx].part(i).x(), dets[idx].part(i).y()),
                   Point(dets[idx].part(i - 1).x(), dets[idx].part(i - 1).y()), color, sz);
      cv::line(dst, Point(dets[idx].part(42).x(), dets[idx].part(42).y()),
               Point(dets[idx].part(47).x(), dets[idx].part(47).y()), color, sz);
      //lips out part
  
  }
  }
  </code></pre>
  
  <p>I don't want execute this line </p>
  
  <p><code>frontal_face_detector detector = get_frontal_face_detector();</code>
  and</p>
  
  <p><code>deserialize("storage/emulated/0/shape_predictor_68_face_landmarks.dat")&gt;&gt;pose_model;</code></p>
  
  <p>for each and every frame.</p>
  
  <p>So How can I do these only once and then use these for all other frames?</p>
  
  <p>I am a newbie.
  Please help.</p>
  
  <p>Sorry for my poor English. :)</p>