www in docker support

This commit is contained in:
MaddoScientisto 2026-04-22 18:41:37 +02:00
commit c227fce036
2145 changed files with 399596 additions and 58 deletions

View file

@ -0,0 +1,95 @@
package it.acxent.face.fr.zoo;
import it.acxent.common.Parm;
import it.acxent.db.ApplParmFull;
import it.acxent.db.WcString;
import it.acxent.util.StringTokenizer;
import it.acxent.util.Vectumerator;
import java.sql.PreparedStatement;
public class Users extends it.acxent.anag.Users {
public Users() {}
public Users(ApplParmFull newApplParmFull) {
super(newApplParmFull);
}
public void initApplicationParms(ApplParmFull ap) {
if (ap != null) {
super.initApplicationParms(ap);
String l_tipoParm = "";
Parm bean = new Parm(ap);
l_tipoParm = "FACE-FR-ZOO";
bean.findByCodice("PATH_ZOO_BACKEND_TARGET");
bean.setFlgAdmin(1L);
bean.setTipoParm(l_tipoParm);
bean.setCodice("PATH_ZOO_BACKEND_TARGET");
bean.setDescrizione("PATH_ZOO_BACKEND_TARGET");
bean.setFlgTipo(1L);
bean.setNota(" Choose one of the backend-target pair:<br>0: (default) OpenCV implementation + CPU, <br>1: CUDA + GPU (CUDA), <br>2: CUDA + GPU (CUDA FP16), <br>3: TIM-VX + NPU, <br>4: CANN + NPU");
bean.save();
bean.findByCodice("PATH_ZOO_FACE_DETECTION_SCRIPT");
bean.setFlgAdmin(1L);
bean.setTipoParm(l_tipoParm);
bean.setCodice("PATH_ZOO_FACE_DETECTION_SCRIPT");
bean.setDescrizione("PATH_ZOO_FACE_DETECTION_SCRIPT");
bean.setFlgTipo(0L);
if (bean.getTesto().isEmpty())
bean.setTesto("/home/sites/fr/admin/_shell/detection.sh");
bean.setNota("Script completo per recognizer: <br>/home/sites/zoo/admin/_shell/detection.sh<br>I parametri sono<br> --input_json INPUT_JSON<br>Path del file JSON di input.\n --output_json OUTPUT_JSON<br>Path del file JSON di output.\n --backend_target BACKEND_TARGET, -bt BACKEND_TARGET<br> -d DELETE Cancella file json input_json alla fine");
bean.save();
bean.findByCodice("PATH_ZOO_FACE_RECOGNITION_SCRIPT");
bean.setFlgAdmin(1L);
bean.setTipoParm(l_tipoParm);
bean.setCodice("PATH_ZOO_FACE_RECOGNITION_SCRIPT");
bean.setDescrizione("PATH_ZOO_FACE_RECOGNITION_SCRIPT");
bean.setFlgTipo(0L);
if (bean.getTesto().isEmpty())
bean.setTesto("/home/sites/fr/admin/_shell/recognizer.sh");
bean.setNota("Script completo per recognizer: <br>/home/sites/zoo/admin/_shell/recognizer.sh<br>I parametri sono<br> --input_json INPUT_JSON<br>Path del file JSON di input.\n --output_json OUTPUT_JSON<br>Path del file JSON di output.\n --backend_target BACKEND_TARGET, -bt BACKEND_TARGET<br> -d DELETE Cancella file json input_json alla fine");
bean.save();
}
}
public Vectumerator<Users> findByCR(UsersCR CR, int pageNumber, int pageRows) {
String s_Sql_Find = "select A.* from USERS AS A";
WcString wc = new WcString();
wc.addWc("dataFineVld is null");
wc = managePolicyFind(CR, wc);
if (!CR.isCode1())
wc.addWc("A.id_users <>1");
if (!CR.getFlgValido().isEmpty())
wc.addWc("A.flgValido='" + CR.getFlgValido() + "'");
if (!CR.getSearchTxt().trim().isEmpty()) {
String temp = CR.getSearchTxt().trim().replace("*", "%");
StringTokenizer st = new StringTokenizer(temp, " ");
StringBuffer txt = new StringBuffer("(");
int countToken = 0;
while (st.hasMoreTokens()) {
String token = prepareSqlString(st.nextToken());
countToken++;
if (countToken > 1)
token = "%" + token;
txt.append("(A.nome like'%" + token + "%' or A.cognome like'%" + token + "%' or A.eMail like'%" + token + "%' or A.codFisc like'%" + token + "%' or A.login like'%" + token + "%')");
if (st.hasMoreTokens())
txt.append(" and ");
}
txt.append(")");
wc.addWc(txt.toString());
}
if (!CR.getEMail().isEmpty())
wc.addWc("A.eMail like'%" + CR.getEMail() + "%'");
if (CR.getFlgMl() == 0L) {
wc.addWc("(A.flgMl is null or A.flgMl=0)");
} else if (CR.getFlgMl() > 0L) {
wc.addWc("A.flgMl =" + CR.getFlgMl());
}
try {
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find);
return findRows(stmt, pageNumber, pageRows);
} catch (Exception e) {
handleDebug(e);
return AB_EMPTY_VECTUMERATOR;
}
}
}

View file

@ -0,0 +1,15 @@
package it.acxent.face.fr.zoo;
import it.acxent.db.ApplParmFull;
public class UsersCR extends it.acxent.common.UsersCR {
public boolean isCode1() {
return false;
}
public UsersCR(ApplParmFull newApplParmFull) {
super(newApplParmFull);
}
public UsersCR() {}
}

View file

@ -0,0 +1 @@
package it.acxent.face.fr.zoo;

View file

@ -0,0 +1,387 @@
package it.acxent.face.fr.zoo.rest;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import it.acxent.db.ApplParm;
import it.acxent.db.ApplParmFull;
import it.acxent.db.CRAdapter;
import it.acxent.db.DBAdapter;
import it.acxent.db.ResParm;
import it.acxent.face.api.opencv.TrainingImage;
import it.acxent.face.fr.FotoRequest;
import it.acxent.face.fr.zoo.Users;
import it.acxent.rest._AblRestAdapter;
import it.acxent.util.Debug;
import it.acxent.util.FileWr;
import it.acxent.util.ShellWrapper;
import it.acxent.util.Timer;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.face.FaceRecognizer;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@WebServlet(urlPatterns = {"/facezoo/*"})
public class FaceZooRestSvlt extends _AblRestAdapter {
private static Debug debugHelper = new Debug();
private static final boolean debug = false;
private Cache<String, Object> cache;
private static final long serialVersionUID = -8266400675249237394L;
public void __templateChiamata(HttpServletRequest req, HttpServletResponse res) {
ApplParmFull apFull = getApFull(req);
boolean debug = false;
String parametroTest = getRequestParameter(req, "id");
DBAdapter.printDebug(" parm id: " + parametroTest + " apiParms: " + getApiParms() + " json post: " + getApiJSONObject().toString(4));
try {
long l_id_user = getRequestLongParameter(req, "id_user");
boolean authOk = checkBasicAuth(req, res);
if (!authOk)
sendHtmlMsgResponse(req, res, "testina di vitello, _elencofoto");
} catch (Exception e) {
e.printStackTrace();
sendHtmlMsgResponse(req, res, "testina di vitello,_elencofoto eccezione: " + e.getMessage());
}
}
protected DBAdapter getBean(HttpServletRequest req) {
return null;
}
protected CRAdapter getBeanCR(HttpServletRequest req) {
return null;
}
public void _detectFaces_POST(HttpServletRequest req, HttpServletResponse res) {
boolean debug = true;
Timer timer = null;
timer = new Timer();
timer.start();
DBAdapter.printDebug(debug, "************ _detectFaces_POST 1");
int MAX_ATTEMPTS = 3;
int RETRY_DELAY_MS = 1000;
JsonObject jresPy = null;
HttpSession oldSession = req.getSession(false);
Users user = getUsersByBasicAuth(req, res);
StringBuilder err = new StringBuilder();
StringBuilder msg = new StringBuilder();
StringBuilder errMsg = new StringBuilder();
FotoRequest fr = new FotoRequest(getApFull());
try {
DBAdapter.printDebug(debug, "************ _detectFaces_POST 2");
JsonObject jo = getApiJsonObject();
DBAdapter.printDebug(debug, "************ _detectFaces_POST 3 *********************");
DBAdapter.printDebug(debug, "json in arrivo");
DBAdapter.printDebug(DBAdapter.gsonToString(jo));
DBAdapter.printDebug(debug, "*-------------- FINE JSON IN ARRIVO ---------------------------*");
String pathBase = getApFull().getParm("path_img_tmp").getTesto();
String targetDirJsonData = pathBase + "_json/";
File targetDirFile = new File(targetDirJsonData);
if (!targetDirFile.exists())
targetDirFile.mkdirs();
JsonArray jresponses = new JsonArray();
int numberOfFaces = 0;
boolean cachedFaces = false;
JsonObject jsonData = new JsonObject();
float confidenceThresold = jo.get("conf_threshold").getAsFloat();
jsonData.addProperty("conf_threshold", Float.valueOf(confidenceThresold));
long detectionMethod = (long)(100 + (int)(confidenceThresold * 100.0F));
HashSet<File> hsFiles = new HashSet<>();
JsonArray jQueryA = new JsonArray();
JsonArray jRequestsA = jo.getAsJsonArray("requests");
for (int i = 0; i < jRequestsA.size(); i++) {
JsonObject jRequest = jRequestsA.get(i).getAsJsonObject();
String imageContent = null, imagePath = null;
boolean hasImage = false;
if (jRequest.has("path")) {
imagePath = jRequest.get("path").getAsString();
} else {
hasImage = true;
JsonObject imageObject = jRequest.getAsJsonObject("image");
imageContent = imageObject.get("content").getAsString();
}
String md5 = jRequest.get("md5").getAsString();
if (oldSession != null) {
DBAdapter.printDebug(debug, "************ _detectFaces_POST 1.2.1 session id: " +
req.getSession().getId() + " i: " + i + " md5: " + md5);
} else {
DBAdapter.printDebug(debug, "************ _detectFaces_POST 1.2.2 session id inesistente md5: " + md5);
}
long l_requestType = 1L;
DBAdapter.printDebug(debug, "************ _detectFaces_POST 4 findByUserMd5DetectionmethodRequesttype *********************");
DBAdapter.printDebug(debug, "md5: " + md5);
fr.findByUserMd5DetectionmethodRequesttype(user.getId_users(), md5, detectionMethod, l_requestType);
String dbJson = fr.getJson();
if (!dbJson.isEmpty()) {
JsonObject jresponse = JsonParser.parseString(dbJson).getAsJsonObject();
jresponses.add((JsonElement)jresponse);
fr.addRichiesta();
fr.save();
cachedFaces = true;
DBAdapter.printDebug("************ _detectFaces_POST 5 findByUserMd5DetectionmethodRequesttype OK *********************");
DBAdapter.printDebug(debug, "md5: " + md5);
JsonObject jsonObject = JsonParser.parseString(fr.getJson()).getAsJsonObject();
DBAdapter.printDebug(debug, "json sul db" + DBAdapter.gsonToString(jsonObject));
} else {
DBAdapter.printDebug(debug, "************ _detectFaces_POST 6 PREPARO RICHIESTA PHYTON *********************");
DBAdapter.printDebug(debug, "md5: " + md5);
JsonObject jQueryRow = new JsonObject();
jQueryRow.addProperty("md5", md5);
if (hasImage) {
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
String currentImage = pathBase + pathBase + ".jpg";
File file = new File(currentImage);
if (!file.exists()) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(decodedBytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null)
fos.close();
}
}
jQueryRow.addProperty("path", currentImage);
hsFiles.add(file);
} else {
jQueryRow.addProperty("path", imagePath);
}
jQueryA.add((JsonElement)jQueryRow);
}
}
if (jQueryA.size() > 0) {
jsonData.add("query", (JsonElement)jQueryA);
DBAdapter.printDebug(debug, "_detectFaces_POST jsonData:\n" + DBAdapter.gsonToString(jsonData));
String rndName = getTimeNameForFileUpload();
String json_data_path = targetDirJsonData + "jDataDetect_" + targetDirJsonData + ".json";
String json_res_path = targetDirJsonData + "jDataDetect_" + targetDirJsonData + "_res.json";
DBAdapter.printDebug(debug, "************ _detectFaces_POST 7 writing json *********************");
DBAdapter.printDebug(debug, "json_data_path: " + json_data_path);
jsonData.addProperty("debug", "FaceZooRest._detectFaces_POST");
FileWr fw = new FileWr(json_data_path, false);
fw.writeLine(DBAdapter.gsonToString(jsonData));
fw.closeFile();
List<String> pythonScript = Arrays.asList(getParm("PATH_ZOO_FACE_DETECTION_SCRIPT").getTesto(), "--input_json", json_data_path, "--output_json", json_res_path);
for (int attempt = 1; attempt <= 3; attempt++) {
String result = ShellWrapper.executeShellScript(pythonScript);
jresPy = DBAdapter.tryParseJsonObject(result);
if (jresPy != null)
break;
if (attempt < 3)
try {
Thread.sleep(1000L);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
}
if (jresPy == null)
throw new RuntimeException("Errore: impossibile ottenere un JSON valido dopo 3 tentativi");
DBAdapter.printDebug(debug, "************ _detectFaces_POST 8 RISPOSTA SCRIPT PYTHON *********************");
DBAdapter.printDebug(DBAdapter.gsonToString(jresPy));
JsonArray jresponsesPy = jresPy.getAsJsonArray("responses");
for (int j = 0; j < jresponsesPy.size(); j++) {
JsonObject jresponsePy = jresponsesPy.get(j).getAsJsonObject();
jresponses.add((JsonElement)jresponsePy);
String md5 = jresponsePy.getAsJsonObject("response").get("md5")
.getAsString();
if (user.getId_users() > 0L) {
fr.setId_users(user.getId_users());
fr.setJson(jresponsePy.toString());
fr.setMd5(md5);
fr.setFaceDetectionMethodCode(detectionMethod);
fr.setFlgRequestType(1L);
fr.addRichiesta();
fr.save();
}
}
for (File elemento : hsFiles)
elemento.delete();
}
JsonObject joRes = new JsonObject();
if (errMsg.length() == 0) {
String target = "vertices";
numberOfFaces = DBAdapter.countOccurrences(jresponses.toString(), target);
joRes.addProperty("success", "true");
joRes.addProperty("msg", "Trovate in totale " + numberOfFaces + " visi su " +
jresponses.size() + " foto. FaceCached?" + cachedFaces + " : " + msg.toString());
joRes.add("responses", (JsonElement)jresponses);
} else {
joRes.addProperty("success", "false");
joRes.addProperty("msg", "Errore! " + err.toString());
}
DBAdapter.printDebug(debug, "************ _detectFaces_POST 9 RISPOSTA AL CLIENT *********************");
DBAdapter.printDebug(DBAdapter.gsonToString(joRes));
DBAdapter.printDebug(debug, "*-------------- FINE ---------------------------*");
sendJsonMsgResponse(req, res, joRes.toString());
timer.stop();
DBAdapter.printDebug(debug, " _detectFaces_POST. Resp size: Trovate in totale " + numberOfFaces + " visi su " +
jresponses.size() + " foto. FaceCached?" + cachedFaces + " ms " + timer.getDurataMilliSec());
} catch (RuntimeException e) {
JsonObject joRes = new JsonObject();
joRes.addProperty("success", "false");
joRes.addProperty("msg", "Eccezione!! " + e.getMessage());
DBAdapter.printDebug(debug, "************ _detectFaces_POST ECCEZIONE RUNTIME!!!! RISPOSTA AL CLIENT *********************");
DBAdapter.printDebug(DBAdapter.gsonToString(joRes));
DBAdapter.printDebug(debug, "*-------------- FINE ---------------------------*");
sendJsonMsgResponse(req, res, DBAdapter.gsonToString(joRes));
} catch (Exception e) {
e.printStackTrace();
JsonObject joRes = new JsonObject();
joRes.addProperty("success", "false");
joRes.addProperty("msg", "Eccezione!! " + e.getMessage());
DBAdapter.printDebug(debug, "************ _detectFaces_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
DBAdapter.printDebug(DBAdapter.gsonToString(joRes));
DBAdapter.printDebug(debug, "*-------------- FINE ---------------------------*");
sendJsonMsgResponse(req, res, DBAdapter.gsonToString(joRes));
}
}
public static void main(String[] args) {
String hostname = "localhost";
String db = "cc";
ApplParmFull ap = new ApplParmFull(new ApplParm(14, "//" + hostname + ":1433;databaseName=" + db + ";user=sa;password=T3st000!", null, null, 1, 10, 300));
}
protected boolean checkBasicAuth(HttpServletRequest req, HttpServletResponse res) {
boolean authOk = false;
try {
String[] auth = getBasicAuthorizationHeaders(req, res);
if (auth != null) {
Users bean = getUsersByBasicAuth(req, res);
if (bean.getDBState() == 1) {
authOk = true;
} else {
DBAdapter.printDebug(false, "FaceRestSvlt.checkBasicAuth KO: login-->" + auth[0] + " pwd-->" + auth[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return authOk;
}
protected String getApiBaseUrl() {
return "/fr/face/";
}
protected Users getUsersByBasicAuth(HttpServletRequest req, HttpServletResponse res) {
String[] auth = getBasicAuthorizationHeaders(req, res);
Users bean = new Users(getApFull());
if (auth != null)
bean.findLogonUtente(auth[0], auth[1], req.getRemoteAddr());
return bean;
}
private ResParm trainingCreate(String l_sessionId, List<TrainingImage> trainingImages, int recognizerType) {
ResParm rp = new ResParm();
FaceRecognizer ocvFr = (FaceRecognizer)getCache()
.getIfPresent(l_sessionId + "_" + l_sessionId);
int counter = 0;
String counterKey = l_sessionId + "_" + l_sessionId + "_counter";
Integer Icounter = (Integer)getCache().getIfPresent(counterKey);
if (Icounter != null)
counter = Icounter;
DBAdapter.printDebug(false, "trainingCreate " + trainingImages.size() + " immagini");
if (trainingImages.size() > 0) {
Mat labels = new Mat(trainingImages.size(), 1, CvType.CV_32SC1);
MatOfInt labelsMat = new MatOfInt();
List<Mat> trainingImagesMat = new ArrayList<>();
for (TrainingImage trainingImage : trainingImages) {
File imageFile = new File(trainingImage.getImageFileName());
Mat image = Imgcodecs.imread(imageFile.getAbsolutePath());
Mat grayImage = new Mat();
Imgproc.cvtColor(image, grayImage, 6);
trainingImagesMat.add(grayImage);
labels.put(counter, 0, new double[] { (double)trainingImage.getLabel() });
DBAdapter.printDebug("train: " +
trainingImage.getImageFileName() + " count:" + counter + " label: " + trainingImage.getLabel());
counter++;
}
labels.convertTo((Mat)labelsMat, CvType.CV_32SC1);
ocvFr.train(trainingImagesMat, labels);
getCache().put(counterKey, Integer.valueOf(counter));
}
return rp;
}
private Cache<String, Object> getCache() {
if (this.cache == null)
this.cache = CacheBuilder.newBuilder().maximumSize(100L).expireAfterAccess(100L, TimeUnit.MINUTES).build();
return this.cache;
}
public void _scoring_POST(HttpServletRequest req, HttpServletResponse res) {
boolean debug = false;
boolean debug2 = false;
Timer timer = null;
timer = new Timer();
timer.start();
String result = "";
String json_data_path = null;
String json_res_path = null;
try {
List<String> pythonScript;
JsonObject jo = getApiJsonObject();
DBAdapter.printDebug(debug, "************ _scoringZoo_POST *********************");
DBAdapter.printDebug(debug, "json in arrivo");
DBAdapter.printDebug(DBAdapter.gsonToString(jo));
DBAdapter.printDebug(debug, "*-------------- FINE ---------------------------*");
JsonObject joRes = new JsonObject();
json_data_path = jo.get("json_data_path").getAsString();
json_res_path = jo.get("json_res_path").getAsString();
if (debug || debug2) {
pythonScript = Arrays.asList(getParm("PATH_ZOO_FACE_RECOGNITION_SCRIPT").getTesto(), "--input_json", json_data_path, "--output_json", json_res_path, "-d");
} else {
pythonScript = Arrays.asList(getParm("PATH_ZOO_FACE_RECOGNITION_SCRIPT").getTesto(), "--input_json", json_data_path, "-d");
}
result = ShellWrapper.executeShellScript(pythonScript);
DBAdapter.printDebug(debug, " RESULT from SCRIPT PY: " + String.valueOf(pythonScript) + ":\n" + result);
DBAdapter.printDebug(debug, "\n--------------------------");
joRes = JsonParser.parseString(result.trim()).getAsJsonObject();
DBAdapter.printDebug(debug, "************ _scoringZoo_POST RISPOSTA AL CLIENT *********************");
DBAdapter.printDebug(DBAdapter.gsonToString(joRes));
timer.stop();
int numResponses = -1;
if (joRes.has("responses"))
numResponses = joRes.getAsJsonArray("responses").size();
DBAdapter.printDebug(debug, " _scoringZoo_POST.. Resp size: " + numResponses + " ms " + timer.getDurataMilliSec() + " json_data_path: " + json_data_path);
sendJsonMsgResponse(req, res, joRes.toString());
} catch (Exception e) {
e.printStackTrace();
JsonObject joRes = new JsonObject();
joRes.addProperty("success", "false");
joRes.addProperty("msg", "Ritorno: " + result + "\nEccezione!! " + e.getMessage());
DBAdapter.printDebug(debug, "************ _scoringZoo_POST ECCEZIONE!!!! json_data_path: " + json_data_path + "\nRISPOSTA AL CLIENT *********************");
DBAdapter.printDebug(DBAdapter.gsonToString(joRes));
DBAdapter.printDebug(debug, "*-------------- FINE ---------------------------*");
sendJsonMsgResponse(req, res, DBAdapter.gsonToString(joRes));
}
}
protected Class<? extends _AblRestAdapter> getConcreteApiClass() {
return null;
}
}

View file

@ -0,0 +1 @@
package it.acxent.face.fr.zoo.rest;