889 lines
30 KiB
Java
889 lines
30 KiB
Java
package it.acxent.face;
|
|
|
|
import it.acxent.anag.Clifor;
|
|
import it.acxent.db.ApplParm;
|
|
import it.acxent.db.ApplParmFull;
|
|
import it.acxent.db.DBAdapter;
|
|
import it.acxent.db.ResParm;
|
|
import it.acxent.db.WcString;
|
|
import it.acxent.dm.FaceDetectionMethod;
|
|
import it.acxent.face.api.FaceRecognitionApi;
|
|
import it.acxent.face.api.FaceRecognitionApiResult;
|
|
import it.acxent.face.api.vision.GoogleVisionApi;
|
|
import it.acxent.face.api.vision.GoogleVisionResult;
|
|
import it.acxent.util.Vectumerator;
|
|
import java.io.IOException;
|
|
import java.io.Serializable;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Time;
|
|
import java.sql.Timestamp;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
public class FotoVision extends DBAdapter implements Serializable, FotoI {
|
|
private static final long serialVersionUID = 1683972369916L;
|
|
|
|
private long id_foto;
|
|
|
|
private String fileName;
|
|
|
|
private String pettorali;
|
|
|
|
private String fileNameMd5;
|
|
|
|
private String md5;
|
|
|
|
private Timestamp tsInserimento;
|
|
|
|
private long flgFotoSearch;
|
|
|
|
private Evento evento;
|
|
|
|
private long id_evento;
|
|
|
|
private long id_puntoFoto;
|
|
|
|
private PuntoFoto puntoFoto;
|
|
|
|
private Time tempo;
|
|
|
|
private long flgVideo;
|
|
|
|
private String fotoFaceRowid;
|
|
|
|
public static final String DIR_MP4_JPG = DBAdapter.SEPARATOR;
|
|
|
|
public FotoVision(ApplParmFull newApplParmFull) {
|
|
super(newApplParmFull);
|
|
}
|
|
|
|
public FotoVision() {}
|
|
|
|
public void setId_foto(long newId_foto) {
|
|
this.id_foto = newId_foto;
|
|
}
|
|
|
|
public void setFileName(String newFileName) {
|
|
this.fileName = newFileName;
|
|
}
|
|
|
|
public void setPettorali(String newPettorali) {
|
|
this.pettorali = newPettorali;
|
|
}
|
|
|
|
public void setFileNameMd5(String newFileNameMd5) {
|
|
this.fileNameMd5 = newFileNameMd5;
|
|
}
|
|
|
|
public void setMd5(String newMd5) {
|
|
this.md5 = newMd5;
|
|
}
|
|
|
|
public long getId_foto() {
|
|
return this.id_foto;
|
|
}
|
|
|
|
public String getFileName() {
|
|
return (this.fileName == null) ? "" : this.fileName.trim();
|
|
}
|
|
|
|
public String getPettorali() {
|
|
return (this.pettorali == null) ? "" : this.pettorali.trim();
|
|
}
|
|
|
|
public String getFileNameMd5() {
|
|
return (this.fileNameMd5 == null) ? "" : this.fileNameMd5.trim();
|
|
}
|
|
|
|
public String getMd5() {
|
|
return (this.md5 == null) ? "" : this.md5.trim();
|
|
}
|
|
|
|
protected ResParm checkDeleteCascade() {
|
|
return new ResParm(true);
|
|
}
|
|
|
|
protected void deleteCascade() {}
|
|
|
|
public static final ResParm loadFacesByFaceRecognition(FotoI foto) {
|
|
ResParm rp = new ResParm();
|
|
if (foto.getId_foto() == 0L || foto.getApFull() == null) {
|
|
rp.setStatus(false);
|
|
rp.setMsg("Errore! Foto non valida");
|
|
}
|
|
if (foto.hasFotoFaces());
|
|
System.out.println("loadFacesByFaceRecognition: " + foto.getId_foto() + " " + foto.getFileNameFaceRecognition());
|
|
String userFR = foto.getParm("FACE_RECOGNITION_API_USER").getTesto();
|
|
String pwdFR = foto.getParm("FACE_RECOGNITION_API_PWD").getTesto();
|
|
FaceRecognitionApi bean = new FaceRecognitionApi(foto.getApFull(), userFR, pwdFR);
|
|
boolean usePath = foto.getApFull().getParm("FACE_DETECTION_USE_PATH").isTrue();
|
|
FaceRecognitionApiResult resF = bean.__detectFaces(foto.getFileNameFaceRecognition(), foto.getMd5(), 99L, true,
|
|
foto.getDetectFaceConfidentThresold(), usePath);
|
|
if (resF.isOk())
|
|
return loadFacesByFaceRecognitionJson(foto, (JSONObject)resF.getResult());
|
|
rp.setStatus(false);
|
|
rp.setMsg(resF.getMsg());
|
|
return rp;
|
|
}
|
|
|
|
public static final ResParm loadFacesByGoogleVisionJson(FotoI foto, JSONObject jo) {
|
|
ResParm rp = new ResParm();
|
|
if (foto.getId_foto() == 0L || foto.getApFull() == null) {
|
|
rp.setStatus(false);
|
|
rp.setMsg("Errore! Foto non valida ");
|
|
}
|
|
StringBuilder msg = new StringBuilder();
|
|
FotoFace ff = new FotoFace(foto.getApFull());
|
|
ff.deleteFotoFaceByFoto(foto.getId_foto(), true);
|
|
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");
|
|
for (int j = 0; j < jaFaceAnnotations.length(); j++) {
|
|
JSONObject joFaceAnnotation = jaFaceAnnotations.getJSONObject(j);
|
|
ff = new FotoFace(foto.getApFull());
|
|
ff.setId_foto(foto.getId_foto());
|
|
JSONObject joFace = new JSONObject();
|
|
JSONObject joFdBoundingPoly = joFaceAnnotation.getJSONObject("fdBoundingPoly");
|
|
joFace.put("fdBoundingPoly", joFdBoundingPoly);
|
|
JSONArray jaLandmarks = joFaceAnnotation.getJSONArray("landmarks");
|
|
joFace.put("landmarks", jaLandmarks);
|
|
ff.setJsonData(joFace.toString());
|
|
rp = ff.save();
|
|
if (!rp.getStatus())
|
|
msg.append("Errore: impossibile caricare fotoface da json per foto id " + foto.getId_foto() + ": " + rp.getMsg() + "\n");
|
|
}
|
|
}
|
|
}
|
|
if (!visiTrovati) {
|
|
ff = new FotoFace(foto.getApFull());
|
|
ff.setId_foto(foto.getId_foto());
|
|
rp = ff.save();
|
|
}
|
|
if (rp.getStatus())
|
|
if (foto.getMd5().isEmpty()) {
|
|
foto.setMd5(DBAdapter.getFileMd5(foto.getFileNameFaceRecognition()));
|
|
rp.append(foto.save());
|
|
}
|
|
if (msg.length() > 0) {
|
|
rp.setStatus(false);
|
|
rp.setMsg(msg.toString());
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
String hostname = "localhost:3308";
|
|
String db = "fotoeventi4";
|
|
System.out.println("Db: " + db);
|
|
ApplParmFull apTarget = new ApplParmFull(new ApplParm(17, "//" + hostname + "/" + db, db, "root", "root", 1, 10, 300));
|
|
apTarget.setDebug(false);
|
|
FotoVision fotoVision = new FotoVision(apTarget);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public String getFileNameFaceRecognition() {
|
|
return getFileNameFullPath();
|
|
}
|
|
|
|
public String getFileNameFullPath() {
|
|
if (getFlgFotoSearch() == 1L)
|
|
return
|
|
DBAdapter.convertPathToCurrentFileSystemSeparator(getDocBase() + "_selfie/" + getDocBase() + DBAdapter.SEPARATOR);
|
|
return getPuntoFoto().getPathCompletoFoto() + getPuntoFoto().getPathCompletoFoto() + DBAdapter.SEPARATOR;
|
|
}
|
|
|
|
public ResParm _ricalcolaFotoFaces() {
|
|
ResParm rp = new ResParm();
|
|
StringBuilder sb = new StringBuilder();
|
|
FotoFace ff = new FotoFace(getApFull());
|
|
Vectumerator<FotoFace> vec = ff.findByFoto(getId_foto());
|
|
while (vec.hasMoreElements()) {
|
|
FotoFace row = (FotoFace)vec.nextElement();
|
|
rp = row.save();
|
|
if (!rp.getStatus()) {
|
|
sb.append(getFileName() + " " + getFileName());
|
|
sb.append("<br>");
|
|
}
|
|
}
|
|
rp.setMsg(sb.toString());
|
|
return rp;
|
|
}
|
|
|
|
public boolean hasFotoFaces() {
|
|
FotoFace ff = new FotoFace(getApFull());
|
|
Vectumerator<FotoFace> vec = ff.findByFoto(getId_foto());
|
|
return vec.hasMoreElements();
|
|
}
|
|
|
|
public Timestamp getTsInserimento() {
|
|
return this.tsInserimento;
|
|
}
|
|
|
|
public void setTsInserimento(Timestamp tsInserimento) {
|
|
this.tsInserimento = tsInserimento;
|
|
}
|
|
|
|
public long getFlgFotoSearch() {
|
|
return this.flgFotoSearch;
|
|
}
|
|
|
|
public void setFlgFotoSearch(long flgFotoSearch) {
|
|
this.flgFotoSearch = flgFotoSearch;
|
|
}
|
|
|
|
public void findByMd5(String l_md5) {
|
|
String s_Sql_Find = "select A.* from FOTO 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 FotoFace getFotoFaceFirst() {
|
|
if (getId_foto() > 0L && getApFull() != null) {
|
|
FotoFace ff = new FotoFace(getApFull());
|
|
ff.findFirstByFoto(getId_foto());
|
|
return ff;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Vectumerator<FotoFace> getFotoFaces() {
|
|
if (getId_foto() > 0L && getApFull() != null) {
|
|
FotoFace ff = new FotoFace(getApFull());
|
|
return ff.findByFoto(getId_foto());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ResParm elaboraFaceRecog(Vectumerator<FotoVision> vecFoto) {
|
|
ResParm rp = new ResParm();
|
|
if (getId_foto() > 0L) {
|
|
FotoFace ffSelfie = getFotoFaceFirst();
|
|
List<Double> fvSelfie = ffSelfie.getFetureVetctor1list();
|
|
int i = 0;
|
|
if (fvSelfie != null)
|
|
while (vecFoto.hasMoreElements()) {
|
|
FotoVision row = (FotoVision)vecFoto.nextElement();
|
|
if (row.getId_foto() == 5192210L)
|
|
println(row.getId_foto());
|
|
Vectumerator<FotoFace> vecFf = row.getFotoFaces();
|
|
if (vecFf != null)
|
|
while (vecFf.hasMoreElements()) {
|
|
i++;
|
|
FotoFace rowFf = (FotoFace)vecFf.nextElement();
|
|
if (rowFf.getFeatureVector1().isEmpty())
|
|
rowFf.save();
|
|
if (!rowFf.getFeatureVector1().isEmpty()) {
|
|
FaceRecog fr = new FaceRecog(getApFull());
|
|
fr.findByFotoFace1FotoFace2(ffSelfie.getId_fotoFace(), rowFf.getId_fotoFace());
|
|
fr.setId_fotoFace1(ffSelfie.getId_fotoFace());
|
|
fr.setId_fotoFace2(rowFf.getId_fotoFace());
|
|
List<Double> fvFF = rowFf.getFetureVetctor1list();
|
|
fr.setEDistance(FotoFace.calculateEuclideanDistance(fvSelfie, fvFF));
|
|
fr.setCosineSim(FotoFace.calculateDotProduct(fvSelfie, fvFF));
|
|
System.out.println("" + i + " ed: " + i + " cd: " + fr.getEDistance());
|
|
if (i == 129)
|
|
System.out.println("" + i + " stop!");
|
|
rp = fr.save();
|
|
continue;
|
|
}
|
|
System.out.println("" + i + " fotoface " + i + " foto " + rowFf.getId_fotoFace() + " nome: " + rowFf.getId_foto() + " senza fv");
|
|
}
|
|
}
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public Evento getEvento() {
|
|
this.evento = (Evento)getSecondaryObject(this.evento, Clifor.class, getId_evento());
|
|
return this.evento;
|
|
}
|
|
|
|
public Vectumerator<FotoFace> findFotoFace() {
|
|
FotoFace ff = new FotoFace(getApFull());
|
|
return ff.findByFoto(getId_foto());
|
|
}
|
|
|
|
public void setEvento(Evento evento) {
|
|
this.evento = evento;
|
|
}
|
|
|
|
public long getId_evento() {
|
|
return this.id_evento;
|
|
}
|
|
|
|
public void setId_evento(long id_evento) {
|
|
this.id_evento = id_evento;
|
|
setEvento(null);
|
|
}
|
|
|
|
public long getNextId_foto(FotoCR CR) {
|
|
long l_id_foto = getId_foto();
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = " order by A.fileName ";
|
|
WcString wc = new WcString();
|
|
if (CR.getId_evento() != 0L)
|
|
wc.addWc("A.id_evento=" + CR.getId_evento());
|
|
if (CR.getId_puntoFoto() != 0L)
|
|
wc.addWc("A.id_puntoFoto=" + CR.getId_puntoFoto());
|
|
wc.addWc("A.fileName >'" + getFileName() + "'");
|
|
if (!CR.getFileName().isEmpty())
|
|
wc.addWc("A.fileName like '%" + CR.getFileName() + "%'");
|
|
if (!CR.getPettorale().isEmpty())
|
|
wc.addWc("A.pettorali like '%," + CR.getPettorale() + ",%'");
|
|
if (CR.getFlgIndicizzate() == 0L) {
|
|
wc.addWc("(A.pettorali is null or length(A.pettorali)=0)");
|
|
} else if (CR.getFlgIndicizzate() == 1L) {
|
|
wc.addWc("(A.pettorali is not null or length(A.pettorali)>0)");
|
|
}
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
Vectumerator<FotoVision> vec = findRows(stmt, 1, 1);
|
|
if (vec.hasMoreElements()) {
|
|
FotoVision row = (FotoVision)vec.nextElement();
|
|
l_id_foto = row.getId_foto();
|
|
}
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
}
|
|
return l_id_foto;
|
|
}
|
|
|
|
public long getPrevId_foto(FotoCR CR) {
|
|
long l_id_foto = getId_foto();
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = " order by A.fileName desc";
|
|
WcString wc = new WcString();
|
|
if (CR.getId_evento() != 0L)
|
|
wc.addWc("A.id_evento=" + CR.getId_evento());
|
|
if (CR.getId_puntoFoto() != 0L)
|
|
wc.addWc("A.id_puntoFoto=" + CR.getId_puntoFoto());
|
|
wc.addWc("A.fileName <'" + getFileName() + "'");
|
|
if (!CR.getFileName().isEmpty())
|
|
wc.addWc("A.fileName like '%" + CR.getFileName() + "%'");
|
|
if (!CR.getPettorale().isEmpty())
|
|
wc.addWc("A.pettorali like '%," + CR.getPettorale() + ",%'");
|
|
if (CR.getFlgIndicizzate() == 0L) {
|
|
wc.addWc("(A.pettorali is null or length(A.pettorali)=0)");
|
|
} else if (CR.getFlgIndicizzate() == 1L) {
|
|
wc.addWc("(A.pettorali is not null or length(A.pettorali)>0)");
|
|
}
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
Vectumerator<FotoVision> vec = findRows(stmt, 1, 1);
|
|
if (vec.hasMoreElements()) {
|
|
FotoVision row = (FotoVision)vec.nextElement();
|
|
l_id_foto = row.getId_foto();
|
|
}
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
}
|
|
return l_id_foto;
|
|
}
|
|
|
|
public boolean hasPuntoFotoFoto(long l_id_puntofoto) {
|
|
StringBuffer s_Sql_Find = new StringBuffer("select A.* from FOTO AS A");
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.id_puntoFoto=" + l_id_puntofoto);
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find));
|
|
Vectumerator vec = findRows(stmt, 1, 1);
|
|
if (vec.getTotNumberOfRecords() > 0)
|
|
return true;
|
|
return false;
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public long getId_puntoFoto() {
|
|
return this.id_puntoFoto;
|
|
}
|
|
|
|
public PuntoFoto getPuntoFoto() {
|
|
this.puntoFoto = (PuntoFoto)getSecondaryObject(this.puntoFoto, PuntoFoto.class, getId_puntoFoto());
|
|
return this.puntoFoto;
|
|
}
|
|
|
|
public void setId_puntoFoto(long id_puntoFoto) {
|
|
this.id_puntoFoto = id_puntoFoto;
|
|
setPuntoFoto(null);
|
|
}
|
|
|
|
public void setPuntoFoto(PuntoFoto newPuntoFoto) {
|
|
this.puntoFoto = newPuntoFoto;
|
|
}
|
|
|
|
public ResParm deleteFotoByPuntoFoto(long l_id_puntoFoto) {
|
|
return delete("delete from FOTO where id_puntoFoto=" + l_id_puntoFoto);
|
|
}
|
|
|
|
public void findByFilenamePuntoFoto(String l_filename, long l_id_puntoFoto) {
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.filename='" + l_filename + "'");
|
|
wc.addWc("A.id_puntoFoto=" + l_id_puntoFoto);
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
findFirstRecord(stmt);
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
}
|
|
}
|
|
|
|
public void findByFilenameEvento(String l_filename, long l_id_evento) {
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.filename='" + l_filename + "'");
|
|
wc.addWc("A.id_evento=" + l_id_evento);
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
findFirstRecord(stmt);
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
}
|
|
}
|
|
|
|
public static final synchronized ResParm syncAggiornaPettorale_2(FotoVision bean, String l_pettorali) {
|
|
String temp = l_pettorali.trim();
|
|
if (!temp.isEmpty()) {
|
|
if (!temp.startsWith(","))
|
|
temp = "," + temp;
|
|
if (!temp.endsWith(","))
|
|
temp = temp + ",";
|
|
while (temp.indexOf(",,") >= 0)
|
|
temp = temp.replaceAll(",,", ",");
|
|
}
|
|
String s_sql = "update FOTO SET pettorali='" + temp + "' where id_foto=" + bean.getId_foto();
|
|
ResParm rp = bean.update(s_sql);
|
|
if (rp.getStatus())
|
|
bean.setPettorali(temp);
|
|
return rp;
|
|
}
|
|
|
|
public Time getTempo() {
|
|
return this.tempo;
|
|
}
|
|
|
|
public void setTempo(Time newTempo) {
|
|
this.tempo = newTempo;
|
|
}
|
|
|
|
public long getFlgVideo() {
|
|
return this.flgVideo;
|
|
}
|
|
|
|
public void setFlgVideo(long flgVideo) {
|
|
this.flgVideo = flgVideo;
|
|
}
|
|
|
|
public Vectumerator findByPuntofoto(long l_id_puntofoto) {
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = " ";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.id_puntofoto=" + l_id_puntofoto);
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
return findRows(stmt);
|
|
} catch (SQLException e) {
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
public boolean isPuntoFotoInviata(long l_id_puntofoto, boolean inviata) {
|
|
StringBuffer s_Sql_Find = new StringBuffer("select A.* from FOTO AS A");
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.id_puntoFoto=" + l_id_puntofoto);
|
|
if (inviata) {
|
|
wc.addWc("(A.flgFotoInviata is null or A.flgFotoInviata=0)");
|
|
} else {
|
|
wc.addWc("A.flgFotoInviata =1");
|
|
}
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find));
|
|
Vectumerator vec = findRows(stmt, 1, 1);
|
|
if (vec.getTotNumberOfRecords() > 0)
|
|
return false;
|
|
return true;
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public Vectumerator<FaceRecog> findFaceRecogBySelfie(FotoI selfie) {
|
|
if (getId_foto() > 0L && selfie != null && selfie.getId_foto() > 0L) {
|
|
FaceRecog fr = new FaceRecog(getApFull());
|
|
return fr.findByFoto1Foto2(selfie.getId_foto(), getId_foto());
|
|
}
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
|
|
public String getFileNameReale() {
|
|
if (getFileNameMd5().isEmpty())
|
|
return getFileName();
|
|
return getFileNameMd5();
|
|
}
|
|
|
|
public Vectumerator<FotoVision> findByCR(FotoCR CR, int pageNumber, int pageRows) {
|
|
if (CR.getFotoSelfie() != null)
|
|
return findByCRFotoSelfie(CR, pageNumber, pageRows);
|
|
boolean flgOttimizzo = true;
|
|
if (pageNumber == 0 && pageRows == 0)
|
|
flgOttimizzo = false;
|
|
StringBuffer s_Sql_Find = new StringBuffer("select A.* from FOTO AS A");
|
|
StringBuffer s_Sql_Order = new StringBuffer(" order by A.fileName ");
|
|
WcString wc = new WcString();
|
|
findByCRCreateWC(CR, s_Sql_Find, wc);
|
|
try {
|
|
PreparedStatement stmt;
|
|
if ((pageNumber == 0 && pageRows == 0) || !flgOttimizzo) {
|
|
stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find) + wc.toString());
|
|
} else {
|
|
if (pageNumber == 0)
|
|
pageNumber = 1;
|
|
int start = (pageNumber - 1) * pageRows;
|
|
int stop = start + pageRows;
|
|
stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find) + wc.toString() + " limit " + String.valueOf(s_Sql_Order) + "," + start);
|
|
}
|
|
Vectumerator<FotoVision> vec = findRows(stmt, 1, pageRows);
|
|
vec.setPageNumber(pageNumber);
|
|
vec.setTotNumberOfRecords(findByCRTotRecord(CR));
|
|
return vec;
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
protected void findByCRCreateWC(FotoCR CR, StringBuffer s_Sql_Find, WcString wc) {
|
|
if (CR.getId_evento() != 0L)
|
|
wc.addWc("A.id_evento=" + CR.getId_evento());
|
|
if (CR.getId_fotoS() != 0L)
|
|
wc.addWc("A.id_foto=" + CR.getId_fotoS());
|
|
if (CR.getFlgConPettorali() == 1L)
|
|
wc.addWc("A.pettorali is not null");
|
|
if (CR.getId_puntoFoto() != 0L)
|
|
wc.addWc("A.id_puntoFoto=" + CR.getId_puntoFoto());
|
|
if (!CR.getFileName().isEmpty())
|
|
wc.addWc("A.fileName like '%" + CR.getFileName() + "%'");
|
|
if (CR.getFlgIndicizzate() == 0L) {
|
|
wc.addWc("(A.pettorali is null or length(A.pettorali)=0)");
|
|
} else if (CR.getFlgIndicizzate() == 1L) {
|
|
wc.addWc("(A.pettorali is not null or length(A.pettorali)>0)");
|
|
}
|
|
if (CR.getFlgVideo() == 0L) {
|
|
wc.addWc("(A.flgVideo is null or A.flgVideo=0)");
|
|
} else if (CR.getFlgVideo() == 1L) {
|
|
wc.addWc("A.flgVideo=1");
|
|
}
|
|
if (!CR.getPettorale().isEmpty())
|
|
wc.addWc("A.pettorali like'%," + CR.getPettorale() + ",%'");
|
|
}
|
|
|
|
public Vectumerator<FotoVision> findByCRFotoSelfie(FotoCR CR, int pageNumber, int pageRows) {
|
|
if (CR.getFotoSelfie() == null)
|
|
return findByCR(CR, pageNumber, pageRows);
|
|
String s_Sql_Find1 = "select C.cosineSim , A.* from FOTO AS A inner join FOTO_FACE AS B ON A.id_foto=B.id_foto inner join FACE_RECOG AS C ON C.id_fotoFace2 = B.id_fotoFace inner join FOTO_FACE AS S ON S.id_fotoFace=C.id_fotoFace1";
|
|
String s_Sql_Find2 = "select C.cosineSim ,A.* from FOTO AS A inner join FOTO_FACE AS B ON A.id_foto=B.id_foto inner join FACE_RECOG AS C ON C.id_fotoFace2 = B.id_fotoFace";
|
|
String s_Sql_Order = " order by C.cosineSim DESC ";
|
|
if ((pageNumber == 0 && pageRows == 0) || pageRows == 999)
|
|
s_Sql_Order = " order by rand() ";
|
|
WcString wc = new WcString();
|
|
if (CR.getId_evento() != 0L)
|
|
wc.addWc("A.id_evento=" + CR.getId_evento());
|
|
if (CR.getId_puntoFoto() != 0L)
|
|
wc.addWc("A.id_puntoFoto=" + CR.getId_puntoFoto());
|
|
if (!CR.getFileName().isEmpty())
|
|
wc.addWc("A.fileName like '%" + CR.getFileName() + "%'");
|
|
String l_pettorale = "";
|
|
if (!CR.getPettorale().isEmpty())
|
|
l_pettorale = CR.getPettorale();
|
|
if (!l_pettorale.isEmpty())
|
|
wc.addWc("A.pettorali like'%," + l_pettorale + ",%'");
|
|
if (CR.getFlgVideo() == 0L) {
|
|
wc.addWc("(A.flgVideo is null or A.flgVideo=0)");
|
|
} else if (CR.getFlgVideo() == 1L) {
|
|
wc.addWc("A.flgVideo=1");
|
|
}
|
|
wc.addWc("S.id_foto=" + CR.getFotoSelfie().getId_foto());
|
|
double csim = CR.getPercIdx1() / 100.0D;
|
|
wc.addWc("C.cosineSim>=" + csim);
|
|
System.out.println(s_Sql_Find1 + s_Sql_Find1 + wc.toString());
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find1 + s_Sql_Find1 + wc.toString());
|
|
return findRows(stmt, pageNumber, pageRows);
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
protected int findByCRTotRecord(FotoCR CR) {
|
|
StringBuffer s_Sql_Find = new StringBuffer("select count(A.id_foto) as tot from FOTO AS A");
|
|
WcString wc = new WcString();
|
|
findByCRCreateWC(CR, s_Sql_Find, wc);
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(String.valueOf(s_Sql_Find) + String.valueOf(s_Sql_Find));
|
|
return (int)getTots(stmt);
|
|
} catch (Exception e) {
|
|
handleDebug(e);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public Vectumerator<FotoVision> findByPettoraliCR(FotoCR CR, int pageNumber, int pageRows) {
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = " order by A.fileName ";
|
|
if ((pageNumber == 0 && pageRows == 0) || pageRows == 999)
|
|
s_Sql_Order = " order by rand() ";
|
|
WcString wc = new WcString();
|
|
if (CR.getId_puntoFoto() == 0L) {
|
|
s_Sql_Find = s_Sql_Find + ", PUNTO_FOTO AS B";
|
|
wc.addWc("A.id_puntoFoto=B.id_puntoFoto");
|
|
}
|
|
if (CR.getId_evento() != 0L)
|
|
wc.addWc("A.id_evento=" + CR.getId_evento());
|
|
if (CR.getId_puntoFoto() != 0L)
|
|
wc.addWc("A.id_puntoFoto=" + CR.getId_puntoFoto());
|
|
if (!CR.getFileName().isEmpty())
|
|
wc.addWc("A.fileName like '%" + CR.getFileName() + "%'");
|
|
String l_pettorale = "";
|
|
if (!CR.getPettorale().isEmpty())
|
|
l_pettorale = CR.getPettorale();
|
|
if (!l_pettorale.isEmpty())
|
|
wc.addWc("A.pettorali like'%," + l_pettorale + ",%'");
|
|
if (CR.getFlgVideo() == 0L) {
|
|
wc.addWc("(A.flgVideo is null or A.flgVideo=0)");
|
|
} else if (CR.getFlgVideo() == 1L) {
|
|
wc.addWc("A.flgVideo=1");
|
|
}
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
return findRows(stmt, pageNumber, pageRows);
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
public String getFotoFaceRowid() {
|
|
return (this.fotoFaceRowid == null) ? "" : this.fotoFaceRowid.trim();
|
|
}
|
|
|
|
public void setFotoFaceRowid(String fotoFaceRowid) {
|
|
this.fotoFaceRowid = fotoFaceRowid;
|
|
}
|
|
|
|
public ResParm _ricalcolaFotoFacesSelfie() {
|
|
ResParm rp = new ResParm();
|
|
Vectumerator<FotoVision> vec = findSelfies(0L);
|
|
StringBuilder sb = new StringBuilder("N. Tot Selfie:" + vec.getTotNumberOfRecords() + "<br>");
|
|
while (vec.hasMoreElements()) {
|
|
FotoVision row = (FotoVision)vec.nextElement();
|
|
FotoVision foto = new FotoVision(getApFull());
|
|
foto.setId_foto(row.getId_foto());
|
|
rp = foto._ricalcolaFotoFaces();
|
|
if (!rp.getMsg().isEmpty())
|
|
sb.append(rp.getMsg());
|
|
}
|
|
rp.setMsg(sb.toString());
|
|
return rp;
|
|
}
|
|
|
|
public Vectumerator<FotoVision> findSelfies(long ggGreaterThan) {
|
|
String s_Sql_Find = "select A.* from FOTO AS A";
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.flgFotoSearch=1");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
return findRows(stmt);
|
|
} catch (SQLException e) {
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
public ResParm updatePettoraliByLabel() {
|
|
ResParm rp = new ResParm();
|
|
if (getId_foto() > 0L) {
|
|
FotoFace ff = new FotoFace(getApFull());
|
|
Vectumerator<FotoFace> vec = ff.findByFotoConPettorale(getId_foto());
|
|
StringBuilder sb = new StringBuilder(",");
|
|
while (vec.hasMoreElements()) {
|
|
FotoFace row = (FotoFace)vec.nextElement();
|
|
sb.append(row.getPettorale());
|
|
sb.append(",");
|
|
}
|
|
setPettorali(sb.toString());
|
|
rp = save();
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
protected void fillFields(ResultSet rst) {
|
|
super.fillFields(rst);
|
|
}
|
|
|
|
public ResParm save() {
|
|
ResParm rp = super.save();
|
|
return rp;
|
|
}
|
|
|
|
protected void initFields() {
|
|
super.initFields();
|
|
}
|
|
|
|
public static final ResParm loadFacesByFaceRecognitionJson(FotoI foto, JSONObject jo) {
|
|
ResParm rp = new ResParm();
|
|
if (foto.getId_foto() == 0L || foto.getApFull() == null) {
|
|
rp.setStatus(false);
|
|
rp.setMsg("Errore! Foto non valida..");
|
|
}
|
|
StringBuilder msg = new StringBuilder();
|
|
FotoFace ff = new FotoFace(foto.getApFull());
|
|
ff.deleteFotoFaceByFoto(foto.getId_foto(), true);
|
|
JSONArray jaResponses = jo.getJSONArray("responses");
|
|
boolean visiTrovati = false;
|
|
for (int i = 0; i < jaResponses.length(); i++) {
|
|
JSONObject joResponse = jaResponses.getJSONObject(i).getJSONObject("response");
|
|
System.out.println(joResponse.toString(4));
|
|
HashMap<Integer, Integer> hmNumbers = new HashMap<>();
|
|
if (!joResponse.has("numbers"));
|
|
JSONArray jaNumbers = joResponse.getJSONArray("numbers");
|
|
for (int j = 0; j < jaNumbers.length(); j++) {
|
|
JSONObject joNumber = jaNumbers.getJSONObject(j);
|
|
hmNumbers.put(Integer.valueOf(joNumber.getInt("xCenter")), Integer.valueOf(joNumber.getInt("value")));
|
|
}
|
|
if (joResponse.has("faceAnnotations")) {
|
|
visiTrovati = true;
|
|
JSONArray jaFaceAnnotations = joResponse.getJSONArray("faceAnnotations");
|
|
for (int k = 0; k < jaFaceAnnotations.length(); k++) {
|
|
JSONObject joFaceAnnotation = jaFaceAnnotations.getJSONObject(k);
|
|
ff = new FotoFace(foto.getApFull());
|
|
ff.setId_foto(foto.getId_foto());
|
|
JSONObject joFace = new JSONObject();
|
|
JSONObject joFdBoundingPoly = joFaceAnnotation.getJSONObject("fdBoundingPoly");
|
|
joFace.put("fdBoundingPoly", joFdBoundingPoly);
|
|
for (Map.Entry<Integer, Integer> entry : hmNumbers.entrySet())
|
|
System.out.println("Key : " + String.valueOf(entry.getKey()) + " Value : " + String.valueOf(entry.getValue()));
|
|
rp = ff.save();
|
|
if (!rp.getStatus())
|
|
msg.append("Errore: impossibile caricare fotoface da json per foto id " + foto.getId_foto() + ": " + rp.getMsg() + "\n");
|
|
}
|
|
}
|
|
}
|
|
if (!visiTrovati) {
|
|
ff = new FotoFace(foto.getApFull());
|
|
ff.setId_foto(foto.getId_foto());
|
|
rp = ff.save();
|
|
}
|
|
if (rp.getStatus())
|
|
if (foto.getMd5().isEmpty()) {
|
|
foto.setMd5(DBAdapter.getFileMd5(foto.getFileNameFaceRecognition()));
|
|
rp.append(foto.save());
|
|
}
|
|
if (msg.length() > 0) {
|
|
rp.setStatus(false);
|
|
rp.setMsg(msg.toString());
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public static final ResParm loadFacesByGoogleVision(FotoI foto) {
|
|
ResParm rp = new ResParm();
|
|
if (foto.getId_foto() == 0L || foto.getApFull() == null) {
|
|
rp.setStatus(false);
|
|
rp.setMsg("Errore! Foto non valida");
|
|
}
|
|
if (!foto.hasFotoFaces()) {
|
|
System.out.println("loadFacesByGoogleVision: " + foto.getId_foto() + " " + foto.getFileNameFaceRecognition());
|
|
String GVA_KEY = foto.getParm("GOOGLE_API_KEY_VISION").getTesto();
|
|
GoogleVisionApi gva = new GoogleVisionApi(GVA_KEY);
|
|
GoogleVisionResult resF = gva.annotateFaces(foto.getFileNameFaceRecognition());
|
|
if (resF.isOk())
|
|
return loadFacesByGoogleVisionJson(foto, (JSONObject)resF.getResult());
|
|
rp.setStatus(false);
|
|
rp.setMsg(resF.getMsg());
|
|
return rp;
|
|
}
|
|
System.out.println("loadFacesByGoogleVision solo ricalcolo: " + foto.getId_foto() + " " + foto.getFileNameFaceRecognition());
|
|
foto._ricalcolaFotoFaces();
|
|
rp.setStatus(true);
|
|
rp.setMsg("Ricalcolo effettuato ");
|
|
return rp;
|
|
}
|
|
|
|
public long getNumOfFaces() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getHeight() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getWidth() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getMargineFotoBottom() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getMargineFotoTop() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getMargineFotoSx() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getMargineFotoDx() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getDimMinFotoFaceReal() {
|
|
return 0L;
|
|
}
|
|
|
|
public FaceDetectionMethod getFaceDetectionMethodReal() {
|
|
return null;
|
|
}
|
|
|
|
public double getDetectFaceConfidentThresold() {
|
|
return 0.9D;
|
|
}
|
|
|
|
public void setId_faceDetectionMethod(long l_id_faceDetectionMethod) {}
|
|
|
|
public long getId_faceDetectionMethod() {
|
|
return 0L;
|
|
}
|
|
|
|
public long getFaceSize() {
|
|
return getEvento().getFaceRecognizer().getFaceSize();
|
|
}
|
|
}
|