www in docker support
This commit is contained in:
parent
539a848e95
commit
c227fce036
2145 changed files with 399596 additions and 58 deletions
|
|
@ -0,0 +1,41 @@
|
|||
package it.acxent.face.fr.rest;
|
||||
|
||||
public class FaceRestResult {
|
||||
private String msg;
|
||||
|
||||
public FaceRestResult(String msg, boolean status, Object result) {
|
||||
this.msg = msg;
|
||||
this.ok = status;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
private boolean ok = true;
|
||||
|
||||
private Object result;
|
||||
|
||||
public FaceRestResult() {}
|
||||
|
||||
public String getMsg() {
|
||||
return (this.msg == null) ? "" : this.msg.trim();
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public boolean isOk() {
|
||||
return this.ok;
|
||||
}
|
||||
|
||||
public void setOk(boolean status) {
|
||||
this.ok = status;
|
||||
}
|
||||
|
||||
public Object getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
public void setResult(Object result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,891 @@
|
|||
package it.acxent.face.fr.rest;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import it.acxent.common.StatusMsg;
|
||||
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.dm.FaceDetectionMethod;
|
||||
import it.acxent.dm.FaceDetectionMethodCR;
|
||||
import it.acxent.face.api.opencv.FaceRecognition;
|
||||
import it.acxent.face.api.opencv.TrainingImage;
|
||||
import it.acxent.face.fr.Face;
|
||||
import it.acxent.face.fr.FaceFaceRecognizer;
|
||||
import it.acxent.face.fr.FaceRecognizer;
|
||||
import it.acxent.face.fr.Facer;
|
||||
import it.acxent.face.fr.FotoRequest;
|
||||
import it.acxent.face.fr.Users;
|
||||
import it.acxent.rest._AblRestAdapter;
|
||||
import it.acxent.util.Debug;
|
||||
import it.acxent.util.Timer;
|
||||
import it.acxent.util.Vectumerator;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.ArrayList;
|
||||
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 org.apache.commons.io.FileUtils;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfInt;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
@WebServlet(urlPatterns = {"/face/*"})
|
||||
public class FaceRestSvlt 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");
|
||||
if (debug)
|
||||
System.out.println(" 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 _detectFacesAndNumbers_POST(HttpServletRequest req, HttpServletResponse res) {
|
||||
boolean debug = true;
|
||||
if (debug) {
|
||||
System.out.println("************ _detectFacesAndNumbers_POST req res *********************");
|
||||
System.out.println(req.toString());
|
||||
System.out.println("************");
|
||||
System.out.println(res.toString());
|
||||
System.out.println("************");
|
||||
}
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 1");
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
HashSet<File> hsFiles = new HashSet<>();
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 2");
|
||||
Users user = getUsersByBasicAuth(req, res);
|
||||
StringBuilder err = new StringBuilder();
|
||||
StringBuilder msg = new StringBuilder();
|
||||
try {
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 3");
|
||||
JSONObject jo = getApiJSONObject();
|
||||
if (debug) {
|
||||
System.out.println("************ _detectFacesAndNumbers_POST *********************");
|
||||
System.out.println("json in arrivo");
|
||||
System.out.println(jo.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
String pathBase = getApFull().getParm("path_img_tmp").getTesto();
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 3.1 : " + pathBase);
|
||||
File pathBaseFile = new File(pathBase);
|
||||
if (!pathBaseFile.exists())
|
||||
pathBaseFile.mkdirs();
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 4");
|
||||
boolean itsOk = true;
|
||||
FaceRecognition fa = null;
|
||||
fa = FaceRecognition.getInstance(apFull);
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 5");
|
||||
JSONArray jresponses = new JSONArray();
|
||||
int numberOfFaces = 0;
|
||||
int numberOfNumbers = 0;
|
||||
JSONArray jRequestsA = jo.getJSONArray("requests");
|
||||
for (int i = 0; i < jRequestsA.length(); i++) {
|
||||
JSONObject jRequest = jRequestsA.getJSONObject(i);
|
||||
String imageContent = null, imagePath = null;
|
||||
boolean hasImage = false;
|
||||
if (jRequest.has("path")) {
|
||||
imagePath = jRequest.getString("path");
|
||||
} else {
|
||||
hasImage = true;
|
||||
imageContent = jRequest.getJSONObject("image").getString("content");
|
||||
}
|
||||
String md5 = jRequest.getString("md5");
|
||||
long detectionMethod = jRequest.getLong("faceDetectionType");
|
||||
boolean numbers = jRequest.getBoolean("numbers");
|
||||
FotoRequest fr = new FotoRequest(getApFull());
|
||||
long l_requestType = 0L;
|
||||
if (!numbers && detectionMethod > 0L) {
|
||||
l_requestType = 1L;
|
||||
} else if (numbers == true && detectionMethod < 0L) {
|
||||
l_requestType = 3L;
|
||||
} else if (!numbers && detectionMethod < 0L) {
|
||||
l_requestType = -1L;
|
||||
}
|
||||
if (l_requestType >= 0L) {
|
||||
fr.findByUserMd5DetectionmethodRequesttype(user.getId_users(), md5, detectionMethod, l_requestType);
|
||||
String dbJson = fr.getJson();
|
||||
if (!dbJson.isEmpty() && !dbJson.equals("{}")) {
|
||||
JSONObject jresponse = new JSONObject(dbJson);
|
||||
jresponses.put(jresponse);
|
||||
fr.addRichiesta();
|
||||
fr.save();
|
||||
} else {
|
||||
String currentImage;
|
||||
if (hasImage) {
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
|
||||
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();
|
||||
}
|
||||
}
|
||||
hsFiles.add(file);
|
||||
} else {
|
||||
currentImage = imagePath;
|
||||
}
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 6");
|
||||
JSONObject jresponse = fa.detectFaces(currentImage, detectionMethod, numbers);
|
||||
if (hasImage)
|
||||
for (File elemento : hsFiles)
|
||||
elemento.delete();
|
||||
jresponses.put(jresponse);
|
||||
if (jresponse.has("numOfFaces"))
|
||||
numberOfFaces += jresponse.getInt("numOfFaces");
|
||||
if (jresponse.has("numOfNumbers"))
|
||||
numberOfNumbers += jresponse.getInt("numOfNumbers");
|
||||
if (user.getId_users() > 0L) {
|
||||
fr.setId_users(user.getId_users());
|
||||
fr.setJson(jresponse.toString());
|
||||
fr.setMd5(md5);
|
||||
fr.setFaceDetectionMethodCode(detectionMethod);
|
||||
fr.setFlgRequestType(l_requestType);
|
||||
fr.addRichiesta();
|
||||
fr.save();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
itsOk = false;
|
||||
err.append("Tipo richiesta e metodo detect faces non impostati");
|
||||
}
|
||||
}
|
||||
if (debug)
|
||||
System.out.println("************ _detectFacesAndNumbers_POST 7");
|
||||
JSONObject joRes = new JSONObject();
|
||||
if (itsOk) {
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Trovate in totale " + numberOfFaces + " visi e " + numberOfNumbers + " numeri su " +
|
||||
jresponses.length() + " foto: " + msg.toString());
|
||||
joRes.put("responses", jresponses);
|
||||
} else {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Errore! " + err.toString());
|
||||
}
|
||||
if (debug) {
|
||||
System.out.println("************ _detectFacesAndNumbers_POST RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _detectFacesAndNumbers_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
System.out.println("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;
|
||||
}
|
||||
|
||||
public void _trainingSendImages_POST(HttpServletRequest req, HttpServletResponse res) {
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
boolean debug = false;
|
||||
StringBuilder err = new StringBuilder();
|
||||
try {
|
||||
JSONObject jo = getApiJSONObject();
|
||||
if (debug) {
|
||||
System.out.println("************ _trainingSendimages_POST *********************");
|
||||
System.out.println("json in arrivo");
|
||||
System.out.println(jo.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
long newFaces = 0L;
|
||||
long newFacesNoImg = 0L;
|
||||
long findFaces = 0L;
|
||||
JSONObject joRes = new JSONObject();
|
||||
String code = jo.getString("code");
|
||||
StatusMsg.updateMsgByTag(getApFull(), "TRAINING_RUN", "RECEIVING TRAINING IMAGES FOR " + code + " .... ");
|
||||
FaceRecognizer frzer = new FaceRecognizer(getApFull());
|
||||
frzer.findByCode(code);
|
||||
if (frzer.getId_faceRecognizer() == 0L) {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "ERRORE! Recognizer " + code + " non trovato.");
|
||||
} else {
|
||||
JSONArray jRequestsA = jo.getJSONArray("requests");
|
||||
JSONArray jNoImageA = new JSONArray();
|
||||
JSONArray jLabelsA = new JSONArray();
|
||||
Face face = new Face(getApFull());
|
||||
Face faceLabel = new Face(getApFull());
|
||||
FaceFaceRecognizer ffr = new FaceFaceRecognizer(getApFull());
|
||||
Timer timer = new Timer();
|
||||
timer.start();
|
||||
String eta = " ....";
|
||||
for (int i = 0; i < jRequestsA.length(); i++) {
|
||||
StatusMsg.updateMsgByTag(getApFull(), "TRAINING_RUN", "RECEIVING TRAINING IMAGES FOR " + code + " .... " + i + 1 + "/" +
|
||||
jRequestsA.length() + 1 + " new: " + newFaces + " new noimg: " + newFacesNoImg + " find faces:" + findFaces + eta);
|
||||
JSONObject jRequest = jRequestsA.getJSONObject(i);
|
||||
String md5 = jRequest.getString("md5");
|
||||
long label = jRequest.getLong("label");
|
||||
System.out.println(String.valueOf(DBAdapter.getNow()) + " _trainingSendImages_POST: getting md5: " + String.valueOf(DBAdapter.getNow()));
|
||||
if (label > 0L) {
|
||||
faceLabel.findByPrimaryKey(label);
|
||||
if (faceLabel.getId_face() == 0L) {
|
||||
err.append("Inviata immagine con label non trovate: " + label);
|
||||
err.append("\n");
|
||||
}
|
||||
}
|
||||
if (err.length() == 0) {
|
||||
face.findByMd5(md5);
|
||||
if (face.getId_face() == 0L) {
|
||||
newFaces++;
|
||||
if (jRequest.has("image")) {
|
||||
face.setMd5(md5);
|
||||
ResParm rp = face.save();
|
||||
if (rp.getStatus()) {
|
||||
String imageContent = jRequest.getJSONObject("image")
|
||||
.getString("content");
|
||||
File path = new File(face.getPathFaceImg());
|
||||
if (!path.exists())
|
||||
path.mkdirs();
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
|
||||
File file = new File(face.getFullFileName());
|
||||
if (!file.exists()) {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(file);
|
||||
fos.write(decodedBytes);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
err.append("Impossibile salvare l'immagine md5 " + md5 + ". " + e.getMessage());
|
||||
err.append("\n");
|
||||
} finally {
|
||||
if (fos != null)
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err.append("Impossibile salvare record md5 " + md5);
|
||||
err.append("\n");
|
||||
}
|
||||
} else {
|
||||
newFacesNoImg++;
|
||||
JSONObject jMd5 = new JSONObject();
|
||||
jMd5.put("md5", md5);
|
||||
jNoImageA.put(jMd5);
|
||||
}
|
||||
} else {
|
||||
findFaces++;
|
||||
}
|
||||
StatusMsg.updateMsgByTag(getApFull(), "TRAINING_RUN", "RECEIVING TRAINING IMAGES FOR " + code + " .... " + i + 1 + "/" +
|
||||
jRequestsA.length() + 1 + " new: " + newFaces + " new noimg: " + newFacesNoImg + " find faces:" + findFaces + eta);
|
||||
if (face.getId_face() > 0L) {
|
||||
ffr.findByRecognizerLabel(frzer.getId_faceRecognizer(), face.getId_face());
|
||||
boolean doSave = false;
|
||||
if (ffr.getId_faceFaceRecognizer() == 0L) {
|
||||
ffr.setId_face(face.getId_face());
|
||||
ffr.setId_faceRecognizer(frzer.getId_faceRecognizer());
|
||||
doSave = true;
|
||||
}
|
||||
if (ffr.getId_faceLabel() != label) {
|
||||
ffr.setId_faceLabel(label);
|
||||
doSave = true;
|
||||
}
|
||||
if (doSave)
|
||||
ResParm resParm = ffr.save();
|
||||
JSONObject jLabelrow = new JSONObject();
|
||||
jLabelrow.put("md5", face.getMd5());
|
||||
jLabelrow.put("label", ffr.getId_faceLabel());
|
||||
jLabelrow.put("labelMd5", ffr.getFaceLabel().getMd5());
|
||||
jLabelsA.put(jLabelrow);
|
||||
}
|
||||
}
|
||||
eta = " " + timer.getEta((long)jRequestsA.length(), (long)i);
|
||||
}
|
||||
if (err.length() > 0) {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Codice " + code + " Errore: " + err.toString());
|
||||
} else {
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Training in corso: ricezione immagini....");
|
||||
joRes.put("code", code);
|
||||
joRes.put("numberTrainingImages",
|
||||
ffr.getTotFaceByFaceRecognizer(frzer.getId_faceRecognizer()));
|
||||
if (jNoImageA.length() > 0)
|
||||
joRes.put("noImg", jNoImageA);
|
||||
if (jLabelsA.length() > 0)
|
||||
joRes.put("labels", jLabelsA);
|
||||
}
|
||||
}
|
||||
if (!debug);
|
||||
System.out.println("************ _trainingSendimages_POST RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _trainingSendimages_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
StatusMsg.deleteMsgByTag(getApFull(), "TRAINING_RUN");
|
||||
}
|
||||
|
||||
private ResParm trainingCreate(String l_sessionId, List<TrainingImage> trainingImages, int recognizerType) {
|
||||
ResParm rp = new ResParm();
|
||||
org.opencv.face.FaceRecognizer ocvFr = (org.opencv.face.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;
|
||||
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() });
|
||||
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 _recognize_POST(HttpServletRequest req, HttpServletResponse res) {
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
boolean debug = true;
|
||||
Users user = getUsersByBasicAuth(req, res);
|
||||
StringBuilder err = new StringBuilder();
|
||||
try {
|
||||
JSONObject jo = getApiJSONObject();
|
||||
if (debug) {
|
||||
System.out.println("************ _recognize_POST *********************");
|
||||
System.out.println("json in arrivo");
|
||||
System.out.println(jo.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
debugHelper.writeLog("************ _recognize_POST *********************");
|
||||
debugHelper.writeLog("json in arrivo");
|
||||
debugHelper.writeLog(jo.toString(4));
|
||||
debugHelper.writeLog("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
JSONObject joRes = new JSONObject();
|
||||
JSONArray jResponses = new JSONArray();
|
||||
String code = jo.getString("code");
|
||||
FaceRecognition fa = null;
|
||||
fa = FaceRecognition.getInstance(apFull);
|
||||
FaceRecognizer frzer = new FaceRecognizer(getApFull());
|
||||
frzer.findByCode(code);
|
||||
if (frzer.getId_faceRecognizer() == 0L) {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "ERRORE! Recognizer " + code + " non trovato.");
|
||||
} else {
|
||||
JSONArray jRequestsA = jo.getJSONArray("requests");
|
||||
JSONArray jNoImageA = new JSONArray();
|
||||
Facer facer = new Facer(getApFull());
|
||||
for (int i = 0; i < jRequestsA.length(); i++) {
|
||||
JSONObject jRequest = jRequestsA.getJSONObject(i);
|
||||
String md5 = jRequest.getString("md5");
|
||||
facer.findByMd5Recognizer(md5, frzer.getId_faceRecognizer());
|
||||
if (facer.getId_facer() == 0L)
|
||||
if (jRequest.has("image")) {
|
||||
facer.setMd5(md5);
|
||||
facer.setId_faceRecognizer(frzer.getId_faceRecognizer());
|
||||
ResParm rp = facer.save();
|
||||
if (rp.getStatus()) {
|
||||
String imageContent = jRequest.getJSONObject("image")
|
||||
.getString("content");
|
||||
File path = new File(facer.getPathFaceImg());
|
||||
if (!path.exists())
|
||||
path.mkdirs();
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
|
||||
File file = new File(facer.getFullFileName());
|
||||
if (!file.exists()) {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(file);
|
||||
fos.write(decodedBytes);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
err.append("Impossibile salvare l'immagine md5 " + md5 + ". " + e.getMessage());
|
||||
err.append("\n");
|
||||
} finally {
|
||||
if (fos != null)
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err.append("Impossibile salvare record md5 " + md5);
|
||||
err.append("\n");
|
||||
}
|
||||
} else if (jRequest.has("originalPath")) {
|
||||
String originalFilePath = jRequest.getString("originalPath");
|
||||
File originalFile = new File(originalFilePath);
|
||||
if (originalFile.exists()) {
|
||||
if (debug)
|
||||
DBAdapter.printDebug(debug, " _recognize_POST trovato " + originalFilePath);
|
||||
facer.setMd5(md5);
|
||||
facer.setId_faceRecognizer(frzer.getId_faceRecognizer());
|
||||
facer.setFileAlternativo(originalFilePath);
|
||||
ResParm rp = facer.save();
|
||||
if (!rp.getStatus()) {
|
||||
err.append("Impossibile salvare record md5 " + md5 + " filealt: " + originalFilePath);
|
||||
err.append("\n");
|
||||
}
|
||||
} else {
|
||||
JSONObject jMd5 = new JSONObject();
|
||||
jMd5.put("md5", md5);
|
||||
jNoImageA.put(jMd5);
|
||||
}
|
||||
} else {
|
||||
JSONObject jMd5 = new JSONObject();
|
||||
jMd5.put("md5", md5);
|
||||
jNoImageA.put(jMd5);
|
||||
}
|
||||
JSONObject jResponse = new JSONObject();
|
||||
if (facer.getId_facer() > 0L) {
|
||||
String currentFileName;
|
||||
if (facer.getFileAlternativo().isEmpty()) {
|
||||
currentFileName = facer.getFullFileName();
|
||||
} else {
|
||||
currentFileName = facer.getFileAlternativo();
|
||||
}
|
||||
if (debug)
|
||||
System.out.println(
|
||||
String.valueOf(DBAdapter.getNowTs()) + " _recognize_POST chiamo face recognition su " + String.valueOf(DBAdapter.getNowTs()));
|
||||
JSONArray jLabelsA = fa.faceRecognition(currentFileName, frzer);
|
||||
jResponse.put("md5", md5);
|
||||
jResponse.put("labels", jLabelsA);
|
||||
jResponses.put(jResponse);
|
||||
if (debug)
|
||||
DBAdapter.printDebug(debug, " _recognize_POST risposta da face recognition;\n" +
|
||||
jResponses.toString(4));
|
||||
}
|
||||
}
|
||||
if (err.length() > 0) {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Codice " + code + " Errore: " + err.toString());
|
||||
} else {
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Recognizer Ok.");
|
||||
joRes.put("code", code);
|
||||
if (jResponses.length() > 0)
|
||||
joRes.put("responses", jResponses);
|
||||
if (jNoImageA.length() > 0)
|
||||
joRes.put("noImg", jNoImageA);
|
||||
}
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Recognizer Ok");
|
||||
joRes.put("code", code);
|
||||
}
|
||||
if (debug) {
|
||||
System.out.println("************ _recognize_POST RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
debugHelper.writeLog("************ _recognize_POST RISPOSTA AL CLIENT *********************");
|
||||
debugHelper.writeLog(joRes.toString(4));
|
||||
}
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _recognize_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
debugHelper.writeLog("************ _recognize_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
debugHelper.writeLog(joRes.toString(4));
|
||||
debugHelper.writeLog("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void _syncFaceDetectionMethods_GET(HttpServletRequest req, HttpServletResponse res) {
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
try {
|
||||
JSONObject joRes = new JSONObject();
|
||||
FaceDetectionMethod fdm = new FaceDetectionMethod(apFull);
|
||||
Vectumerator<FaceDetectionMethod> vec = fdm.findByCR(new FaceDetectionMethodCR(), 0, 0);
|
||||
JSONArray jaFdm = new JSONArray();
|
||||
while (vec.hasMoreElements()) {
|
||||
FaceDetectionMethod row = (FaceDetectionMethod)vec.nextElement();
|
||||
JSONObject jRow = new JSONObject();
|
||||
jRow.put("codice", row.getCodice());
|
||||
jRow.put("flgDetectionType", row.getFlgDetectionType());
|
||||
jRow.put("descrizione", row.getDescrizione());
|
||||
jRow.put("descrizioneLunga", row.getDescrizioneLunga());
|
||||
jaFdm.put(jRow);
|
||||
}
|
||||
joRes.put("faceDetectionMethods", jaFdm);
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Trovati " + vec.getTotNumberOfRecords() + " metodi");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _trainingRun_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void _checkLabel_POST(HttpServletRequest req, HttpServletResponse res) {
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
ResParm rp = new ResParm(true);
|
||||
boolean debug = false;
|
||||
try {
|
||||
JSONObject jo = getApiJSONObject();
|
||||
if (debug) {
|
||||
System.out.println("************ _checklabel_POST *********************");
|
||||
System.out.println("json in arrivo");
|
||||
System.out.println(jo.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
JSONObject joRes = new JSONObject();
|
||||
long label = jo.getLong("label");
|
||||
String md5 = jo.getString("md5");
|
||||
boolean reqImage = jo.getBoolean("reqImage");
|
||||
boolean addLabel = jo.getBoolean("addLabel");
|
||||
FaceRecognizer frzer = new FaceRecognizer(apFull);
|
||||
String code = "";
|
||||
if (jo.has("code")) {
|
||||
code = jo.getString("code");
|
||||
frzer.findByCode(code);
|
||||
}
|
||||
Face face = new Face(apFull);
|
||||
if (!md5.isEmpty()) {
|
||||
face.findByMd5(md5);
|
||||
if (debug)
|
||||
System.out.println("-- face find by md5--> " + face.getDescRecord());
|
||||
} else if (face.getId_face() == 0L && label > 0L) {
|
||||
face.findByLabel(label);
|
||||
}
|
||||
if (face.getId_face() == 0L) {
|
||||
if (addLabel && !md5.isEmpty()) {
|
||||
face.setMd5(md5);
|
||||
rp = face.save();
|
||||
if (debug)
|
||||
System.out.println("-- face add nuovo by md5--> " + face.getDescRecord());
|
||||
if (rp.getStatus()) {
|
||||
StringBuilder err = new StringBuilder();
|
||||
String imageContent = jo.getJSONObject("image")
|
||||
.getString("content");
|
||||
File path = new File(face.getPathFaceImg());
|
||||
if (!path.exists())
|
||||
path.mkdirs();
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(imageContent);
|
||||
File file = new File(face.getFullFileName());
|
||||
if (!file.exists()) {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(file);
|
||||
fos.write(decodedBytes);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
err.append("Impossibile salvare l'immagine md5 " + face.getMd5() + ". " + e.getMessage());
|
||||
err.append("\n");
|
||||
} finally {
|
||||
if (fos != null)
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
if (err.length() > 0) {
|
||||
handleDebug(err.toString());
|
||||
rp.setStatus(false);
|
||||
rp.setMsg(err.toString());
|
||||
}
|
||||
} else {
|
||||
handleDebug(rp.getErrMsg());
|
||||
}
|
||||
FaceFaceRecognizer ffr = new FaceFaceRecognizer(getApFull());
|
||||
if (frzer.getId_faceRecognizer() == 0L) {
|
||||
rp.setStatus(false);
|
||||
rp.setMsg("Attenzione! Aggiunta immagine ma impossibile associare a recognizer nullo!");
|
||||
} else {
|
||||
ffr.setId_faceRecognizer(frzer.getId_faceRecognizer());
|
||||
ffr.setId_face(face.getId_face());
|
||||
ffr.setId_faceLabel(face.getId_face());
|
||||
rp = ffr.save();
|
||||
if (debug)
|
||||
System.out.println("-- ffr add nuovo dopo face--> " + ffr.getDescRecord());
|
||||
}
|
||||
if (debug)
|
||||
System.out.println("-- prima della scrittura finale se creato addlabel --> " + ffr.getDescRecord());
|
||||
if (rp.getStatus()) {
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", rp.getMsg());
|
||||
joRes.put("id", ffr.getId_face());
|
||||
joRes.put("label", ffr.getId_faceLabel());
|
||||
joRes.put("labelMd5", face.getMd5());
|
||||
} else {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", rp.getMsg());
|
||||
joRes.put("id", ffr.getId_face());
|
||||
joRes.put("label", ffr.getId_faceLabel());
|
||||
joRes.put("labelMd5", face.getMd5());
|
||||
}
|
||||
} else {
|
||||
rp.setStatus(false);
|
||||
rp.setMsg("Label " + label + " non trovata ");
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Label " + label + " non trovata: " + rp.getMsg());
|
||||
}
|
||||
} else {
|
||||
FaceFaceRecognizer ffr = new FaceFaceRecognizer(getApFull());
|
||||
if (addLabel) {
|
||||
if (frzer.getId_faceRecognizer() == 0L) {
|
||||
rp.setStatus(false);
|
||||
rp.setMsg("Attenzione! Aggiunta immagine ma impossibile associare a recognizer nullo!");
|
||||
} else {
|
||||
ffr.findByRecognizerLabel(frzer.getId_faceRecognizer(), face.getId_face());
|
||||
if (debug)
|
||||
System.out.println("-- ffr add o modificato trovato???--> " + ffr.getDescRecord());
|
||||
if (ffr.getId_faceFaceRecognizer() == 0L || ffr.getId_face() != ffr.getId_faceLabel()) {
|
||||
ffr.setId_faceRecognizer(frzer.getId_faceRecognizer());
|
||||
ffr.setId_face(face.getId_face());
|
||||
ffr.setId_faceLabel(face.getId_face());
|
||||
ffr.save();
|
||||
if (debug)
|
||||
System.out.println("-- ffr add o modificato se trovato face con md5--> " + ffr.getDescRecord());
|
||||
}
|
||||
}
|
||||
} else if (frzer.getId_faceRecognizer() == 0L) {
|
||||
rp.setStatus(false);
|
||||
rp.setMsg("Attenzione! Impossibile associare a recognizer nullo!");
|
||||
} else {
|
||||
ffr.findByRecognizerLabel(frzer.getId_faceRecognizer(), face.getId_face());
|
||||
if (ffr.getId_faceFaceRecognizer() == 0L) {
|
||||
ffr.setId_faceRecognizer(frzer.getId_faceRecognizer());
|
||||
ffr.setId_face(face.getId_face());
|
||||
ffr.save();
|
||||
}
|
||||
}
|
||||
if (debug)
|
||||
System.out.println("-- prima della scrittura finale se trovato --> " + ffr.getDescRecord());
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", rp.getMsg());
|
||||
joRes.put("id", ffr.getId_face());
|
||||
joRes.put("label", ffr.getId_faceLabel());
|
||||
joRes.put("labelMd5", face.getMd5());
|
||||
if (reqImage) {
|
||||
JSONObject jImage = new JSONObject();
|
||||
byte[] fileContent = FileUtils.readFileToByteArray(new File(face.getFullFileName()));
|
||||
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
||||
jImage.put("content", encodedString);
|
||||
joRes.put("image", jImage);
|
||||
}
|
||||
}
|
||||
if (debug) {
|
||||
System.out.println("************ _checklabel_POST RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
System.out.println(String.valueOf(DBAdapter.getNow()) + " _checkLabel_POST. md5: " + String.valueOf(DBAdapter.getNow()) + " label: " + md5);
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _checklabel_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void _trainingRun_POST(HttpServletRequest req, HttpServletResponse res) {
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
boolean debug = true;
|
||||
try {
|
||||
JSONObject jo = getApiJSONObject();
|
||||
if (debug) {
|
||||
System.out.println("************ _trainingRun_POST *********************");
|
||||
System.out.println("json in arrivo");
|
||||
System.out.println(jo.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
}
|
||||
JSONObject joRes = new JSONObject();
|
||||
String code = jo.getString("code");
|
||||
FaceRecognizer frzer = new FaceRecognizer(apFull);
|
||||
frzer.findByCode(code);
|
||||
if (frzer.getId_faceRecognizer() == 0L) {
|
||||
if (debug)
|
||||
System.out.println("_trainingRun_POST RECOGNIZER NON TROVATO ");
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "ERRORE! Recognizer " + code + " non trovato.");
|
||||
} else {
|
||||
if (debug)
|
||||
System.out.println("_trainingRun_POST trainingAndCreateModel su thread separato.... ");
|
||||
Thread thread = new Thread(() -> {
|
||||
ResParm rpT = new ResParm();
|
||||
try {
|
||||
rpT = frzer.trainingAndCreateModel(true);
|
||||
if (debug)
|
||||
System.out.println("_trainingRun_POST MODELLO CREATO.... " + rpT.getMsg());
|
||||
} catch (Exception e) {
|
||||
if (debug)
|
||||
System.out.println("_trainingRun_POST MODELLO NON CREATO....: " + e.getMessage());
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
if (debug)
|
||||
System.out.println("_trainingRun_POST preparo la risposta........................................... ");
|
||||
joRes.put("code", code);
|
||||
joRes.put("numberTrainingImages", frzer.getNTrainingImages());
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Training concluso con successo. Creazione modello in corso...");
|
||||
}
|
||||
if (debug) {
|
||||
System.out.println("************ _trainingRun_POST RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
}
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _trainingRun_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void _resetTraining_POST(HttpServletRequest req, HttpServletResponse res) {
|
||||
ApplParmFull apFull = getApFull(req);
|
||||
try {
|
||||
JSONObject jo = getApiJSONObject();
|
||||
JSONObject joRes = new JSONObject();
|
||||
String code = jo.getString("code");
|
||||
FaceRecognizer frzer = new FaceRecognizer(apFull);
|
||||
frzer.findByCode(code);
|
||||
if (frzer.getId_faceRecognizer() == 0L) {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "ERRORE! Recognizer " + code + " non trovato.");
|
||||
} else {
|
||||
ResParm rp = frzer.resetTraining();
|
||||
if (rp.getStatus()) {
|
||||
joRes.put("success", "true");
|
||||
joRes.put("msg", "Codice " + code + " resettato");
|
||||
} else {
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Codice " + code + " NON resettato correttamente: " + rp.getMsg());
|
||||
}
|
||||
}
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JSONObject joRes = new JSONObject();
|
||||
joRes.put("success", "false");
|
||||
joRes.put("msg", "Eccezione!! " + e.getMessage());
|
||||
System.out.println("************ _resetTrainig_POST ECCEZIONE!!!! RISPOSTA AL CLIENT *********************");
|
||||
System.out.println(joRes.toString(4));
|
||||
System.out.println("*-------------- FINE ---------------------------*");
|
||||
sendJsonMsgResponse(req, res, joRes.toString());
|
||||
}
|
||||
}
|
||||
|
||||
protected Class<? extends _AblRestAdapter> getConcreteApiClass() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
package it.acxent.face.fr.rest;
|
||||
Loading…
Add table
Add a link
Reference in a new issue