package it.unife.spf;

import java.io.IOException;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;        // for opencv3.0.0
import org.opencv.imgcodecs.Imgcodecs;      // for opencv3.0.0
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.core.MatOfByte;


public class FaceRecognition {

  static{

    // System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // Explicit library loading
    System.load("/usr/local/lib/libopencv_java310.so");
  }

  public static String doFaceRec(byte[] img_stream, String res_abs_path) {
    Mat frame = Imgcodecs.imdecode(new MatOfByte(img_stream), Imgcodecs.IMREAD_UNCHANGED);
    // CascadeClassifier faceDetector1 = new CascadeClassifier(res_abs_path+"/haarcascade_profileface.xml");
    // faceDetector1.load(res_abs_path+"/haarcascade_profileface.xml");
    // if (faceDetector1.empty()) {
    //   System.out.println("faceDetector is empty");
    //   return "0";
    // }

    MatOfRect faceDetections = new MatOfRect();
    // faceDetector1.detectMultiScale(frame, faceDetections, 1.3, 3, 0, new Size(), new Size());

    int a = detector(frame,faceDetections,res_abs_path+"/haarcascade_profileface.xml");
        if(a==-1){
            faceDetections.release(); 
            frame.release();
            System.out.println("Error in cascade classifier load");
            return "0";   
        }

    int found = faceDetections.toArray().length;
    faceDetections.release();
    frame.release();

    return ""+found;
  }

  private static int detector(Mat frame,MatOfRect dect,String path){
    
        CascadeClassifier detector = new CascadeClassifier(path);
        // detector.load(path);
        if (detector.empty()){
            return -1;
        }
        detector.detectMultiScale(frame, dect,1.3, 3, 0, new Size(), new Size());
    
        return 0;
    }

}

@@ -820,19 +820,16 @@
 ED);%0A   
- //
  Cascade
@@ -922,27 +922,24 @@
 e.xml%22);%0A   
- //
  faceDetecto
@@ -996,18 +996,198 @@
 %22);%0A    
-//
+CascadeClassifier faceDetector2 = new CascadeClassifier(res_abs_path+%22/haarcascade_frontalface_alt.xml%22);%0A    faceDetector2.load(res_abs_path+%22/haarcascade_frontalface_alt.xml%22);%0A   
  if (fac
@@ -1208,18 +1208,40 @@
 ty()
+ %7C%7C faceDetector2.empty()
 ) %7B%0A   
- //
    S
@@ -1287,19 +1287,16 @@
 y%22);%0A   
- //
    retur
@@ -1305,19 +1305,16 @@
 %220%22;%0A   
- //
  %7D%0A%0A    
@@ -1361,18 +1361,64 @@
 ();%0A    
-//
+MatOfRect faceDetections2 = new MatOfRect();%0A   
  faceDet
@@ -1504,36 +1504,50 @@
 ));%0A
-%0A
     
-int a = detector
+faceDetector2.detectMultiScale
 (frame,
+ 
 face
@@ -1560,85 +1560,71 @@
 ions
-,res_abs_path+%22/haarcascade_profileface.xml%22);%0A        if(a==-1)%7B%0A           
+2, 1.1, 3, 0, new Size(), new Size());%0A%0A    // for (Rect rect :
  fac
@@ -1639,170 +1639,225 @@
 ons.
-release(); %0A  
+toArray()) %7B%0A
     
+//
    
-   frame.release();
+Imgproc.rectangle(frame, new Point(rect.x, rect.y),
 %0A    
+//
        
- System.out.println(%22Error in cascade classifier load%22);
+new Point(rect.x + rect.width, rect.y + rect.height),
 %0A    
+//
        
- return %220%22;   
+new Scalar(110, 220, 0), 4);
 %0A    
-   
+//
  %7D%0A%0A
+%0A
     
-int found =
+// for (Rect rect :
  fac
@@ -1859,32 +1859,33 @@
 : faceDetections
+2
 .toArray().lengt
@@ -1882,422 +1882,484 @@
 ay()
-.length;%0A    faceDetections.release();%0A    frame.release();%0A%0A    return %22%22+found;%0A  %7D%0A%0A  private static int detector(Mat frame,MatOfRect dect,String path)%7B
+) %7B%0A    //   Imgproc.rectangle(frame, new Point(rect.x, rect.y),%0A    //       new Point(rect.x + rect.width, rect.y + rect.height),
 %0A    
-%0A
+//
        
- CascadeClassifier detector = new CascadeClassifier(path
+new Scalar(0, 0, 255), 4);%0A    // %7D%0A%0A    // String filenameOut = a + %22-facerecognition.jpg%22;%0A    // Imgcodecs.imwrite(filenameOut, frame
 );%0A    
-  
-  
 // 
-detector.load(path
+frame.release(
 );%0A
+%0A
     
-    if (detector.empty())%7B%0A            return -1;%0A        %7D%0A        detector.detectMultiScale(frame, dect,1.3, 3, 0, new Size(), new Siz
+int found = faceDetections.toArray().length + faceDetections2.toArray().length;%0A    faceDetections.release();%0A    faceDetections2.releas
 e()
-)
 ;%0A    
-%0A    
+frame.release();%0A%0A
     
@@ -2369,13 +2369,18 @@
 urn 
-0;%0A  
+%22%22+found;%0A
   %7D%0A

Rollback to a previous CascadeClassifier