Regalamiunsorriso/decompiled-libs/www/acxent-face-1.0.0/it/acxent/face/FotoFace.java

2589 lines
111 KiB
Java
Raw Normal View History

2026-04-22 18:41:37 +02:00
package it.acxent.face;
import it.acxent.common.StatusMsg;
import it.acxent.db.ApplParmFull;
import it.acxent.db.DBAdapter;
import it.acxent.db.ResParm;
import it.acxent.db.WcString;
import it.acxent.face.api.FaceRecognitionApi;
import it.acxent.face.api.FaceRecognitionApiResult;
import it.acxent.face.api.TrainingImage;
import it.acxent.face.api.vision.GoogleVisionApi;
import it.acxent.face.api.vision.GoogleVisionResult;
import it.acxent.face.callable.TrainingSendTask;
import it.acxent.face.pc.SaveScoreProcessor;
import it.acxent.util.DoubleOperator;
import it.acxent.util.FileWr;
import it.acxent.util.ScaleImage;
import it.acxent.util.StringTokenizer;
import it.acxent.util.Timer;
import it.acxent.util.Vectumerator;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.Serializable;
import java.math.RoundingMode;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.imageio.ImageIO;
import org.apache.commons.math3.geometry.Vector;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.json.JSONArray;
import org.json.JSONObject;
public class FotoFace extends DBAdapter implements Serializable, FaceLabel, FaceScoreTarget {
private static final long serialVersionUID = 1683972032841L;
public static boolean threadBuildRecognizer = false;
public static boolean threadBuildRecognizerSTOP = false;
private Face face;
private long id_fotoFace;
private long id_foto;
private String jsonData;
private Foto foto;
private long faceTopVx;
private long faceTopVy;
private long faceBottomVx;
private long faceBottomVy;
private String featureVector1;
private String featureVector2;
private String fileName;
private String pettorale;
private String pettoraleDb;
private long label;
private double confidence;
private long labelCount;
private long labelDb;
private long flgTrainingOk;
private String md5;
private long predictedLabelLBPH;
private FaceLabel labelFace;
private FaceLabel predictedLabelLBPHFace;
private long predictedLabelEigen;
private long predictedLabelFish;
private double confidenceLBPH;
private double confidenceEigen;
private double confidenceFish;
private FaceLabel faceLabel;
private String labelMd5;
private String predictedLabelMd5LBPH;
private long flgLock;
private long flgTrainingLock;
private Timestamp tsTrainingFaceInviata;
@Deprecated
class ThreadBuildRecognizer extends Thread {
private FotoCR CR;
public ThreadBuildRecognizer(FotoCR l_CR) {
this.CR = l_CR.getClone();
if (!FotoFace.isThreadBuildRecognizer()) {
FotoFace.threadBuildRecognizer = true;
FotoFace.threadBuildRecognizerSTOP = false;
start();
}
}
public void run() {
boolean debug = false;
this.CR.setId_faceRecognizer(this.CR.getEvento().getId_faceRecognizer());
this.CR.setFlgWithFaces(1L);
String TAG_THREAD_MSG = "FACE RECOGNIZING " + this.CR.getFaceRecognizer().getCodice();
int NUM_IMMAGINI_A_VOLTA = 10;
Timer timer = new Timer();
long totFotoInviate = 0L;
long totImgTrovate = 0L;
timer.start();
ResParm rp = new ResParm(true);
if (this.CR.getId_faceRecognizer() == 0L) {
rp.setMsg("Recognizer: Bean Face recognizer non caricato");
rp.setStatus(false);
FotoFace.this.handleDebug("Recognizer: Bean Face recognizer non caricato", 2);
} else {
StringBuffer errMsg = new StringBuffer();
int currentPage = 1;
long totPages = 0L;
long l_id_puntoFoto = this.CR.getId_puntoFoto();
long fotoCount = 0L;
long faceCount = 0L;
try {
long currentTrainingImages = this.CR.getFaceRecognizer().getNTrainingImages();
if (currentTrainingImages > 50L) {
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "...cleaning labels ....");
FotoFace.this.removeLabelNByEvento(this.CR.getId_evento(), 1L);
}
Foto foto = new Foto(FotoFace.this.getApFull());
FotoFace ff = new FotoFace(FotoFace.this.getApFull());
FotoFaceCR ffCR = new FotoFaceCR(FotoFace.this.getApFull());
ffCR.setId_evento(this.CR.getId_evento());
ffCR.setId_puntoFoto(this.CR.getId_puntoFoto());
rp = ff.resetPredictedLabelByCR(ffCR);
this.CR.setFlgFotoOrFace(0L);
this.CR.setFlgLabel0(1L);
FotoCR CRRec = new FotoCR(FotoFace.this.getApFull());
CRRec.setId_faceRecognizer(this.CR.getId_faceRecognizer());
CRRec.setFlgTrainingOk(0L);
long totRecord = (long)foto.findByCRTotRecord(this.CR);
totPages = totRecord / (long)NUM_IMMAGINI_A_VOLTA + 1L;
long MAX_LABEL_X_EVENTO = FotoFace.this.getParm("CONFIDENCE_MAX_LABEL_X_EVENTO").getNumeroLong();
long CONFIDENCE_OK_NEAR = FotoFace.this.getParm("CONFIDENCE_OK_NEAR").getNumeroLong();
long CONFIDENCE_OK_LEVEL_1 = FotoFace.this.getParm("CONFIDENCE_OK_LEVEL_1").getNumeroLong();
long CONFIDENCE_OK_LEVEL_2 = FotoFace.this.getParm("CONFIDENCE_OK_LEVEL_2").getNumeroLong();
long CONFIDENCE_NO_LEVEL_1 = FotoFace.this.getParm("CONFIDENCE_NO_LEVEL_1").getNumeroLong();
long FOTO_FACE_SEC_DISTANCE_MAX = FotoFace.this.getParm("CONFIDENCE_FOTO_FACE_SEC_DISTANCE_MAX").getNumeroLong();
long currentConfidenceLevel1 = CONFIDENCE_OK_LEVEL_1;
long currentConfidenceLevel2 = CONFIDENCE_OK_LEVEL_2;
boolean doTraining = true;
boolean recognizerDone = false;
Time timePreviousFoto = null;
boolean isNumFacePreviousFoto1 = false;
boolean isNumFaceCurrentFoto1 = false;
long previuslabel = 0L;
String previusLabelMd5 = "";
Vectumerator<Foto> vecFoto;
while (!FotoFace.threadBuildRecognizerSTOP && rp.getStatus() && (
vecFoto = foto.findByCR(this.CR, 1, NUM_IMMAGINI_A_VOLTA)).hasMoreElements()) {
recognizerDone = false;
while (!FotoFace.threadBuildRecognizerSTOP && vecFoto.hasMoreElements()) {
fotoCount++;
Foto rowFoto = (Foto)vecFoto.nextElement();
if (debug)
System.out.println("ciclo foto " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount);
isNumFaceCurrentFoto1 = (rowFoto.getNumOfFaces() == 1L);
boolean isFotoNear = false;
if (timePreviousFoto != null) {
long distance = Math.abs(DBAdapter.getTimestampDiff(rowFoto.getTempo(), timePreviousFoto));
if (distance <= FOTO_FACE_SEC_DISTANCE_MAX)
isFotoNear = true;
}
if (!isFotoNear || !isNumFaceCurrentFoto1 || !isNumFacePreviousFoto1)
if (doTraining && currentTrainingImages > 0L) {
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "training " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount);
this.CR.setId_puntoFoto(0L);
this.CR.setFlgLabel0(-1L);
rp = FotoFace.this.trainingByCR(this.CR, TAG_THREAD_MSG + " " + TAG_THREAD_MSG, false, false);
currentTrainingImages = this.CR.getFaceRecognizer().getNTrainingImages();
if (debug)
System.out.println("training foto " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount + " tot training images:" + currentTrainingImages);
this.CR.setFlgTrainingOk(-1L);
this.CR.setFlgTrainingInviate(-1L);
this.CR.setId_puntoFoto(l_id_puntoFoto);
this.CR.setFlgLabel0(1L);
} else {
rp.setStatus(true);
}
if (rp.getStatus()) {
if (!isFotoNear || !isNumFaceCurrentFoto1 || !isNumFacePreviousFoto1)
if (currentTrainingImages > 0L) {
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "recognizer " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount);
CRRec.setId_foto(rowFoto.getId_foto());
rp = FotoFace.this.recognizeByCR(CRRec, TAG_THREAD_MSG, false);
} else {
rp.setStatus(true);
}
if (rp.getStatus()) {
Vectumerator<FotoFace> vecFace = ff.findByFoto(rowFoto.getId_foto(), 0L);
if (vecFace.hasMoreElements()) {
doTraining = true;
recognizerDone = true;
} else {
doTraining = false;
}
while (vecFace.hasMoreElements()) {
FotoFace rowFf = (FotoFace)vecFace.nextElement();
faceCount++;
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "check confidence " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount);
if (debug) {
System.out.println("Check confidence foto " + rowFoto.getFileName() + " face: " + rowFf.getMd5() + " label.: " +
rowFf.getPredictedLabelLBPH() + " conf.: " + rowFf.getConfidenceLBPH());
System.out.println("predicted label pettorale: " + rowFf.getLabelFace().getPettorale() + " face pettorale: " +
rowFf.getPettorale());
}
if (rowFf.getFlgTrainingLock() == 0L) {
boolean res = false;
if (isFotoNear && isNumFaceCurrentFoto1 && isNumFacePreviousFoto1) {
res = true;
if (debug)
System.out.print("foto " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " tot training images:" + currentTrainingImages + " face uguale alla precedente perchè vicino e singola faccia. Label:" + previuslabel + " labelMd5: " + previusLabelMd5);
rowFf.setLabel(previuslabel);
rowFf.setLabelMd5(previusLabelMd5);
rowFf.setFlgTrainingOk(1L);
rp = rowFf.save();
currentTrainingImages++;
} else {
boolean isPredictedNear = false;
long distance = FotoFace.this.getSecFotoDistance(rowFf.getId_fotoFace(), rowFf.getPredictedLabelLBPH());
if (distance <= FOTO_FACE_SEC_DISTANCE_MAX)
isPredictedNear = true;
boolean isNear = (isPredictedNear || isFotoNear);
boolean isConfidenceL1 = false;
if (rowFf.getConfidenceLBPH() >= (double)currentConfidenceLevel1)
isConfidenceL1 = true;
boolean isConfidenceL2 = false;
if (rowFf.getConfidenceLBPH() >= (double)currentConfidenceLevel2)
isConfidenceL2 = true;
boolean isPettoraleUguale = false;
if (!rowFf.getLabelFace().getPettorale().isEmpty() && !rowFf.getPettorale().isEmpty())
if (rowFf.getLabelFace().getPettorale().equals(rowFf.getPettorale())) {
isPettoraleUguale = true;
} else {
isPettoraleUguale = false;
}
long numboerOfPredictelabel = rowFf.getNumberOfLabelsByEventoLabel(this.CR.getId_evento(),
rowFf.getPredictedLabelLBPH());
boolean isTooMuchLabel = false;
if (numboerOfPredictelabel > MAX_LABEL_X_EVENTO)
isTooMuchLabel = true;
boolean isConfidenceLow = false;
if (rowFf.getConfidenceLBPH() <= (double)CONFIDENCE_NO_LEVEL_1)
isConfidenceLow = true;
boolean isConfidenceNear = false;
if (rowFf.getConfidenceLBPH() >= (double)CONFIDENCE_OK_NEAR)
isConfidenceNear = true;
if (debug)
System.out.print("foto " + rowFoto.getFileName() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " tot training images:" + currentTrainingImages + " currentl1:" + currentConfidenceLevel1 + " currentl2: " + currentConfidenceLevel2 + " currentLNerar: " + CONFIDENCE_OK_NEAR + " confidence: " +
rowFf.getConfidenceLBPH() + "\nL1 L2 P Ne TM LN NF RES\n" + isConfidenceL1 + " " + isConfidenceL2 + " " + isPettoraleUguale + " " + isNear + " " + isTooMuchLabel + " " + isConfidenceNear + " " + isNumFaceCurrentFoto1 + " --> ");
if (!res && isNear && isNumFaceCurrentFoto1 && rowFf.getPredictedLabelLBPH() > 0L)
res = true;
if (!res && isConfidenceL1 && isConfidenceL2)
if (!isPettoraleUguale && !isNear && isTooMuchLabel) {
res = false;
} else {
res = true;
}
if (!res && isConfidenceL1 && !isConfidenceL2)
if (isPettoraleUguale) {
res = true;
} else if (isNear && !isTooMuchLabel) {
res = true;
}
if (!res && !isConfidenceL1 && !isConfidenceL2)
if (isPettoraleUguale && isNear && !isConfidenceLow) {
res = true;
} else if (isNear && isConfidenceNear && !isTooMuchLabel) {
res = true;
}
if (res) {
rowFf.setLabel(rowFf.getPredictedLabelLBPH());
rowFf.setLabelMd5(rowFf.getPredictedLabelMd5LBPH());
rowFf.setFlgTrainingOk(1L);
rp = rowFf.save();
currentTrainingImages++;
} else {
rp = rowFf.addNewLabelFr(this.CR.getFaceRecognizer().getCodice());
currentTrainingImages++;
}
}
previuslabel = rowFf.getLabel();
previusLabelMd5 = rowFf.getLabelMd5();
} else {
currentTrainingImages++;
}
if (debug)
System.out.println(" ---> " + ((rowFf.getLabel() == rowFf.getPredictedLabelLBPH()) ? (" trovata " +
rowFf.getPredictedLabelLBPH()) : (" NUOVA: " +
rowFf.getLabel())));
}
} else {
System.out.println("RECOGNIZER ERROR " + rowFoto.getFileName() + ": " + rp.getMsg());
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "RECOGNIZER ERROR " +
rowFoto.getFileName() + ": " + rp.getMsg() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount);
}
} else {
System.out.println("TRAINING ERROR " + rowFoto.getFileName() + ": " + rp.getMsg());
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "TRAINING ERROR " + rowFoto.getFileName() + ": " + rp.getMsg());
}
timePreviousFoto = rowFoto.getTempo();
isNumFacePreviousFoto1 = isNumFaceCurrentFoto1;
}
currentPage++;
}
} catch (Exception e) {
FotoFace.this.handleDebug(e);
errMsg.append("\nTrainig fallita <br>" + e.getMessage() + " page " + currentPage + "/" + totPages + " foto n. " + fotoCount + " face n. " + faceCount);
}
if (FotoFace.threadBuildRecognizerSTOP) {
rp.setMsg("Trainig STOP MANUALE!.<br>Numero foto inviate:" + totFotoInviate + " totale immagini trovate : " + totImgTrovate + "<br> - Errori rilevati: " +
errMsg.toString());
} else {
rp.setMsg("Trainig completato.<br>Numero foto inviate:" + totFotoInviate + " totale immagini trovate : " + totImgTrovate + "<br> - Errori rilevati: " +
errMsg.toString());
}
rp.setStatus(true);
}
timer.stop();
System.out.println(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + " " + timer.getDurata());
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "Concluso: DURATA: " + timer.getDurata() + "\nRisultato: " + rp.getMsg());
try {
sleep(4000L);
} catch (Exception e) {}
StatusMsg.deleteMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG);
FotoFace.threadBuildRecognizer = false;
}
}
class ThreadRecognizingOld extends Thread {
private FotoCR CR;
public ThreadRecognizingOld(FotoCR l_CR) {
this.CR = l_CR;
if (!FotoFace.isThreadBuildRecognizer()) {
FotoFace.threadBuildRecognizer = true;
start();
}
}
public void run() {
boolean debug = false;
String TAG_THREAD_MSG = "FACE RECOGNIZING " + this.CR.getFaceRecognizer().getCodice();
int NUM_IMMAGINI_A_VOLTA = 10;
int CONFIDENCE_MIN = 65;
Timer timer = new Timer();
long totFotoInviate = 0L;
long totImgTrovate = 0L;
timer.start();
ResParm rp = new ResParm(true);
if (this.CR.getId_faceRecognizer() == 0L) {
rp.setMsg("Recognizer: Bean Face recognizer non caricato");
rp.setStatus(false);
FotoFace.this.handleDebug("Recognizer: Bean Face recognizer non caricato", 2);
} else {
StringBuffer errMsg = new StringBuffer();
try {
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "...run Trainig ....");
Foto foto = new Foto(FotoFace.this.getApFull());
FotoFace ff = new FotoFace(FotoFace.this.getApFull());
FotoFaceCR ffCR = new FotoFaceCR(FotoFace.this.getApFull());
ffCR.setId_evento(this.CR.getId_evento());
ffCR.setId_puntoFoto(this.CR.getId_puntoFoto());
rp = ff.resetPredictedLabelByCR(ffCR);
this.CR.setPageRow(NUM_IMMAGINI_A_VOLTA);
this.CR.setFlgFotoOrFace(1L);
boolean trainingFound = true;
while (trainingFound) {
rp = FotoFace.this.trainingByCR(this.CR, TAG_THREAD_MSG, false, false);
long totRecord = (long)foto.findByCRTotRecord(this.CR);
int currentPage = 1;
int endPage = currentPage + 2;
this.CR.setFlgTrainingOk(0L);
for (int i = 1; i <= endPage; i++) {
this.CR.setPageNumber(i);
rp = FotoFace.this.recognizeByCR(this.CR, TAG_THREAD_MSG, false);
}
trainingFound = false;
Vectumerator<FotoFace> vecLabels = ff.findLabelsByFotoCR(this.CR);
while (vecLabels.hasMoreElements()) {
FotoFace labelR = (FotoFace)vecLabels.nextElement();
long currentLabel = labelR.getLabel();
this.CR.setFlgTrainingOk(0L);
this.CR.setFlgOrderBy(9L);
for (int j = 1; j <= 2; j++) {
Vectumerator<Foto> vecFf = foto.findByCR(this.CR, j, this.CR.getPageRow());
while (vecFf.hasMoreElements()) {
FotoFace row = ((Foto)vecFf.nextElement()).getFotoFace();
System.out.println("" + currentLabel + " " + currentLabel + " " +
row.getId_fotoFace() + " " + row.getMd5());
if (row.getConfidenceLBPH() > (double)CONFIDENCE_MIN) {
row.setLabel(currentLabel);
row.setFlgTrainingOk(1L);
row.save();
trainingFound = true;
}
}
}
}
if (!trainingFound) {
this.CR.setFlgTrainingOk(0L);
this.CR.setFlgOrderBy(0L);
Vectumerator<Foto> vecFf = foto.findByCR(this.CR, 1, 1);
if (vecFf.hasMoreElements()) {
FotoFace row = ((Foto)vecFf.nextElement()).getFotoFace();
row.setLabel(row.getId_fotoFace());
row.setFlgTrainingOk(1L);
row.save();
trainingFound = true;
}
}
}
} catch (Exception e) {
FotoFace.this.handleDebug(e);
errMsg.append("\nTrainig fallita <br>" + e.getMessage());
}
rp.setMsg("Trainig completato.<br>Numero foto inviate:" + totFotoInviate + " totale immagini trovate : " + totImgTrovate + "<br> - Errori rilevati: " +
errMsg.toString());
rp.setStatus(true);
}
timer.stop();
System.out.println(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + " " + timer.getDurata());
StatusMsg.updateMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG, "Concluso: DURATA: " + timer.getDurata() + "\nRisultato: " + rp.getMsg());
try {
sleep(4000L);
} catch (Exception e) {}
StatusMsg.deleteMsgByTag(FotoFace.this.getApFull(), TAG_THREAD_MSG);
FotoFace.threadBuildRecognizer = false;
}
}
public FotoFace(ApplParmFull newApplParmFull) {
super(newApplParmFull);
}
public FotoFace() {}
public void setId_fotoFace(long newId_fotoFace) {
this.id_fotoFace = newId_fotoFace;
}
public void setId_foto(long newId_foto) {
this.id_foto = newId_foto;
setFoto(null);
}
public void setJsonData(String newJsonData) {
this.jsonData = newJsonData;
}
public final ResParm loadSelfieByGoogleVision(String fileName) {
ResParm rp = new ResParm();
try {
String GVA_KEY = getParm("GOOGLE_API_KEY_VISION").getTesto();
GoogleVisionApi gva = new GoogleVisionApi(GVA_KEY);
GoogleVisionResult resF = gva.annotateFaces(fileName);
if (resF.isOk()) {
rp = loadSelfieByJson((JSONObject)resF.getResult());
} else {
rp.setStatus(false);
rp.setMsg(resF.getMsg());
}
} catch (Exception e) {
rp.setException(e);
rp.setStatus(false);
}
return rp;
}
public final ResParm loadSelfieByJson(JSONObject jo) {
ResParm rp = new ResParm();
StringBuilder msg = new StringBuilder();
JSONArray jaResponses = jo.getJSONArray("responses");
boolean visiTrovati = false;
for (int i = 0; i < jaResponses.length(); i++) {
JSONObject joResponse = jaResponses.getJSONObject(i);
if (joResponse.has("faceAnnotations")) {
visiTrovati = true;
JSONArray jaFaceAnnotations = joResponse.getJSONArray("faceAnnotations");
if (jaFaceAnnotations.length() > 1) {
rp.setStatus(false);
rp.setMsg("Trovati duo o piu visi..");
} else {
JSONObject joFaceAnnotation = jaFaceAnnotations.getJSONObject(0);
JSONObject joFace = new JSONObject();
JSONObject joFdBoundingPoly = joFaceAnnotation.getJSONObject("fdBoundingPoly");
joFace.put("fdBoundingPoly", joFdBoundingPoly);
JSONArray jaLandmarks = joFaceAnnotation.getJSONArray("landmarks");
joFace.put("landmarks", jaLandmarks);
setJsonData(joFace.toString());
rp = _loadFotoFaceFromFaceJson(false);
if (!rp.getStatus())
msg.append("Errore: impossibile caricare fotoface da json per il selfie: " + rp.getMsg());
}
}
}
if (!visiTrovati) {
rp.setStatus(false);
rp.setMsg("Nessun viso trovato!!");
}
return rp;
}
public long getId_fotoFace() {
return this.id_fotoFace;
}
public long getId_foto() {
return this.id_foto;
}
public String getJsonData() {
return (this.jsonData == null) ? "" : this.jsonData.trim();
}
public void setFoto(Foto newFoto) {
this.foto = newFoto;
}
public Foto getFoto() {
this.foto = (Foto)getSecondaryObject(this.foto, Foto.class, getId_foto());
return this.foto;
}
protected ResParm checkDeleteCascade() {
return new ResParm(true);
}
protected void deleteCascade() {
new File(getFullFaceImgPath()).delete();
}
public Vectumerator<FotoFace> findByCR(FotoFaceCR CR, int pageNumber, int pageRows) {
String s_Sql_Find = "select distinct A.* from FOTO_FACE AS A inner join FOTO as B on A.id_foto=B.id_foto";
String s_Sql_Order = " order by B.fileName";
if (CR.getId_evento() > 0L || CR.getId_puntoFoto() > 0L);
if (CR.getFlgTarget() == 1L)
s_Sql_Find = s_Sql_Find + " inner join FACE_SCORE AS FS ON A.id_fotoFace=FS.id_fotoFace";
WcString wc = new WcString();
if (CR.getId_evento() != 0L)
wc.addWc("B.id_evento=" + CR.getId_evento());
if (CR.getId_fotoFaceS() != 0L)
wc.addWc("A.id_fotoFace=" + CR.getId_fotoFaceS());
if (CR.getFlgConPettorali() == 1L)
wc.addWc("A.pettorale is not null");
if (CR.getId_puntoFoto() != 0L)
wc.addWc("B.id_puntoFoto=" + CR.getId_puntoFoto());
if (!CR.getFileName().isEmpty())
wc.addWc("B.fileName like '%" + CR.getFileName() + "%'");
if (CR.getFlgIndicizzate() == 0L) {
wc.addWc("(A.pettorale is null or length(A.pettorale)=0)");
} else if (CR.getFlgIndicizzate() == 1L) {
wc.addWc("(A.pettorale is not null or length(A.pettorale)>0)");
}
if (!CR.getPettorale().isEmpty())
wc.addWc("A.pettorale ='" + CR.getPettorale() + "'");
if (CR.getConfidence() > 0.0D)
wc.addWc("A.confidenceLBPH >=" + CR.getConfidence());
if (CR.getLabel() > 0L)
wc.addWc("A.label =" + CR.getLabel());
if (CR.getLabelCount() > 0L) {
s_Sql_Find = s_Sql_Find + " inner join FOTO_FACE AS FF ON FF.id_fotoFace=A.label";
wc.addWc("(A.labelCount >=" + CR.getLabelCount() + " or FF.labelCount >=" + CR.getLabelCount() + ")");
}
if (CR.getLabelSearch() > 0L)
wc.addWc("A.predictedLabelLBPH =" + CR.getLabelSearch());
if (CR.getFlgTrainingInviate() == 0L) {
wc.addWc("A.tsTrainingFaceInviata is null ");
} else if (CR.getFlgTrainingInviate() == 1L) {
wc.addWc("A.tsTrainingFaceInviata is not null");
}
if (CR.getFlgTrainingOk() == 0L) {
wc.addWc("(A.flgTrainingOk is null or A.flgTrainingOk=0)");
} else if (CR.getFlgTrainingOk() == 1L) {
wc.addWc("A.flgTrainingOk=1");
}
if (CR.getFlgLabelPrincipale() == 0L) {
wc.addWc("A.id_fotoFace<>A.label");
} else if (CR.getFlgLabelPrincipale() == 1L) {
wc.addWc("A.id_fotoFace=A.label");
}
if (CR.getFlgSoloLabels() == 1L)
wc.addWc(" A.id_fotoFace in (select distinct label from FOTO_FACE ff where ff.label is not null and ff.flgTrainingOk =1)");
if (CR.getFlgTarget() == 1L)
wc.addWc("FS.id_fotoFace2>0");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt, pageNumber, pageRows);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
private ResParm _loadFotoFaceFromFaceJson(boolean salva) {
ResParm rp = new ResParm();
if (getJsonData().isEmpty()) {
rp.setStatus(false);
rp.setMsg("Json data vuoto... non sono state trovate facce");
} else {
this.face = new Face();
JSONObject joFace = new JSONObject(getJsonData());
JSONObject joFdBoundingPoly = joFace.getJSONObject("fdBoundingPoly");
JSONArray jaVertices = joFdBoundingPoly.getJSONArray("vertices");
JSONObject joTopLeft = jaVertices.getJSONObject(0);
JSONObject joTopRigth = jaVertices.getJSONObject(1);
JSONObject joBottomRigth = jaVertices.getJSONObject(2);
JSONObject joBottomLeft = jaVertices.getJSONObject(3);
try {
this.face.setB_TOP_LEFT(new Point(
joTopLeft.getLong("x"), joTopLeft.getLong("y")));
} catch (Exception e) {
e.printStackTrace();
}
try {
this.face.setB_TOP_RIGHT(new Point(joTopRigth.getLong("x"),
joTopRigth.getLong("y")));
} catch (Exception e) {
e.printStackTrace();
}
try {
this.face.setB_BOTTOM_RIGHT(new Point(joBottomRigth.getLong("x"),
joBottomRigth.getLong("y")));
} catch (Exception e) {
e.printStackTrace();
}
try {
this.face.setB_BOTTOM_LEFT(new Point(joBottomLeft.getLong("x"),
joBottomLeft.getLong("y")));
} catch (Exception e) {
e.printStackTrace();
}
JSONArray jaLandmarks = joFace.getJSONArray("landmarks");
for (int k = 0; k < jaLandmarks.length(); k++) {
JSONObject joLandmark = jaLandmarks.getJSONObject(k);
_fillFaceLandmark(this.face, joLandmark);
}
if (salva) {
rp = save();
} else {
rp.setStatus(true);
rp.setMsg("Fotoface creato ma non salvato");
}
}
return rp;
}
public ResParm deleteFotoFaceByFoto(long l_id_foto, boolean noLock) {
if (getApFull() == null || l_id_foto == 0L)
return new ResParm(false, "Errore!! Dati connessione db assenti");
ResParm rp = new ResParm(true);
Vectumerator<FotoFace> vec = findByFotoNoLock(l_id_foto, noLock);
while (vec.hasMoreElements()) {
FotoFace row = (FotoFace)vec.nextElement();
rp.append(row.delete());
}
return rp;
}
public void findFirstByFoto(long l_id_foto) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = "";
WcString wc = new WcString();
wc.addWc("A.id_foto=" + l_id_foto);
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
findFirstRecord(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
}
}
private static final void _fillFaceLandmark(Face face, JSONObject joLandmark) {
String type = joLandmark.getString("type");
JSONObject joPosition = joLandmark.getJSONObject("position");
Point currentPoint = new Point(joPosition.getLong("x"),
joPosition.getLong("y"), joPosition.getLong("z"));
if (type.equals("LEFT_EYE")) {
face.setEL_C(currentPoint);
} else if (type.equals("RIGHT_EYE")) {
face.setER_C(currentPoint);
} else if (type.equals("LEFT_OF_LEFT_EYEBROW")) {
face.setEBL_L(currentPoint);
} else if (type.equals("RIGHT_OF_LEFT_EYEBROW")) {
face.setEBL_R(currentPoint);
} else if (type.equals("LEFT_OF_RIGHT_EYEBROW")) {
face.setEBR_L(currentPoint);
} else if (type.equals("RIGHT_OF_RIGHT_EYEBROW")) {
face.setEBR_R(currentPoint);
} else if (type.equals("MIDPOINT_BETWEEN_EYES")) {
face.setE_C(currentPoint);
} else if (type.equals("NOSE_TIP")) {
face.setN_T(currentPoint);
} else if (type.equals("UPPER_LIP")) {
face.setL_T(currentPoint);
} else if (type.equals("LOWER_LIP")) {
face.setL_B(currentPoint);
} else if (type.equals("MOUTH_LEFT")) {
face.setM_L(currentPoint);
} else if (type.equals("MOUTH_RIGHT")) {
face.setM_R(currentPoint);
} else if (type.equals("MOUTH_CENTER")) {
face.setM_C(currentPoint);
} else if (type.equals("NOSE_BOTTOM_RIGHT")) {
face.setN_RB(currentPoint);
} else if (type.equals("NOSE_BOTTOM_LEFT")) {
face.setN_LB(currentPoint);
} else if (type.equals("NOSE_BOTTOM_CENTER")) {
face.setN_CB(currentPoint);
} else if (type.equals("LEFT_EYE_TOP_BOUNDARY")) {
face.setEL_T(currentPoint);
} else if (type.equals("LEFT_EYE_RIGHT_CORNER")) {
face.setEL_R(currentPoint);
} else if (type.equals("LEFT_EYE_BOTTOM_BOUNDARY")) {
face.setEL_B(currentPoint);
} else if (type.equals("LEFT_EYE_LEFT_CORNER")) {
face.setEL_L(currentPoint);
} else if (type.equals("RIGHT_EYE_TOP_BOUNDARY")) {
face.setER_T(currentPoint);
} else if (type.equals("RIGHT_EYE_RIGHT_CORNER")) {
face.setER_R(currentPoint);
} else if (type.equals("RIGHT_EYE_BOTTOM_BOUNDARY")) {
face.setER_B(currentPoint);
} else if (type.equals("RIGHT_EYE_LEFT_CORNER")) {
face.setER_L(currentPoint);
} else if (type.equals("LEFT_EYEBROW_UPPER_MIDPOINT")) {
face.setEBL_M(currentPoint);
} else if (type.equals("RIGHT_EYEBROW_UPPER_MIDPOINT")) {
face.setEBR_M(currentPoint);
} else if (type.equals("LEFT_EAR_TRAGION")) {
face.setEA_L(currentPoint);
} else if (type.equals("RIGHT_EAR_TRAGION")) {
face.setEA_R(currentPoint);
} else if (type.equals("FOREHEAD_GLABELLA")) {
face.setFORH(currentPoint);
} else if (type.equals("CHIN_GNATHION")) {
face.setCH(currentPoint);
} else if (type.equals("CHIN_LEFT_GONION")) {
face.setCH_L(currentPoint);
} else if (type.equals("CHIN_RIGHT_GONION")) {
face.setCH_R(currentPoint);
} else if (type.equals("LEFT_CHEEK_CENTER")) {
face.setCK_L(currentPoint);
} else if (type.equals("RIGHT_CHEEK_CENTER")) {
face.setCK_R(currentPoint);
} else {
System.out.println("ERRORE!!! Face.fillFaceLandmark tipo non trovato: " + type);
}
}
public double getCalcFaceIdx1() {
DoubleOperator dop = new DoubleOperator();
dop.setScale(2, RoundingMode.UNNECESSARY);
dop.add(Point.getDistanceNormF(getFace().getEA_L(), getFace().getM_C(), getFace().getScaleFactor()));
dop.add(Point.getDistanceNormF(getFace().getEA_R(), getFace().getM_C(), getFace().getScaleFactor()));
return dop.getResult();
}
public double getCalcFaceIdx2() {
DoubleOperator dop = new DoubleOperator();
dop.setScale(2, RoundingMode.UNNECESSARY);
dop.add(Point.getDistanceNormF(getFace().getEA_R(), getFace().getN_T(), getFace().getScaleFactor()));
dop.add(Point.getDistanceNormF(getFace().getEA_L(), getFace().getN_T(), getFace().getScaleFactor()));
dop.add(Point.getDistanceNormF(getFace().getM_C(), getFace().getN_T(), getFace().getScaleFactor()));
return dop.getResult();
}
public double getCalcFaceIdx3() {
DoubleOperator dop = new DoubleOperator();
dop.setScale(2, RoundingMode.UNNECESSARY);
dop.add(Point.getDistanceNormF(getFace().getCK_L(), getFace().getCK_R(), getFace().getScaleFactor()));
return dop.getResult();
}
public double getCalcFaceIdx4() {
DoubleOperator dop = new DoubleOperator();
dop.setScale(2, RoundingMode.UNNECESSARY);
dop.add(Point.getDistanceNormF(getFace().getEA_R(), getFace().getN_T(), getFace().getScaleFactor()));
dop.add(Point.getDistanceNormF(getFace().getEA_L(), getFace().getN_T(), getFace().getScaleFactor()));
dop.add(Point.getDistanceNormF(getFace().getM_C(), getFace().getN_T(), getFace().getScaleFactor()));
return dop.getResult();
}
public double[] getFaceIdRange(double faceId, double percRange) {
DoubleOperator scarto = new DoubleOperator(100.0F);
scarto.setScale(2, RoundingMode.UNNECESSARY);
scarto.subtract(percRange);
scarto.multiply(faceId);
scarto.divide(100.0F);
DoubleOperator min = new DoubleOperator(faceId);
min.subtract(scarto);
DoubleOperator max = new DoubleOperator(faceId);
max.add(scarto);
double[] result = { min.getResult(), max.getResult() };
return result;
}
public ResParm save() {
boolean debug = false;
String l_pettoraleDB = getPettoraleDb();
long l_labelDB = getLabelDb();
if (getFlgTrainingOk() != 1L || getLabel() == 0L);
if (getLabelDb() != l_labelDB)
setTsTrainingFaceInviata(null);
long width = Math.abs(getFaceTopVx() - getFaceBottomVx());
long height = Math.abs(getFaceTopVy() - getFaceBottomVy());
if (debug) {
System.out.println("------------------------------------------------------------");
System.out.println("dimensione " + width + " x " + height);
System.out.println("" + getFaceTopVx() + "," + getFaceTopVx() + "\n" + getFaceTopVy() + "," + getFaceBottomVx());
}
if (width != height) {
long diff = Math.abs(width - height);
long diffL = diff / 2L;
long diffR = diff - diffL;
if (width < height) {
if (getFaceTopVx() > getFaceBottomVx()) {
setFaceTopVx(getFaceTopVx() + diffR);
setFaceBottomVx(getFaceBottomVx() - diffL);
} else {
setFaceTopVx(getFaceTopVx() - diffR);
setFaceBottomVx(getFaceBottomVx() + diffL);
}
} else if (getFaceTopVy() > getFaceBottomVy()) {
setFaceTopVx(getFaceTopVy() + diffR);
setFaceBottomVx(getFaceBottomVy() - diffL);
} else {
setFaceTopVx(getFaceTopVy() - diffR);
setFaceBottomVx(getFaceBottomVy() + diffL);
}
if (debug) {
width = Math.abs(getFaceTopVx() - getFaceBottomVx());
height = Math.abs(getFaceTopVy() - getFaceBottomVy());
System.out.println("trasformato con dimensione " + width + " x " + height);
System.out.println("" + getFaceTopVx() + "," + getFaceTopVx() + "\n" + getFaceTopVy() + "," + getFaceBottomVx());
}
}
if (debug)
System.out.println("##########################################");
ResParm rp = super.save();
if (rp.getStatus()) {
createFaceImage(false);
boolean superSave = false;
if (getMd5().isEmpty()) {
setMd5(DBAdapter.getFileMd5(getFullFaceImgPath()));
superSave = true;
}
if (getFlgTrainingOk() == 1L && getLabel() > 0L)
if (getId_fotoFace() == getLabel()) {
setLabelCount(getNumberOfLabelsByLabel(getLabel()));
superSave = true;
} else {
getFaceLabel().setLabelCount(getNumberOfLabelsByLabel(getLabel()));
getFaceLabel().superSave();
}
if (superSave)
superSave();
if (!getPettorale().equals(l_pettoraleDB))
rp.append(getFoto().updatePettoraliByFotoFace(true));
}
return rp;
}
public ResParm superSave() {
return super.save();
}
public FaceLabel getFaceLabel() {
if (this.faceLabel == null && !getLabelMd5().isEmpty() && getApFull() != null) {
this.faceLabel = new FotoFace(getApFull());
this.faceLabel.findByMd5(getLabelMd5());
if (this.faceLabel.getLabel() == 0L) {
this.faceLabel = new FaceRemota(getApFull());
this.faceLabel.findByMd5(getLabelMd5());
}
}
return (this.faceLabel == null) ? new FotoFace(getApFull()) : this.faceLabel;
}
public Face getFace() {
if (this.face == null && !getJsonData().isEmpty())
_loadFotoFaceFromFaceJson(false);
return this.face;
}
public void setFace(Face face) {
this.face = face;
}
public ResParm _createFeatureVectorAll() {
ResParm rp = new ResParm();
List<Double> featureVector = new ArrayList<>();
Double faceHeight = Point.getDistance(getFace().getFORH(), getFace().getM_C());
Double faceWidth = Point.getDistance(getFace().getEA_L(), getFace().getEA_R());
Double eyeDistance = Point.getDistance(getFace().getEL_C(), getFace().getER_C());
Double mouthLen = Point.getDistance(getFace().getM_L(), getFace().getM_R());
Double nooseLen = Point.getDistance(getFace().getE_C(), getFace().getN_CB());
List<Point> points = new ArrayList<>();
points.add(getFace().getEL_C());
points.add(getFace().getER_C());
points.add(getFace().getEA_L());
points.add(getFace().getEA_R());
points.add(getFace().getEBL_M());
points.add(getFace().getEBR_M());
points.add(getFace().getCK_L());
points.add(getFace().getCK_R());
points.add(getFace().getM_C());
points.add(getFace().getN_T());
points.add(getFace().getE_C());
points.add(getFace().getFORH());
List<Double> distances = new ArrayList<>();
for (int i = 0; i < points.size() - 1; i++) {
Point p1 = points.get(i);
for (int j = i + 1; j < points.size(); j++) {
Point p2 = points.get(j);
double distance = Point.getDistance(p1, p2);
distances.add(Double.valueOf(distance));
}
}
double averageDistance = calculateAverageDistance(distances);
featureVector.add(Double.valueOf(averageDistance));
Double eyeMouthDistance = Point.getDistance(getFace().getE_C(), getFace().getM_C());
featureVector.add(Double.valueOf(eyeMouthDistance / eyeDistance));
Double eyeNoseDistance = Point.getDistance(getFace().getE_C(), getFace().getN_T());
featureVector.add(Double.valueOf(eyeDistance / eyeNoseDistance));
Double eyeChinDistance = Point.getDistance(getFace().getE_C(), getFace().getCH());
Double eyeLipCHinDistance = Point.getDistance(getFace().getL_B(), getFace().getCH());
featureVector.add(Double.valueOf(eyeDistance / eyeLipCHinDistance));
Double eyeLipNooseDistance = Point.getDistance(getFace().getL_T(), getFace().getN_T());
featureVector.add(Double.valueOf(eyeDistance / eyeLipNooseDistance));
Double eyeEyebrowDistance = Point.getDistance(getFace().getEBL_M(), getFace().getEBR_M());
featureVector.add(Double.valueOf(eyeDistance / eyeEyebrowDistance));
Point midCheekPoint = new Point((getFace().getCK_L().getX() + getFace().getCK_R().getX()) / 2L, (
getFace().getCK_L().getY() + getFace().getCK_R().getY()) / 2L, (
getFace().getCK_L().getZ() + getFace().getCK_R().getZ()) / 2L);
Double eyeCheekDistance = Point.getDistance(getFace().getE_C(), midCheekPoint);
featureVector.add(Double.valueOf(eyeDistance / eyeCheekDistance));
featureVector.add(eyeDistance);
featureVector.add(eyeEyebrowDistance);
Double cheekDistance = Point.getDistance(getFace().getCK_L(), getFace().getCK_R());
featureVector.add(cheekDistance);
Double mouthCornerDistance = Point.getDistance(getFace().getM_L(), getFace().getM_R());
featureVector.add(mouthCornerDistance);
Double chinDistance = Point.getDistance(getFace().getCH_L(), getFace().getCH_R());
featureVector.add(chinDistance);
featureVector.add(faceWidth);
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getE_C(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEBL_M(), getFace().getEL_C(), getFace().getN_T())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEBR_M(), getFace().getER_C(), getFace().getN_T())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEBL_L(), getFace().getEBL_M(), getFace().getEBL_R())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEBR_L(), getFace().getEBR_M(), getFace().getEBR_R())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEA_R(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEA_L(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(eyeDistance / faceWidth));
Double noseLen = Point.getDistance(getFace().getE_C(), getFace().getN_CB());
featureVector.add(Double.valueOf(noseLen / faceHeight));
featureVector.add(Double.valueOf(mouthLen / faceWidth));
featureVector.add(Double.valueOf(eyeEyebrowDistance / faceWidth));
featureVector.add(Double.valueOf(cheekDistance / faceWidth));
normalizeFeatureVector(featureVector);
StringBuilder sb = new StringBuilder();
for (Iterator<Double> iterator = featureVector.iterator(); iterator.hasNext(); ) {
double element = iterator.next();
sb.append(element);
sb.append(",");
}
setFeatureVector1(sb.toString().substring(1, sb.toString().length() - 1));
return rp;
}
public ResParm _createFeatureVectorTop() {
ResParm rp = new ResParm();
if (getFace() == null) {
rp.setStatus(false);
rp.setMsg(getFoto().getFileName() + ": No json data...");
return rp;
}
List<Double> featureVector = new ArrayList<>();
List<Point> points = new ArrayList<>();
points.add(getFace().getEL_C());
points.add(getFace().getER_C());
points.add(getFace().getEA_L());
points.add(getFace().getEA_R());
points.add(getFace().getCK_L());
points.add(getFace().getCK_R());
points.add(getFace().getM_C());
points.add(getFace().getN_T());
List<Double> distances = new ArrayList<>();
for (int i = 0; i < points.size() - 1; i++) {
Point p1 = points.get(i);
for (int j = i + 1; j < points.size(); j++) {
Point p2 = points.get(j);
double distance = Point.getDistance(p1, p2);
distances.add(Double.valueOf(distance));
}
}
double averageDistance = calculateAverageDistance(distances);
featureVector.add(Double.valueOf(averageDistance));
Double eyeDistance = Point.getDistance(getFace().getEL_C(), getFace().getER_C());
featureVector.add(eyeDistance);
Double eyeMouthDistance = Point.getDistance(getFace().getE_C(), getFace().getM_C());
featureVector.add(Double.valueOf(eyeMouthDistance / eyeDistance));
Double eyeNoseDistance = Point.getDistance(getFace().getE_C(), getFace().getN_T());
featureVector.add(Double.valueOf(eyeDistance / eyeNoseDistance));
Double eyeChinDistance = Point.getDistance(getFace().getE_C(), getFace().getCH());
Double eyeEyebrowDistance = Point.getDistance(getFace().getEBL_M(), getFace().getEBR_M());
Point midCheekPoint = new Point((getFace().getCK_L().getX() + getFace().getCK_R().getX()) / 2L, (
getFace().getCK_L().getY() + getFace().getCK_R().getY()) / 2L, (
getFace().getCK_L().getZ() + getFace().getCK_R().getZ()) / 2L);
Double eyeCheekDistance = Point.getDistance(getFace().getE_C(), midCheekPoint);
Double cheekDistance = Point.getDistance(getFace().getCK_L(), getFace().getCK_R());
featureVector.add(cheekDistance);
Double mouthCornerDistance = Point.getDistance(getFace().getM_L(), getFace().getM_R());
Double chinDistance = Point.getDistance(getFace().getCH_L(), getFace().getCH_R());
featureVector.add(chinDistance);
Double faceWidth = Point.getDistance(getFace().getEA_L(), getFace().getEA_R());
featureVector.add(faceWidth);
Double faceHeight = Point.getDistance(getFace().getFORH(), getFace().getM_C());
featureVector.add(faceHeight);
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEA_R(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEA_L(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(eyeDistance / faceWidth));
Double noseLen = Point.getDistance(getFace().getE_C(), getFace().getN_CB());
featureVector.add(Double.valueOf(noseLen / faceHeight));
Double mouthLen = Point.getDistance(getFace().getM_L(), getFace().getM_R());
featureVector.add(Double.valueOf(cheekDistance / faceWidth));
featureVector.add(Double.valueOf(faceHeight / faceWidth));
normalizeFeatureVector(featureVector);
StringBuilder sb = new StringBuilder();
for (Iterator<Double> iterator = featureVector.iterator(); iterator.hasNext(); ) {
double element = iterator.next();
sb.append(element);
sb.append(",");
}
setFeatureVector1(sb.toString().substring(1, sb.toString().length() - 1));
return rp;
}
public static double calculateAngleDeg(Point p1, Point p2, Point p3) {
Vector3D v1 = new Vector3D((double)p1.getX(), (double)p1.getY(), (double)p1.getZ());
Vector3D v2 = new Vector3D((double)p2.getX(), (double)p2.getY(), (double)p2.getZ());
Vector3D v3 = new Vector3D((double)p3.getX(), (double)p3.getY(), (double)p3.getZ());
Vector3D v12 = v2.subtract((Vector)v1);
Vector3D v23 = v2.subtract((Vector)v3);
double angRad = Vector3D.angle(v12, v23);
return Math.toDegrees(angRad);
}
public static double calculateAverageDistance(List<Double> distances) {
double sum = 0.0D;
for (Iterator<Double> iterator = distances.iterator(); iterator.hasNext(); ) {
double distance = iterator.next();
sum += distance;
}
return sum / (double)distances.size();
}
public static void normalizeFeatureVector(List<Double> featureVector) {
double norm = calculateEuclideanNorm(featureVector);
for (int i = 0; i < featureVector.size(); i++)
featureVector.set(i, Double.valueOf(featureVector.get(i) / norm));
}
private static double calculateEuclideanNorm(List<Double> vector) {
double sumOfSquares = 0.0D;
for (Iterator<Double> iterator = vector.iterator(); iterator.hasNext(); ) {
double value = iterator.next();
sumOfSquares += value * value;
}
return Math.sqrt(sumOfSquares);
}
public static double calculateDotProduct(List<Double> featureVector1, List<Double> featureVector2) {
if (featureVector1.size() != featureVector2.size())
return -1.0D;
double dotProduct = 0.0D;
for (int i = 0; i < featureVector1.size(); i++)
dotProduct += featureVector1.get(i) * featureVector2.get(i);
return dotProduct;
}
public static double calculateEuclideanDistance(List<Double> featureVector1, List<Double> featureVector2) {
if (featureVector1.size() != featureVector2.size()) {
System.out.println("calculateEuclideanDistance: " + featureVector1.size() + " " + featureVector2.size());
return 99999.0D;
}
double sumOfSquares = 0.0D;
for (int i = 0; i < featureVector1.size(); i++) {
double diff = featureVector1.get(i) - featureVector2.get(i);
sumOfSquares += diff * diff;
}
return Math.sqrt(sumOfSquares);
}
public long getFaceTopVx() {
return this.faceTopVx;
}
public void setFaceTopVx(long faceTopVx) {
this.faceTopVx = faceTopVx;
}
public long getFaceTopVy() {
return this.faceTopVy;
}
public void setFaceTopVy(long faceTopVy) {
this.faceTopVy = faceTopVy;
}
public long getFaceBottomVx() {
return this.faceBottomVx;
}
public void setFaceBottomVx(long faceBottomVx) {
this.faceBottomVx = faceBottomVx;
}
public long getFaceBottomVy() {
return this.faceBottomVy;
}
public void setFaceBottomVy(long faceBottomVy) {
this.faceBottomVy = faceBottomVy;
}
public String getFeatureVector1() {
return (this.featureVector1 == null) ? "" : this.featureVector1.trim();
}
public void setFeatureVector1(String featureVector1) {
this.featureVector1 = featureVector1;
}
public String getFeatureVector2() {
return (this.featureVector2 == null) ? "" : this.featureVector2.trim();
}
public void setFeatureVector2(String featureVector2) {
this.featureVector2 = featureVector2;
}
public Vectumerator<FotoFace> findLabelsByFotoCR(FotoCR CR) {
String s_Sql_Find = "select distinct A.label from FOTO_FACE AS A inner join FOTO as B on A.id_foto=B.id_foto";
String s_Sql_Order = " order by A.label";
WcString wc = new WcString();
wc.addWc("A.label>0");
wc.addWc("A.flgTrainingOk=1");
if (CR.getId_evento() > 0L)
wc.addWc("B.id_evento=" + CR.getId_evento());
if (CR.getId_puntoFoto() > 0L)
wc.addWc("B.id_puntoFoto=" + CR.getId_puntoFoto());
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public Vectumerator<FotoFace> findByFotoConPettorale(long l_id_foto) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = " order by faceTopVx";
WcString wc = new WcString();
wc.addWc("A.id_foto=" + l_id_foto);
wc.addWc("(A.pettorale is not null or A.pettorale <>'')");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public List<Double> getFetureVetctor1list() {
if (getFeatureVector1().isEmpty())
return null;
StringTokenizer st = new StringTokenizer(getFeatureVector1(), ",");
List<Double> res = new ArrayList<>();
while (st.hasMoreTokens()) {
String token = st.nextToken();
res.add(Double.valueOf(Double.parseDouble(token)));
}
return res;
}
public ResParm _createFeatureVectorBottom() {
ResParm rp = new ResParm();
if (getFace() == null) {
rp.setStatus(false);
rp.setMsg(getFoto().getFileName() + ": No json data...");
return rp;
}
List<Double> featureVector = new ArrayList<>();
List<Point> points = new ArrayList<>();
points.add(getFace().getEL_C());
points.add(getFace().getER_C());
points.add(getFace().getEA_L());
points.add(getFace().getEA_R());
points.add(getFace().getCK_L());
points.add(getFace().getCK_R());
points.add(getFace().getM_C());
points.add(getFace().getN_T());
List<Double> distances = new ArrayList<>();
for (int i = 0; i < points.size() - 1; i++) {
Point p1 = points.get(i);
for (int j = i + 1; j < points.size(); j++) {
Point p2 = points.get(j);
double distance = Point.getDistance(p1, p2);
distances.add(Double.valueOf(distance));
}
}
double averageDistance = calculateAverageDistance(distances);
featureVector.add(Double.valueOf(averageDistance));
Double eyeDistance = Point.getDistance(getFace().getEL_C(), getFace().getER_C());
featureVector.add(eyeDistance);
Double eyeMouthDistance = Point.getDistance(getFace().getE_C(), getFace().getM_C());
featureVector.add(Double.valueOf(eyeMouthDistance / eyeDistance));
Double eyeNoseDistance = Point.getDistance(getFace().getE_C(), getFace().getN_T());
featureVector.add(Double.valueOf(eyeDistance / eyeNoseDistance));
Double eyeChinDistance = Point.getDistance(getFace().getE_C(), getFace().getCH());
Double eyeEyebrowDistance = Point.getDistance(getFace().getEBL_M(), getFace().getEBR_M());
Point midCheekPoint = new Point((getFace().getCK_L().getX() + getFace().getCK_R().getX()) / 2L, (
getFace().getCK_L().getY() + getFace().getCK_R().getY()) / 2L, (
getFace().getCK_L().getZ() + getFace().getCK_R().getZ()) / 2L);
Double eyeCheekDistance = Point.getDistance(getFace().getE_C(), midCheekPoint);
Double cheekDistance = Point.getDistance(getFace().getCK_L(), getFace().getCK_R());
featureVector.add(cheekDistance);
Double mouthCornerDistance = Point.getDistance(getFace().getM_L(), getFace().getM_R());
Double chinDistance = Point.getDistance(getFace().getCH_L(), getFace().getCH_R());
featureVector.add(chinDistance);
Double faceWidth = Point.getDistance(getFace().getEA_L(), getFace().getEA_R());
featureVector.add(faceWidth);
Double faceHeight = Point.getDistance(getFace().getFORH(), getFace().getM_C());
featureVector.add(faceHeight);
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getN_T(), getFace().getM_C())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEL_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEA_R(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getEA_L(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(calculateAngleDeg(getFace().getER_C(), getFace().getM_C(), getFace().getCH())));
featureVector.add(Double.valueOf(eyeDistance / faceWidth));
Double noseLen = Point.getDistance(getFace().getE_C(), getFace().getN_CB());
featureVector.add(Double.valueOf(noseLen / faceHeight));
Double mouthLen = Point.getDistance(getFace().getM_L(), getFace().getM_R());
featureVector.add(Double.valueOf(cheekDistance / faceWidth));
featureVector.add(Double.valueOf(faceHeight / faceWidth));
normalizeFeatureVector(featureVector);
StringBuilder sb = new StringBuilder();
for (Iterator<Double> iterator = featureVector.iterator(); iterator.hasNext(); ) {
double element = iterator.next();
sb.append(element);
sb.append(",");
}
setFeatureVector1(sb.toString().substring(1, sb.toString().length() - 1));
return rp;
}
public String getFileName() {
if (this.fileName == null || this.fileName.isEmpty())
this
.fileName = getFoto().getFileNameReale() + "_tl-" + getFoto().getFileNameReale() + "-" + getFaceTopVx() + "_br-" + getFaceTopVy() + "-" + getFaceBottomVx() + ".jpg";
return (this.fileName == null) ? "" : this.fileName.trim();
}
public String getFileNameWeb() {
return getFoto().getFileNameReale() + "_tl-" + getFoto().getFileNameReale() + "-" + getFaceTopVx() + "_br-" + getFaceTopVy() + "-" + getFaceBottomVx() + ".jpg";
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getPettorale() {
return (this.pettorale == null) ? "" : this.pettorale.trim();
}
public void setPettorale(String label) {
this.pettorale = label;
}
public ResParm createFaceImage(boolean overwrite) {
ResParm rp = new ResParm();
if (getId_fotoFace() > 0L && hasImageBounds()) {
int faceTopLeftX = 0;
int faceTopLeftY = 0;
int faceBottomRightX = 0;
int faceBottomRightY = 0;
int faceWidth = 0;
int faceHeight = 0;
int faceSizeRecognizer = (int)getFoto().getFaceSize();
try {
File faceImgFile = new File(getFullFaceImgPath());
String faceRicognitionSourceImg = getFoto().getFileNameFaceRecognition();
if (!faceImgFile.exists() || overwrite) {
if (faceImgFile.exists())
faceImgFile.delete();
BufferedImage sourceImage = ImageIO.read(new File(faceRicognitionSourceImg));
int sourceWidth = sourceImage.getWidth();
int sourceHeight = sourceImage.getHeight();
int margin = 10;
faceTopLeftX = (int)getFaceTopVx() - margin;
if (faceTopLeftX < 0)
faceTopLeftX = 0;
faceTopLeftY = (int)getFaceTopVy() - margin;
if (faceTopLeftY < 0)
faceTopLeftY = 0;
faceBottomRightX = (int)getFaceBottomVx() + margin;
faceBottomRightY = (int)getFaceBottomVy() + margin;
faceWidth = faceBottomRightX - faceTopLeftX;
faceHeight = faceBottomRightY - faceTopLeftY;
if (faceTopLeftX + faceWidth > sourceWidth)
faceWidth = sourceWidth - faceTopLeftX;
if (faceTopLeftY + faceHeight > sourceHeight)
faceHeight = sourceHeight - faceTopLeftY;
DoubleOperator percW = new DoubleOperator((double)sourceWidth, 4);
percW.divide((float)faceWidth);
percW.subtract(1);
percW.multiply(100);
DoubleOperator percH = new DoubleOperator((double)sourceHeight, 4);
percH.divide((float)faceHeight);
percH.subtract(1);
percH.multiply(100);
File targetDir = new File(getFoto().getPuntoFoto().getPathCompletoFotoFace());
if (!targetDir.exists())
targetDir.mkdirs();
if (percW.getResult() < 26.0D || percH.getResult() < 26.0D) {
DBAdapter.copyFile(faceRicognitionSourceImg, getFullFaceImgPath());
} else {
BufferedImage faceImage = sourceImage.getSubimage(faceTopLeftX, faceTopLeftY, faceWidth, faceHeight);
if (faceWidth != faceSizeRecognizer) {
Image resultImage = faceImage.getScaledInstance(faceSizeRecognizer, faceSizeRecognizer, 16);
BufferedImage biRescaled = ScaleImage.toBufferedImage(resultImage, 1);
ImageIO.write(biRescaled, "jpg", faceImgFile);
} else {
ImageIO.write(faceImage, "jpg", faceImgFile);
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("createFaceImage: " + faceTopLeftX + " " + faceTopLeftY + " " + faceWidth + " " + faceHeight);
}
}
return rp;
}
public String getFullFaceImgPath() {
return getFoto().getPuntoFoto().getPathCompletoFotoFace() + "/" + getFoto().getPuntoFoto().getPathCompletoFotoFace();
}
@Deprecated
public String getFullFaceImgPathNew() {
return getFoto().getPuntoFoto().getPathCompletoFotoFace() + "/" + getFoto().getPuntoFoto().getPathCompletoFotoFace() + " _" + getFoto().getFileNameReale() + "_" + getId_fotoFace() + "_.jpg";
}
protected void fillFields(ResultSet rst) {
super.fillFields(rst);
setPettoraleDb(getPettorale());
setLabelDb(getLabel());
}
public boolean hasImageBounds() {
if (getFaceTopVx() == 0L || getFaceBottomVx() == 0L || getFaceBottomVy() == 0L || getFaceTopVy() == 0L)
return false;
return true;
}
public String getPettoraleDb() {
return (this.pettoraleDb == null) ? "" : this.pettoraleDb.trim();
}
public void setPettoraleDb(String labelDb) {
this.pettoraleDb = labelDb;
}
protected void initFields() {
super.initFields();
setPettoraleDb(null);
}
public long getLabel() {
if (getFoto().getId_evento() <= 0L || this.label == 0L);
return this.label;
}
public void setLabel(long label) {
if (label == 0L || (label > 0L && this.flgTrainingLock == 0L)) {
this.label = label;
setLabelFace(null);
}
}
public ResParm updateLabel(long l_newLabel, String l_codice) {
ResParm rp = new ResParm();
if (getId_fotoFace() == 0L) {
rp.setStatus(false);
rp.setMsg("Errore!. Foto Face non trovata!");
} else if (l_newLabel == 0L) {
setLabel(0L);
setLabelMd5("");
setTsTrainingFaceInviata(null);
setTsTrainingFaceInviata(null);
setFlgTrainingOk(0L);
setFlgLock(0L);
rp = superSave();
} else {
FaceRecognitionApi fra = new FaceRecognitionApi(getApFull());
FaceRecognitionApiResult frar = fra.__checkLabel(l_newLabel, l_codice);
if (!frar.isOk()) {
rp.setStatus(false);
rp.setMsg("Errore!. Label non trovata: " + frar.getMsg());
} else {
String l_md5 = ((JSONObject)frar.getResult()).getString("labelMd5");
setFlgTrainingLock(0L);
setLabel(l_newLabel);
setLabelMd5(l_md5);
setTsTrainingFaceInviata(null);
setFlgTrainingOk(1L);
setFlgTrainingLock(1L);
rp = superSave();
}
}
return rp;
}
public ResParm addNewLabelFr(String l_recognizer_code) {
ResParm rp = new ResParm();
boolean debug = false;
if (getId_fotoFace() == 0L) {
rp.setStatus(false);
rp.setMsg("Errore!. Foto Face non trovata!");
} else if (getMd5().isEmpty()) {
rp.setStatus(false);
rp.setMsg("Errore!. Md5 non calcolato!");
} else {
FaceRecognitionApi fra = new FaceRecognitionApi(getApFull());
FaceRecognitionApiResult frar = fra.__addLabel(getMd5(), getFullFaceImgPath(), l_recognizer_code);
if (!frar.isOk()) {
rp.setStatus(false);
rp.setMsg("Errore!. Impossibile aggiungere Label: " + frar.getMsg());
} else {
String l_md5 = ((JSONObject)frar.getResult()).getString("labelMd5");
long l_label = ((JSONObject)frar.getResult()).getLong("label");
long l_id = ((JSONObject)frar.getResult()).getLong("id");
setLabel(l_id);
setLabelMd5(l_md5);
setFlgTrainingLock(1L);
setFlgTrainingOk(1L);
setTsTrainingFaceInviata(null);
rp = superSave();
if (!rp.getStatus()) {
System.out.println(String.valueOf(DBAdapter.getNow()) + " ERRORE addNewLabelFr. " + String.valueOf(DBAdapter.getNow()));
} else if (debug) {
JSONObject res = (JSONObject)frar.getResult();
System.out.println(String.valueOf(DBAdapter.getNow()) + " addNewLabelFr. salvato fotofaceid " + String.valueOf(DBAdapter.getNow()) + " label " + getId_fotoFace() + " id " +
getLabel() + "\n" + l_id);
}
}
}
return rp;
}
public boolean isRealFace() {
if (getFaceTopVx() > 0L || getFaceTopVy() > 0L || getFaceBottomVx() > 0L || getFaceBottomVy() > 0L)
return true;
return false;
}
public long getPredictedLabelLBPH() {
return this.predictedLabelLBPH;
}
public void setPredictedLabelLBPH(long labelSearch) {
this.predictedLabelLBPH = labelSearch;
setPredictedLabelLBPHFace(null);
}
public long getFlgTrainingOk() {
return this.flgTrainingOk;
}
public void setFlgTrainingOk(long flgTrainingOk) {
this.flgTrainingOk = flgTrainingOk;
}
public String getMd5() {
return (this.md5 == null) ? "" : this.md5.trim();
}
public void setMd5(String md5) {
this.md5 = md5;
}
public void findByMd5(String l_md5) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = "";
WcString wc = new WcString();
wc.addWc("A.md5='" + l_md5 + "'");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
findFirstRecord(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
}
}
public void findByLabel(String l_label) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = "";
WcString wc = new WcString();
wc.addWc("A.label='" + l_label + "'");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
findFirstRecord(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
}
}
public ResParm recognize(FaceRecognizer fr, boolean saveLabel) {
ResParm rp = new ResParm();
boolean debug = false;
if (getId_fotoFace() > 0L) {
DBAdapter.printDebug(debug, " FotoFace.recognize chiamata _recognize");
List<TrainingImage> imageFilenames = new ArrayList<>();
imageFilenames.add(new TrainingImage(getFullFaceImgPath(), getMd5()));
rp = recognizeImages(fr, imageFilenames, saveLabel);
} else {
rp.setStatus(false);
rp.setMsg("Errore! FotoFace nullo!");
}
return rp;
}
public ResParm recognize(FaceRecognizer fr) {
return recognize(fr, false);
}
public ResParm recognizeImages(FaceRecognizer fr, List<TrainingImage> imageFilenames, boolean saveLabel) {
ResParm rp = new ResParm();
boolean debug = false;
long totImgTrovate = 0L;
StringBuilder errMsg = new StringBuilder();
FaceRecognitionApi frApi = new FaceRecognitionApi(getApFull());
FaceRecognitionApiResult res = frApi.__recognize(fr.getId_faceRecognizer(), imageFilenames, false);
JSONObject jRes = (JSONObject)res.getResult();
DBAdapter.printDebug(debug, " fotoface.recognizeImages prima risposta jres: " + res.getMsg());
if (jRes != null)
DBAdapter.printDebug(debug, " fotoface.recognizeImages jres prima risposta jres: " + jRes.toString(4));
if (jRes != null && jRes.getBoolean("success") == true) {
FotoFace currentFf = new FotoFace(getApFull());
rp = processRecognizingResponses(currentFf, jRes, saveLabel);
totImgTrovate += (Long)rp.getReturnObj();
if (jRes.has("noImg")) {
JSONArray jNoimgA = jRes.getJSONArray("noImg");
imageFilenames = new ArrayList<>();
for (int k = 0; k < jNoimgA.length(); k++) {
JSONObject jNoImgRow = jNoimgA.getJSONObject(k);
currentFf.findByMd5(jNoImgRow.getString("md5"));
if (currentFf.getId_fotoFace() > 0L)
imageFilenames.add(new TrainingImage(currentFf.getFullFaceImgPath(), currentFf.getLabel(), currentFf.getMd5()));
}
res = frApi.__recognize(fr.getId_faceRecognizer(), imageFilenames, true);
jRes = (JSONObject)res.getResult();
DBAdapter.printDebug(debug, " fotoface.recognizeImages chiamata noimg:\n" + res.getMsg());
if (jRes != null && jRes.getBoolean("success") == true) {
rp = processRecognizingResponses(currentFf, jRes, saveLabel);
totImgTrovate += (Long)rp.getReturnObj();
}
}
}
rp.setMsg("Trovate " + totImgTrovate + " immagini. ");
rp.setReturnObj(Long.valueOf(totImgTrovate));
if (errMsg.length() > 0) {
rp.setStatus(false);
rp.appendMsg(errMsg.toString());
} else {
rp.setStatus(true);
}
return rp;
}
public ResParm recognizeImages(FaceRecognizer fr, List<TrainingImage> imageFilenames) {
return recognizeImages(fr, imageFilenames, false);
}
public boolean isPredictOk() {
if (getLabel() > 0L && getLabel() != getPredictedLabelLBPH())
return false;
return true;
}
public boolean isLabelPrincipale() {
if (!getLabelMd5().isEmpty() && getLabelMd5().equals(getMd5()))
return true;
return false;
}
private ResParm processRecognizingResponses(FotoFace currentFf, JSONObject jRes, boolean saveLabel) {
ResParm rp = new ResParm();
StringBuilder errMsg = new StringBuilder();
long totImgTrovate = 0L;
if (jRes.has("responses")) {
JSONArray jResponses = jRes.getJSONArray("responses");
for (int i = 0; i < jResponses.length(); i++) {
JSONObject jResponse = jResponses.getJSONObject(i);
String md5 = jResponse.getString("md5");
currentFf.findByMd5(md5);
if (currentFf.getId_fotoFace() > 0L) {
JSONArray jLabels = jResponse.getJSONArray("labels");
JSONObject jLabelRow = jLabels.getJSONObject(0);
long predictedLabel = jLabelRow.getLong("label");
String predictedLabelMd5 = jLabelRow.getString("labelMd5");
double confidence = jLabelRow.getDouble("confidence");
currentFf.setPredictedLabelLBPH(predictedLabel);
currentFf.setPredictedLabelMd5LBPH(predictedLabelMd5);
currentFf.setConfidenceLBPH(confidence);
if (saveLabel) {
currentFf.setLabel(predictedLabel);
currentFf.setLabelMd5(predictedLabelMd5);
currentFf.setConfidence(confidence);
}
rp = currentFf.save();
if (rp.getStatus()) {
totImgTrovate++;
} else {
errMsg.append(rp.getErrMsg());
errMsg.append("\n");
}
}
}
}
rp.setMsg("Trovate " + totImgTrovate + " immagini. ");
rp.setReturnObj(Long.valueOf(totImgTrovate));
if (errMsg.length() > 0) {
rp.setStatus(false);
rp.appendMsg(errMsg.toString());
} else {
rp.setStatus(true);
}
return rp;
}
public long getPredictedLabelEigen() {
return this.predictedLabelEigen;
}
public void setPredictedLabelEigen(long predictedLabelEigen) {
this.predictedLabelEigen = predictedLabelEigen;
}
public long getPredictedLabelFish() {
return this.predictedLabelFish;
}
public void setPredictedLabelFish(long predictedLabelFish) {
this.predictedLabelFish = predictedLabelFish;
}
public double getConfidenceLBPH() {
return this.confidenceLBPH;
}
public void setConfidenceLBPH(double confidenceLBPH) {
this.confidenceLBPH = confidenceLBPH;
}
public double getConfidenceEigen() {
return this.confidenceEigen;
}
public void setConfidenceEigen(double confidenceEigen) {
this.confidenceEigen = confidenceEigen;
}
public double getConfidenceFish() {
return this.confidenceFish;
}
public void setConfidenceFish(double confidenceFish) {
this.confidenceFish = confidenceFish;
}
public final ResParm startBuildRecognizer(FotoCR l_CR) {
if (!isThreadBuildRecognizer()) {
new ThreadBuildRecognizer(l_CR);
return new ResParm(true, "Thread BuildRecognizer avviato");
}
return new ResParm(false, "ATTENZIONE!! Thread in esecuzione!!!");
}
public final ResParm stopBuildRecognizer() {
if (!isThreadBuildRecognizer())
return new ResParm(false, "ATTENZIONE!! Thread NON in esecuzione!!!");
threadBuildRecognizerSTOP = true;
return new ResParm(true, "Stop Build Recognizer lanciato!!!");
}
public static boolean isThreadBuildRecognizer() {
return threadBuildRecognizer;
}
public ResParm removeLabelNByEvento(long l_id_evento, long nLabels) {
ResParm rp = new ResParm();
FotoFaceCR CR = new FotoFaceCR(getApFull());
CR.setId_evento(l_id_evento);
CR.setFlgLabelPrincipale(1L);
Vectumerator<FotoFace> vec = findByCR(CR, 0, 0);
int nUpdate = 0;
while (vec.hasMoreElements()) {
FotoFace row = (FotoFace)vec.nextElement();
long nLabel = getNumberOfLabelsByEventoLabel(l_id_evento, row.getId_fotoFace());
if (nLabel <= nLabels && row.getFlgTrainingLock() == 0L) {
row.setLabel(0L);
row.setLabelMd5(null);
row.setFlgTrainingOk(0L);
row.save();
nUpdate++;
}
}
System.out.println("label in training rimosse: " + nUpdate);
return rp;
}
public long getNumberOfLabelsByEventoLabel(long l_id_evento, long l_label) {
if (l_label == 0L)
return 0L;
StringBuffer s_Sql_Find = new StringBuffer("select count( A.id_fotoFace) as tot from FOTO_FACE AS A ");
WcString wc = new WcString();
if (l_id_evento > 0L) {
s_Sql_Find.append(" inner join FOTO as B on A.id_foto=B.id_foto ");
wc.addWc("B.id_evento=" + l_id_evento);
}
wc.addWc("A.label=" + l_label);
try {
PreparedStatement stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find));
return (long)getTots(stmt);
} catch (Exception e) {
handleDebug(e);
return 0L;
}
}
public long getNumberOfLabels() {
return getNumberOfLabelsByLabel(getId_fotoFace());
}
public long getNumberOfLabelsByLabel(long l_label) {
return getNumberOfLabelsByEventoLabel(0L, l_label);
}
public static final void updateLabels(ApplParmFull apFull, JSONObject jres) {
FotoFace ff = new FotoFace(apFull);
JSONArray jLabelsS = jres.getJSONArray("labels");
for (int k = 0; k < jLabelsS.length(); k++) {
JSONObject jLabelRow = jLabelsS.getJSONObject(k);
String md5 = jLabelRow.getString("md5");
if (md5.isEmpty())
System.out.println("aaaaaaaaaaaaaaaaaaa");
ff.findByMd5(md5);
if (ff.getId_fotoFace() > 0L) {
long label = jLabelRow.getLong("label");
String labelMd5 = jLabelRow.getString("labelMd5");
if (ff.getLabel() != label || !ff.getLabelMd5().equals(labelMd5)) {
ff.setLabel(label);
ff.setLabelMd5(labelMd5);
ff.save();
}
}
}
}
public ResParm recognizeByCR(FotoCR CR, String TAG_THREAD_MSG, boolean sleepMsg) {
ResParm rp = new ResParm();
boolean debug = false;
int NUM_IMMAGINI_A_VOLTA = 12;
Timer timer = new Timer();
long totFotoInviate = 0L;
long totImgTrovate = 0L;
timer.start();
if (CR.getId_faceRecognizer() == 0L) {
rp.setMsg("Recognizer: Bean Face recognizer non caricato");
rp.setStatus(false);
handleDebug("Recognizer: Bean Face recognizer non caricato", 2);
} else {
StringBuffer errMsg = new StringBuffer();
try {
Foto foto = new Foto(getApFull());
FotoFace ff = new FotoFace(getApFull());
CR.setPageRow(NUM_IMMAGINI_A_VOLTA);
CR.setFlgFotoOrFace(1L);
long totRecord = (long)foto.findByCRTotRecord(CR);
int startPage = CR.getPageNumber();
long endPage = (long)startPage;
if (CR.getPageNumber() == 0) {
startPage = 1;
endPage = totRecord / (long)CR.getPageRow() + 1L;
}
for (int j = startPage; (long)j <= endPage; j++) {
List<TrainingImage> imageFilenames = new ArrayList<>();
Vectumerator<Foto> vec = foto.findByCR(CR, j, CR.getPageRow());
while (vec.hasMoreElements()) {
totFotoInviate++;
Foto row = (Foto)vec.nextElement();
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "...run recognizing ... startPage " + startPage + " endpage: " + endPage + " foto: " +
row.getFileName() + " toto foto inviate: " + totFotoInviate);
imageFilenames.add(new TrainingImage(row.getFotoFace().getFullFaceImgPath(), row.getFotoFace().getLabel(),
row.getFotoFace().getMd5()));
}
rp = ff.recognizeImages(CR.getFaceRecognizer(), imageFilenames);
totImgTrovate += (Long)rp.getReturnObj();
if (rp.getStatus()) {
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "recognizing inviate " + totFotoInviate + " su " + totRecord + " tot immagini trovate: " + totImgTrovate);
} else {
errMsg.append(rp.getMsg());
errMsg.append("\n");
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "inviate " + totFotoInviate + " su " + totRecord + " tot immagini trovate: " + totImgTrovate + ". ERRORE! " +
rp.getMsg());
break;
}
}
} catch (Exception e) {
handleDebug(e);
errMsg.append("\nRecognizing fallita <br>" + e.getMessage());
}
rp.setMsg("Recognizing completato.<br>Numero foto inviate:" + totFotoInviate + " totale immagini trovate : " + totImgTrovate + "<br> - Errori rilevati: " +
errMsg.toString());
rp.setStatus(true);
}
timer.stop();
if (debug)
System.out.println(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + " " + timer.getDurata());
if (sleepMsg) {
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "Concluso: DURATA: " + timer.getDurata() + "\nRisultato: " + rp.getMsg());
try {
Thread.sleep(4000L);
} catch (Exception e) {}
}
StatusMsg.deleteMsgByTag(getApFull(), TAG_THREAD_MSG);
return rp;
}
public Vectumerator<FotoFace> findByFoto(long l_id_foto, long l_flgTrainingOk) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = " order by faceTopVx";
WcString wc = new WcString();
wc.addWc("A.id_foto=" + l_id_foto);
if (l_flgTrainingOk == 0L) {
wc.addWc("(A.flgTrainingOk is null or A.flgTrainingOk=0)");
} else if (l_flgTrainingOk == 0L) {
wc.addWc("A.flgTrainingOk=" + l_flgTrainingOk);
}
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public Vectumerator<FotoFace> findByEvento(long l_id_evento, int pageNumber, int pageRows) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A inner join FOTO AS B ON A.id_foto=B.id_foto";
String s_Sql_Order = " order by B.fileName, A.faceTopVx";
WcString wc = new WcString();
wc.addWc("B.id_evento=" + l_id_evento);
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt, pageNumber, pageRows);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public Vectumerator<FotoFace> findByEventoNoLabel(long l_id_evento, int pageNumber, int pageRows) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A inner join FOTO AS B ON A.id_foto=B.id_foto";
String s_Sql_Order = " ";
WcString wc = new WcString();
wc.addWc("B.id_evento=" + l_id_evento);
wc.addWc("(A.label is null or A.label=0)");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt, pageNumber, pageRows);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public Vectumerator<FotoFace> findByFotoNoLock(long l_id_foto, boolean noLock) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = " order by faceTopVx";
WcString wc = new WcString();
wc.addWc("A.id_foto=" + l_id_foto);
if (noLock)
wc.addWc("(flgLock IS NULL OR flgLock=0)");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public Vectumerator<FotoFace> findByFoto(long l_id_foto) {
return findByFoto(l_id_foto, -1L);
}
public ResParm resetPredictedLabelByCR(FotoFaceCR CR) {
ResParm rp = new ResParm();
String s_Sql_Find = "update FOTO_FACE AS A inner join FOTO as B on A.id_foto=B.id_foto set predictedLabelLBPH=null, predictedLabelEigen=null, predictedLabelFish=null, confidenceLBPH=0, confidenceEigen=0, confidenceFish=0";
WcString wc = new WcString();
if (CR.getId_evento() > 0L)
wc.addWc("B.id_evento=" + CR.getId_evento());
if (CR.getId_puntoFoto() > 0L)
wc.addWc("B.id_puntoFoto=" + CR.getId_puntoFoto());
if (CR.getFlgTrainingOk() == 0L) {
wc.addWc("(A.flgTrainingOk is null or A.flgTrainingOk=0)");
} else if (CR.getFlgTrainingOk() == 1L) {
wc.addWc("A.flgTrainingOk=1");
}
rp = update(s_Sql_Find + s_Sql_Find);
return rp;
}
public FaceLabel getLabelFace() {
if (this.labelFace == null && !getLabelMd5().isEmpty() && getApFull() != null) {
this.labelFace = new FotoFace(getApFull());
this.labelFace.findByMd5(getLabelMd5());
if (this.labelFace.getLabel() == 0L) {
this.labelFace = new FaceRemota(getApFull());
this.labelFace.findByMd5(getLabelMd5());
}
}
return (this.labelFace == null) ? new FotoFace(getApFull()) : this.labelFace;
}
public void setLabelFace(FaceLabel predictedLabelLBPHFace) {
this.labelFace = predictedLabelLBPHFace;
}
public long getLabelCount() {
return this.labelCount;
}
public void setLabelCount(long labelCount) {
this.labelCount = labelCount;
}
public long getSecFotoDistance(long label, long predictedLabel) {
if (predictedLabel == 0L)
return Long.MAX_VALUE;
if (label == predictedLabel)
return 0L;
long res = Long.MAX_VALUE;
FotoFace ffPredictedLabel = new FotoFace(getApFull());
ffPredictedLabel.findByPrimaryKey(predictedLabel);
if (ffPredictedLabel.getId_fotoFace() > 0L) {
FotoFace ffLabel = new FotoFace(getApFull());
ffLabel.findByPrimaryKey(label);
if (ffLabel.getId_fotoFace() > 0L && ffLabel.getFoto().getId_evento() == ffPredictedLabel.getFoto().getId_evento())
res = Math.abs(getTimestampDiff(ffLabel.getFoto().getTempo(), ffPredictedLabel.getFoto().getTempo()));
}
return res;
}
public String getLabelMd5() {
if (getLabel() == 0L)
return "";
return (this.labelMd5 == null) ? "" : this.labelMd5.trim();
}
public void setLabelMd5(String labelMd5) {
this.labelMd5 = labelMd5;
}
public String getPredictedLabelMd5LBPH() {
return (this.predictedLabelMd5LBPH == null) ? "" : this.predictedLabelMd5LBPH;
}
public void setPredictedLabelMd5LBPH(String predictedLabelMd5LBPH) {
this.predictedLabelMd5LBPH = predictedLabelMd5LBPH;
}
public long getFlgLock() {
return this.flgLock;
}
public void setFlgLock(long flgLock) {
this.flgLock = flgLock;
}
public ResParm addFace(long centerX, long centerY) {
ResParm rp = new ResParm(true);
try {
long width = 80L;
long halfW = width / 2L;
String faceRicognitionSourceImg = getFoto().getFileNameFaceRecognition();
BufferedImage sourceImage = ImageIO.read(new File(faceRicognitionSourceImg));
int sourceWidth = sourceImage.getWidth();
DoubleOperator ratio = new DoubleOperator((double)sourceWidth, 2);
ratio.divide(600.0F);
double ratioD = ratio.getResult();
DoubleOperator realX = new DoubleOperator(ratioD, 2);
realX.multiply(centerX);
DoubleOperator realY = new DoubleOperator(ratioD, 2);
realY.multiply(centerY);
setFaceTopVx((long)realX.getResult() - halfW);
setFaceTopVy((long)realY.getResult() - halfW);
setFaceBottomVx((long)realX.getResult() + halfW);
setFaceBottomVy((long)realY.getResult() + halfW);
rp = save();
} catch (Exception e) {
rp.setStatus(false);
rp.setException(e);
}
return rp;
}
public ResParm moveFace(long moveX, long moveY) {
setFaceTopVx(getFaceTopVx() + moveX);
setFaceTopVy(getFaceTopVy() + moveY);
setFaceBottomVx(getFaceBottomVx() + moveX);
setFaceBottomVy(getFaceBottomVy() + moveY);
File faceImg = new File(getFullFaceImgPath());
if (faceImg.exists())
faceImg.delete();
return save();
}
public ResParm resizeFace(long resizeBy) {
setFaceTopVx(getFaceTopVx() - resizeBy);
setFaceTopVy(getFaceTopVy() - resizeBy);
setFaceBottomVx(getFaceBottomVx() + resizeBy);
setFaceBottomVy(getFaceBottomVy() + resizeBy);
File faceImg = new File(getFullFaceImgPath());
if (faceImg.exists())
faceImg.delete();
return save();
}
public long getFaceSize() {
return getFaceBottomVx() - getFaceTopVx();
}
public long getFlgTrainingLock() {
if (getLabel() == 0L || getFlgTrainingOk() == 0L)
return 0L;
return this.flgTrainingLock;
}
public void setFlgTrainingLock(long flgTrainingLock) {
this.flgTrainingLock = flgTrainingLock;
}
public ResParm changeAllLabel(long changeLabel, String l_codice) {
ResParm rp = new ResParm();
if (getId_fotoFace() == 0L || getLabel() == 0L || getLabel() == changeLabel) {
rp.setStatus(false);
rp.setMsg("Errore!. Foto face errata o label errata");
} else {
FaceRecognitionApi fra = new FaceRecognitionApi(getApFull());
FaceRecognitionApiResult frar = fra.__checkLabel(changeLabel, l_codice);
if (!frar.isOk()) {
rp.setStatus(false);
rp.setMsg("Errore!. Nuova label non trovata: " + frar.getMsg());
} else {
String l_md5 = ((JSONObject)frar.getResult()).getString("labelMd5");
String s_Sql_Find = "update FOTO_FACE set label=" + changeLabel + ", labelMd5='" + l_md5 + "' where label=" + getLabel();
rp = update(s_Sql_Find);
}
}
return rp;
}
public Timestamp getTsTrainingFaceInviata() {
return this.tsTrainingFaceInviata;
}
public void setTsTrainingFaceInviata(Timestamp tsTrainingFaceInviata) {
this.tsTrainingFaceInviata = tsTrainingFaceInviata;
}
public long getLabelDb() {
return this.labelDb;
}
public void setLabelDb(long labelDb) {
this.labelDb = labelDb;
}
public ResParm resetTsTraingFace() {
String sql = "update FOTO_FACE set tsTrainingFaceInviata=NULL";
return update(sql);
}
public Vectumerator<FotoFace> findAllNoMd5(int pageNumber, int pageRow) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A";
String s_Sql_Order = "";
WcString wc = new WcString();
wc.addWc("(A.md5='' or A.md5 is null)");
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt, pageNumber, pageRow);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public long getId_selfie() {
return 0L;
}
public String getFacePath() {
return getFullFaceImgPath();
}
public boolean hasFaceLegami() {
FaceScore fs = new FaceScore(getApFull());
return fs.hasFaceLegami(getId_fotoFace());
}
public float getFaceScoring(FotoFace fotoFaceQuery, boolean saveFaceScore) {
boolean debug = false;
FaceScore fs = new FaceScore(getApFull());
float scoring = 0.0F;
fs.findByFaceFace(getId_fotoFace(), fotoFaceQuery.getId_fotoFace());
if (fs.getId_faceScore() > 0L) {
scoring = fs.getScore();
if (debug)
System.out.println("******************************** getFaceScoring: scoring trovato già din facescore: " + scoring);
} else {
String targetDir = getParm("PATHFOTO_FACE").getTesto() + getParm("PATHFOTO_FACE").getTesto();
File targetDirFile = new File(targetDir);
if (!targetDirFile.exists())
targetDirFile.mkdirs();
FaceRecognitionApi frApi = new FaceRecognitionApi(getApFull());
JSONObject jsonData = new JSONObject();
JSONObject jsonTarget = new JSONObject();
jsonTarget.put("path", getFacePath());
jsonTarget.put("md5", getMd5());
String targetType = "face";
jsonTarget.put("type", targetType);
double confDetectLevel = fotoFaceQuery.getFoto().getEvento().getDetectFaceConfidentThresold();
long dysType = getParm("ZOO_YUNET_SCORING_DYS_TYPE").getNumeroLong();
jsonData.put("dis_type", dysType);
jsonData.put("conf_threshold", confDetectLevel);
jsonData.put("target", jsonTarget);
JSONArray jsonQuery = new JSONArray();
JSONObject jsonQueryRow = new JSONObject();
jsonQueryRow.put("path", fotoFaceQuery.getFacePath());
jsonQueryRow.put("md5", fotoFaceQuery.getMd5());
jsonQueryRow.put("type", "face");
jsonQuery.put(jsonQueryRow);
jsonData.put("query", jsonQuery);
if (debug)
System.out.println("******************************** getFaceScoring: jsondata: " + jsonData.toString(4));
String jsonDataFileName = targetDir + "jsonDataScoring_" + targetDir + ".json";
String jsonResFileName = targetDir + "jsonDataScoring_" + targetDir + "_res.json";
jsonData.put("debug", "FotoFace.getFaceScoring");
FileWr fw = new FileWr(jsonDataFileName, false);
try {
fw.writeLine(jsonData.toString(4));
fw.closeFile();
FaceRecognitionApiResult frar = frApi.__scoring(jsonDataFileName, jsonResFileName);
if (frar.isOk()) {
if (debug)
System.out.println("******************************** getFaceScoring: json reponse: " +
frar.getJSONObjectResult().toString(4));
ResParm rp = fs.elaboraScoringResult(frar.getJSONObjectResult(), getFoto().getEvento(), saveFaceScore);
if (rp.getStatus()) {
scoring = (Float)rp.getReturnObj();
if (debug)
System.out.println("******************************** getFaceScoring: nuovo scoring id: " + fs.getId_faceScore() + " score: " + scoring);
} else {
handleDebug(rp.getErrMsg());
}
}
} catch (Exception e) {
if (debug)
System.out.println("******************************** getFaceScoring: ex: " + e.getMessage());
e.printStackTrace();
handleDebug(e);
}
}
SaveScoreProcessor ssProcessor = SaveScoreProcessor.getInstance(getApFull(), 10);
ssProcessor.shutdown();
return scoring;
}
public Vectumerator<FotoFace> findNoScoreByEvento(long l_id_evento, int pageNumber, int pageRows) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A inner join FOTO AS B ON A.id_foto=B.id_foto LEFT JOIN FACE_SCORE as C\tON A.id_fotoFace = C.id_fotoFace OR A.id_fotoFace = C.id_fotoFace2 ";
String s_Sql_Order = "";
WcString wc = new WcString();
wc.addWc("C.id_fotoFace IS NULL AND C.id_fotoFace2 IS NULL");
wc.addWc("B.id_evento=" + l_id_evento);
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt, pageNumber, pageRows);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
@Deprecated
public Vectumerator<FotoFace> findNoScoreByPuntoFoto(long l_id_puntoFoto, int pageNumber, int pageRows) {
String s_Sql_Find = "select A.* from FOTO_FACE AS A inner join FOTO AS B ON A.id_foto=B.id_foto LEFT JOIN FACE_SCORE as C\tON A.id_fotoFace = C.id_fotoFace OR A.id_fotoFace = C.id_fotoFace2 ";
String s_Sql_Order = " order by B.fileName, A.faceTopVx";
WcString wc = new WcString();
wc.addWc("C.id_fotoFace IS NULL AND C.id_fotoFace2 IS NULL");
wc.addWc("B.id_puntoFoto=" + l_id_puntoFoto);
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
return findRows(stmt, pageNumber, pageRows);
} catch (SQLException e) {
removeCPConnection();
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
public ResParm trainingByCR(FotoCR CR, String TAG_THREAD_MSG, boolean buildAnyway, boolean sleepMsg) {
assert false : "Decompilation failed at line #4821 -> offsets [0]";
assert false : "Decompilation failed at line #4822 -> offsets [10]";
assert false : "Decompilation failed at line #4823 -> offsets [13]";
assert false : "Decompilation failed at line #4825 -> offsets [18]";
assert false : "Decompilation failed at line #4826 -> offsets [27]";
assert false : "Decompilation failed at line #4827 -> offsets [30]";
assert false : "Decompilation failed at line #4829 -> offsets [35]";
assert false : "Decompilation failed at line #4830 -> offsets [38]";
assert false : "Decompilation failed at line #4838 -> offsets [42]";
assert false : "Decompilation failed at line #4840 -> offsets [45]";
assert false : "Decompilation failed at line #4841 -> offsets [57]";
assert false : "Decompilation failed at line #4842 -> offsets [65]";
assert false : "Decompilation failed at line #4843 -> offsets [71]";
assert false : "Decompilation failed at line #4846 -> offsets [82]";
assert false : "Decompilation failed at line #4849 -> offsets [91]";
assert false : "Decompilation failed at line #4850 -> offsets [104]";
assert false : "Decompilation failed at line #4851 -> offsets [117]";
assert false : "Decompilation failed at line #4853 -> offsets [123]";
assert false : "Decompilation failed at line #4854 -> offsets [128]";
assert false : "Decompilation failed at line #4857 -> offsets [133]";
assert false : "Decompilation failed at line #4858 -> offsets [144]";
assert false : "Decompilation failed at line #4859 -> offsets [153]";
assert false : "Decompilation failed at line #4861 -> offsets [168]";
assert false : "Decompilation failed at line #4863 -> offsets [173]";
assert false : "Decompilation failed at line #4864 -> offsets [182]";
assert false : "Decompilation failed at line #4865 -> offsets [194]";
assert false : "Decompilation failed at line #4866 -> offsets [206]";
assert false : "Decompilation failed at line #4869 -> offsets [223]";
assert false : "Decompilation failed at line #4870 -> offsets [230]";
assert false : "Decompilation failed at line #4871 -> offsets [241]";
assert false : "Decompilation failed at line #4872 -> offsets [246]";
assert false : "Decompilation failed at line #4880 -> offsets [261]";
assert false : "Decompilation failed at line #4882 -> offsets [264]";
assert false : "Decompilation failed at line #4883 -> offsets [277]";
assert false : "Decompilation failed at line #4885 -> offsets [288]";
assert false : "Decompilation failed at line #4894 -> offsets [299]";
assert false : "Decompilation failed at line #4895 -> offsets [305]";
assert false : "Decompilation failed at line #4896 -> offsets [310]";
assert false : "Decompilation failed at line #4898 -> offsets [317]";
assert false : "Decompilation failed at line #4899 -> offsets [320]";
assert false : "Decompilation failed at line #4905 -> offsets [335]";
assert false : "Decompilation failed at line #4906 -> offsets [338]";
assert false : "Decompilation failed at line #4909 -> offsets [350]";
assert false : "Decompilation failed at line #4910 -> offsets [361]";
assert false : "Decompilation failed at line #4915 -> offsets [380, 1181]";
assert false : "Decompilation failed at line #4916 -> offsets [393]";
assert false : "Decompilation failed at line #4917 -> offsets [399]";
assert false : "Decompilation failed at line #4919 -> offsets [402]";
assert false : "Decompilation failed at line #4920 -> offsets [411]";
assert false : "Decompilation failed at line #4922 -> offsets [432]";
assert false : "Decompilation failed at line #4926 -> offsets [446]";
assert false : "Decompilation failed at line #4927 -> offsets [460]";
assert false : "Decompilation failed at line #4929 -> offsets [466]";
assert false : "Decompilation failed at line #4930 -> offsets [477]";
assert false : "Decompilation failed at line #4935 -> offsets [502]";
assert false : "Decompilation failed at line #4938 -> offsets [512, 545]";
assert false : "Decompilation failed at line #4939 -> offsets [536]";
assert false : "Decompilation failed at line #4945 -> offsets [551]";
assert false : "Decompilation failed at line #4946 -> offsets [564]";
assert false : "Decompilation failed at line #4947 -> offsets [573]";
assert false : "Decompilation failed at line #4949 -> offsets [584]";
assert false : "Decompilation failed at line #4951 -> offsets [587]";
assert false : "Decompilation failed at line #4952 -> offsets [598, 623]";
assert false : "Decompilation failed at line #4954 -> offsets [611]";
assert false : "Decompilation failed at line #4959 -> offsets [626]";
assert false : "Decompilation failed at line #4964 -> offsets [643]";
assert false : "Decompilation failed at line #4966 -> offsets [651]";
assert false : "Decompilation failed at line #4967 -> offsets [661]";
assert false : "Decompilation failed at line #4968 -> offsets [677]";
assert false : "Decompilation failed at line #4970 -> offsets [690]";
assert false : "Decompilation failed at line #4971 -> offsets [693]";
assert false : "Decompilation failed at line #4972 -> offsets [698]";
assert false : "Decompilation failed at line #4976 -> offsets [723]";
assert false : "Decompilation failed at line #4977 -> offsets [734]";
assert false : "Decompilation failed at line #4979 -> offsets [753]";
assert false : "Decompilation failed at line #4984 -> offsets [762]";
assert false : "Decompilation failed at line #4985 -> offsets [773]";
assert false : "Decompilation failed at line #4988 -> offsets [792]";
assert false : "Decompilation failed at line #4989 -> offsets [802]";
assert false : "Decompilation failed at line #4990 -> offsets [811, 886]";
assert false : "Decompilation failed at line #4991 -> offsets [824]";
assert false : "Decompilation failed at line #4993 -> offsets [833]";
assert false : "Decompilation failed at line #4994 -> offsets [846]";
assert false : "Decompilation failed at line #4995 -> offsets [856]";
assert false : "Decompilation failed at line #5004 -> offsets [892]";
assert false : "Decompilation failed at line #5005 -> offsets [909]";
assert false : "Decompilation failed at line #5007 -> offsets [919]";
assert false : "Decompilation failed at line #5008 -> offsets [924, 943]";
assert false : "Decompilation failed at line #5009 -> offsets [935]";
assert false : "Decompilation failed at line #5011 -> offsets [949]";
assert false : "Decompilation failed at line #5013 -> offsets [960]";
assert false : "Decompilation failed at line #5015 -> offsets [979]";
assert false : "Decompilation failed at line #5021 -> offsets [988]";
assert false : "Decompilation failed at line #5022 -> offsets [996]";
assert false : "Decompilation failed at line #5023 -> offsets [1006]";
assert false : "Decompilation failed at line #5024 -> offsets [1022]";
assert false : "Decompilation failed at line #5026 -> offsets [1035]";
assert false : "Decompilation failed at line #5027 -> offsets [1038]";
assert false : "Decompilation failed at line #5028 -> offsets [1043]";
assert false : "Decompilation failed at line #5031 -> offsets [1068]";
assert false : "Decompilation failed at line #5033 -> offsets [1089]";
assert false : "Decompilation failed at line #5034 -> offsets [1092]";
assert false : "Decompilation failed at line #5035 -> offsets [1108]";
assert false : "Decompilation failed at line #5036 -> offsets [1118, 1144]";
assert false : "Decompilation failed at line #5038 -> offsets [1134]";
assert false : "Decompilation failed at line #5041 -> offsets [1147]";
assert false : "Decompilation failed at line #5042 -> offsets [1152, 1175]";
assert false : "Decompilation failed at line #5044 -> offsets [1167]";
assert false : "Decompilation failed at line #5053 -> offsets [1187]";
assert false : "Decompilation failed at line #5054 -> offsets [1196]";
assert false : "Decompilation failed at line #5055 -> offsets [1201]";
assert false : "Decompilation failed at line #5056 -> offsets [1215]";
assert false : "Decompilation failed at line #5057 -> offsets [1222]";
assert false : "Decompilation failed at line #5058 -> offsets [1228]";
assert false : "Decompilation failed at line #5061 -> offsets [1240]";
assert false : "Decompilation failed at line #5064 -> offsets [1249, 1269]";
assert false : "Decompilation failed at line #5065 -> offsets [1255]";
assert false : "Decompilation failed at line #5068 -> offsets [1272]";
assert false : "Decompilation failed at line #5071 -> offsets [1286]";
assert false : "Decompilation failed at line #5074 -> offsets [1294]";
assert false : "Decompilation failed at line #5075 -> offsets [1304]";
assert false : "Decompilation failed at line #5076 -> offsets [1320]";
assert false : "Decompilation failed at line #5078 -> offsets [1333]";
assert false : "Decompilation failed at line #5079 -> offsets [1336]";
assert false : "Decompilation failed at line #5080 -> offsets [1341]";
assert false : "Decompilation failed at line #5083 -> offsets [1366]";
assert false : "Decompilation failed at line #5084 -> offsets [1378]";
assert false : "Decompilation failed at line #5085 -> offsets [1391]";
assert false : "Decompilation failed at line #5087 -> offsets [1402]";
assert false : "Decompilation failed at line #5090 -> offsets [1421]";
assert false : "Decompilation failed at line #5092 -> offsets [1424]";
assert false : "Decompilation failed at line #5093 -> offsets [1434]";
assert false : "Decompilation failed at line #5095 -> offsets [1450]";
assert false : "Decompilation failed at line #5096 -> offsets [1457]";
assert false : "Decompilation failed at line #5097 -> offsets [1462]";
assert false : "Decompilation failed at line #5100 -> offsets [1479]";
assert false : "Decompilation failed at line #5109 -> offsets [1500, 1516]";
assert false : "Decompilation failed at line #5110 -> offsets [1508]";
assert false : "Decompilation failed at line #5111 -> offsets [1519]";
assert false : "Decompilation failed at line #5115 -> offsets [1525]";
assert false : "Decompilation failed at line #5118 -> offsets [1530]";
assert false : "Decompilation failed at line #5119 -> offsets [1535]";
assert false : "Decompilation failed at line #5122 -> offsets [1557]";
assert false : "Decompilation failed at line #5123 -> offsets [1562, 1582]";
assert false : "Decompilation failed at line #5124 -> offsets [1569]";
assert false : "Decompilation failed at line #5126 -> offsets [1585]";
assert false : "Decompilation failed at line #5127 -> offsets [1590]";
assert false : "Decompilation failed at line #5128 -> offsets [1602]";
assert false : "Decompilation failed at line #5129 -> offsets [1608]";
assert false : "Decompilation failed at line #5130 -> offsets [1613]";
assert false : "Decompilation failed at line #5132 -> offsets [1628]";
assert false : "Decompilation failed at line #5134 -> offsets [1630]";
assert false : "Decompilation failed at line #5135 -> offsets [1625]";
assert false : "Decompilation failed at line #5137 -> offsets [1635]";
assert false : "Decompilation failed at line #5140 -> offsets [1643]";
}
public ResParm trainingByCRCallable(FotoCR CR, String TAG_THREAD_MSG, boolean buildAnyway, boolean sleepMsg) {
ResParm rp = new ResParm(true);
boolean debug = true;
int NUM_IMMAGINI_A_VOLTA = 200;
Timer timer = new Timer();
long totFotoInviate = 0L;
timer.start();
boolean sendTrainingImagesDone = false;
int STEP_STATUS_MSG = 20;
if (CR.getEvento().getId_faceRecognizer() == 0L) {
rp.setMsg("Training: Bean Face recognizer non caricato");
rp.setStatus(false);
handleDebug("Training: Bean Face recognizer non caricato", 2);
} else {
StringBuffer errMsg = new StringBuffer();
Foto foto = new Foto(getApFull());
CR.setPageRow(NUM_IMMAGINI_A_VOLTA);
CR.setFlgTrainingInviate(0L);
CR.setFlgTrainingOk(1L);
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD ...calcolo tot record....");
long totRecord = (long)foto.findByCRTotRecord(CR);
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD ...tot record foto face: " + totRecord);
String eta = " ...";
FaceRecognitionApiResult resF = new FaceRecognitionApiResult();
String userFR = getParm("FACE_RECOGNITION_API_USER").getTesto();
String pwdFR = getParm("FACE_RECOGNITION_API_PWD").getTesto();
FaceRecognitionApi frApi = new FaceRecognitionApi(getApFull(), userFR, pwdFR);
if (totRecord == 0L) {
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD nessuna nuova immagine di training da inviare......");
if (debug)
System.out.println(TAG_THREAD_MSG + " nessuna nuova immagine di training da inviare......");
} else {
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<FaceRecognitionApiResult>> futures = new ArrayList<>();
do {
sendTrainingImagesDone = true;
int startPage = CR.getPageNumber();
long endPage = (startPage == 0) ? (totRecord / (long)CR.getPageRow() + 1L) : (totRecord / (long)CR.getPageRow() + 1L);
for (int j = startPage; (long)j <= endPage &&
!Evento.threadBuildModelSTOP; j++) {
List<TrainingImage> imageFilenames = new ArrayList<>();
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD ...run Trainig startpage: " + startPage + " endpage: " + endPage + " ... QUERY PAGINA.... n. record x pagina: " + NUM_IMMAGINI_A_VOLTA + eta);
Vectumerator<Foto> vec = foto.findByCR(CR, j, CR.getPageRow());
while (vec.hasMoreElements() && !Evento.threadBuildModelSTOP) {
totFotoInviate++;
Foto row = (Foto)vec.nextElement();
imageFilenames.add(new TrainingImage(row.getFotoFace().getFullFaceImgPath(), row.getFotoFace().getLabel(),
row.getFotoFace().getMd5()));
row.getFotoFace().setTsTrainingFaceInviata(timer.getTStart());
row.getFotoFace().superSave();
eta = timer.getEta(totRecord, totFotoInviate);
}
if (!imageFilenames.isEmpty()) {
TrainingSendTask task = new TrainingSendTask(frApi, CR.getEvento().getId_faceRecognizer(), imageFilenames);
Future<FaceRecognitionApiResult> future = executorService.submit(task);
futures.add(future);
}
}
for (Future<FaceRecognitionApiResult> future : futures) {
try {
FaceRecognitionApiResult result = future.get();
if (result.isOk()) {
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD Training image inviata correttamente.");
continue;
}
errMsg.append("Trainig Send fallito\n" + result.getMsg());
} catch (Exception e) {
e.printStackTrace();
errMsg.append("Errore durante l'invio delle immagini\n");
}
}
totRecord = (long)foto.findByCRTotRecord(CR);
if (!debug)
continue;
System.out.println(TAG_THREAD_MSG + " fine ciclo.. tot record (se >0 riprende il ciclo): " + TAG_THREAD_MSG);
} while (totRecord > 0L);
executorService.shutdown();
rp.setStatus(true);
rp.setMsg("Inviate " + totFotoInviate + " training images");
}
if (sendTrainingImagesDone || buildAnyway) {
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD esecuzione training in remoto per " +
CR.getEvento().getFaceRecognizer().getCodice() + " ...... ");
resF = frApi.__trainingRun(CR.getEvento().getId_faceRecognizer());
if (resF.isOk()) {
JSONObject jres = (JSONObject)resF.getResult();
long numberTrainingImages = (jres != null && jres.has("numberTrainingImages")) ?
jres.getLong("numberTrainingImages") :
0L;
CR.getEvento().getFaceRecognizer().setNTrainingImages(numberTrainingImages);
CR.getEvento().getFaceRecognizer().setTsStop(DBAdapter.getTimestamp());
CR.getEvento().getFaceRecognizer().save();
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD training remoto eseguito: " + totFotoInviate + " su " + totRecord + " tot trainingImages: " + numberTrainingImages);
} else {
JSONObject jres = (JSONObject)resF.getResult();
errMsg.append("Trainig Run fallito\n" + resF.getMsg());
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD Training Run fallito. Inviate " + totFotoInviate + " su " + totRecord + ". ERRORE! " +
jres.getString("msg"));
}
}
rp.setMsg("Trainig completato.<br>Numero foto processate:" + totFotoInviate + "<br> - Errori rilevati: " + errMsg.toString());
rp.setStatus(true);
}
timer.stop();
if (debug)
System.out.println(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + " " + timer.getDurata());
if (sleepMsg) {
StatusMsg.updateMsgByTag(getApFull(), TAG_THREAD_MSG, "TRAINING E BUILD Concluso: DURATA: " +
timer.getDurata() + "\nRisultato: " + rp.getMsg());
try {
if (debug)
System.out.print(TAG_THREAD_MSG + " sleep......");
Thread.sleep(10000L);
if (debug)
System.out.println(TAG_THREAD_MSG + " done");
} catch (Exception e) {
e.printStackTrace();
}
}
StatusMsg.deleteMsgByTag(getApFull(), TAG_THREAD_MSG);
return rp;
}
public double getConfidence() {
return this.confidence;
}
public void setConfidence(double confidence) {
this.confidence = confidence;
}
public FaceLabel getPredictedLabelLBPHFace() {
return this.predictedLabelLBPHFace;
}
public void setPredictedLabelLBPHFace(FaceLabel predictedLabelLBPHFace) {
this.predictedLabelLBPHFace = predictedLabelLBPHFace;
}
}